package application;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class UI {
	private static final boolean all_transformers = false;
	static JButton transformButton;
	static JButton loadButton;
	public static List<UITask> ui_tf = new ArrayList<UITask>();
	public static List<UITask> ui_tm = new ArrayList<UITask>();

	private static void showUI() {
		JFrame frame = new JFrame("TimeDistortion");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		var content = frame.getContentPane();

		JPanel loadPanel = new JPanel(new GridLayout(0, 1));
		content.add(loadPanel, BorderLayout.NORTH);
		loadButton = new JButton("Load");
		loadPanel.add(loadButton);
		JLabel loadlabel = new JLabel("undefined input");
		loadPanel.add(loadlabel);
		loadButton.addActionListener(new LoadAction(frame, loadlabel));

		GridLayout taskGrid = new GridLayout(1, 2);
		JPanel taskPanel = new JPanel(taskGrid);
		GridLayout slitGrid = new GridLayout(0, 1);
		JPanel slitPanel = new JPanel(slitGrid);
		addTransformers(slitPanel);
		taskPanel.add(slitPanel);
		GridLayout timeGrid = new GridLayout(0, 1);
		JPanel timePanel = new JPanel(timeGrid);
		addTimeMappers(timePanel);
		taskPanel.add(timePanel);

		content.add(taskPanel, BorderLayout.CENTER);

		JPanel startPanel = new JPanel(new GridLayout(0, 1));
		content.add(startPanel, BorderLayout.SOUTH);
		JLabel statuslabel = new JLabel("no output");
		transformButton = new JButton("Transform");
		transformButton.addActionListener(new TransformAction(statuslabel));
		startPanel.add(transformButton);
		startPanel.add(statuslabel);

		frame.pack();
		frame.setVisible(true);
	}

	private static void addTransformers(JPanel slitPanel) {
		addTransformer(slitPanel, transformer.HalfReverse.class);
		addTransformer(slitPanel, transformer.HalfVelocity.class);
		addTransformer(slitPanel, transformer.Horizontal.class);
		addTransformer(slitPanel, transformer.Vertical.class);
		addTransformer(slitPanel, transformer.Max.class);
		addTransformer(slitPanel, transformer.TriChrome.class);
		addTransformer(slitPanel, transformer.Radial.class);
		addTransformer(slitPanel, transformer.Manhattan.class);
		if(all_transformers) {
			addTransformer(slitPanel, transformer.Average.class);
			addTransformer(slitPanel, transformer.Curtain.class);
			addTransformer(slitPanel, transformer.TriChromeSum.class);
		}
	}

	private static void addTransformer(JPanel slitPanel, Class<?> class1) {
		JCheckBox tf = new JCheckBox(class1.getName());
		ui_tf.add(new UITask(class1, tf));
		slitPanel.add(tf);
	}

	private static void addTimeMappers(JPanel timePanel) {
		addTimeMapper(timePanel, timemapper.Linear.class);
		addTimeMapper(timePanel, timemapper.OneMinusLinear.class);
		addTimeMapper(timePanel, timemapper.Sinus.class);
		addTimeMapper(timePanel, timemapper.OneMinusSinus.class);
	}

	private static void addTimeMapper(JPanel timePanel, Class<?> class1) {
		JCheckBox tm = new JCheckBox(class1.getName());
		ui_tm.add(new UITask(class1, tm));
		timePanel.add(tm);
	}

	public static void main(String[] args) {
		TaskEnvironment.scanProgramArgs(args);

		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				showUI();
			}
		});
	}
}
