/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.tests.lwjgl;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;

import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.backends.lwjgl.LwjglFiles;
import com.badlogic.gdx.backends.lwjgl.LwjglPreferences;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.tests.utils.GdxTest;
import com.badlogic.gdx.tests.utils.GdxTests;

public class LwjglTestStarter extends JFrame {
	public LwjglTestStarter () throws HeadlessException {
		super("libgdx Tests");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setContentPane(new TestList());
		pack();
		setSize(getWidth(), 600);
		setLocationRelativeTo(null);
		setVisible(true);
	}

	/**
	 * Runs the {@link GdxTest} with the given name.
	 * 
	 * @param testName the name of a test class
	 * @return {@code true} if the test was found and run, {@code false} otherwise
	 */
	public static boolean runTest (String testName) {
		GdxTest test = GdxTests.newTest(testName);
		if (test == null) {
			return false;
		}
		LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
		config.width = 640;
		config.height = 480;
		config.title = testName;
		config.forceExit = false;
		new LwjglApplication(test, config);
		return true;
	}

	class TestList extends JPanel {
		public TestList () {
			setLayout(new BorderLayout());

			final JButton button = new JButton("Run Test");

			final JList list = new JList(GdxTests.getNames().toArray());
			JScrollPane pane = new JScrollPane(list);

			DefaultListSelectionModel m = new DefaultListSelectionModel();
			m.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			m.setLeadAnchorNotificationEnabled(false);
			list.setSelectionModel(m);

			list.addMouseListener(new MouseAdapter() {
				public void mouseClicked (MouseEvent event) {
					if (event.getClickCount() == 2) button.doClick();
				}
			});

			list.addKeyListener(new KeyAdapter() {
				public void keyPressed (KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) button.doClick();
				}
			});

			final Preferences prefs = new LwjglPreferences(new FileHandle(new LwjglFiles().getExternalStoragePath()
				+ ".prefs/lwjgl-tests"));
			list.setSelectedValue(prefs.getString("last", null), true);

			button.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed (ActionEvent e) {
					String testName = (String)list.getSelectedValue();
					prefs.putString("last", testName);
					prefs.flush();
					dispose();
					runTest(testName);
				}
			});

			add(pane, BorderLayout.CENTER);
			add(button, BorderLayout.SOUTH);

			// GdxTest test = GdxTests.newTest("BitmapFontFlipTest");
			// new LwjglApplication(test, "Test", 480, 320, test.needsGL20());
		}
	}

	/**
	 * Runs a libgdx test.
	 * 
	 * If no arguments are provided on the command line, shows a list of tests to choose from.
	 * If an argument is present, the test with that name will immediately be run.
	 * 
	 * @param argv command line arguments
	 */
	public static void main (String[] argv) throws Exception {
		if (argv.length > 0) {
			if (runTest(argv[0])) {
				return;
				// Otherwise, fall back to showing the list
			}
		}
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		new LwjglTestStarter();
	}
}

@@ -1673,24 +1673,81 @@
 ileHandle;%0D%0A
+import com.badlogic.gdx.graphics.glutils.ShaderProgram;%0D%0A
 import com.b
@@ -1776,24 +1776,24 @@
 s.GdxTest;%0D%0A
-
 import com.b
@@ -2392,16 +2392,44 @@
 ame) %7B%0D%0A
+%09%09boolean useGL30 = false;%0D%0A
 %09%09GdxTes
@@ -2666,16 +2666,16 @@
 tName;%0D%0A
-
 %09%09config
@@ -2695,16 +2695,452 @@
 false;%0D%0A
+%09%09if (useGL30) %7B%0D%0A%09%09%09config.useGL30 = true;%0D%0A%09%09%09ShaderProgram.prependVertexCode = %22#version 140%5Cn#define varying out%5Cn#define attribute in%5Cn%22;%0D%0A%09%09%09ShaderProgram.prependFragmentCode = %22#version 140%5Cn#define varying in%5Cn#define texture2D texture%5Cn#define gl_FragColor fragColor%5Cnout vec4 fragColor;%5Cn%22;%0D%0A%09%09%7D else %7B%0D%0A%09%09%09config.useGL30 = false;%0D%0A%09%09%09ShaderProgram.prependVertexCode = %22%22;%0D%0A%09%09%09ShaderProgram.prependFragmentCode = %22%22;%09%09%09%0D%0A%09%09%7D%0D%0A
 %09%09new Lw

Test to make (most of) the shaders work for gles3, see #2087