package org.jerrymouse.gaes.mavenPlugin.gui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.text.DefaultCaret;

import org.jerrymouse.gaes.mavenPlugin.base.MvnCommond;

public class Main extends JPanel {
	static private final String newline = "\n";
	JButton openButton;
	JPanel headPanel;
	JButton okButton;
	JComboBox optionlist;
	JStreamedTextArea log;
	File file;
	JFileChooser fc;
	JButton stopButton;
	MvnCommond commond;

	public Main() {
		log = new JStreamedTextArea(30, 70);
		log.setEditable(false);
		log.setFont(new Font("Monospaced", 0, 12));

		JScrollPane logScrollPane = new JScrollPane(log);
		// Create a file chooser
		fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		headPanel = new JPanel();
		openButton = new JButton("Open the Project Dictionary");
		openButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int returnVal = fc.showOpenDialog(Main.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					file = fc.getSelectedFile();
					openButton.setText(file.getName());
				} else {
				}
			}
		});

		okButton = new JButton("start");
		stopButton = new JButton("stop");
		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String cmd = (String) optionlist.getSelectedItem();
				List<String> args = Arrays.asList(cmd.split(" "));
				commond = new MvnCommond(args, file, new PrintStream(log
						.getOutputStream()));
				commond.start();
			}
		});

		stopButton.addActionListener(new ActionListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent e) {
				commond.stop();
			}
		});
		String[] options = { "gaes:config -Dnormal", "gaes:config -Dgae",
				"gaes:config -Dhadoop", "gaes:run", "gaes:deploy" };
		optionlist = new JComboBox(options);
		headPanel.add(openButton);
		headPanel.add(optionlist);
		headPanel.add(okButton);
		headPanel.add(stopButton);

		// Add the buttons and the log to this panel.
		setLayout(new BorderLayout());
		add(headPanel, BorderLayout.NORTH);
		add(logScrollPane, BorderLayout.CENTER);
		DefaultCaret caret = (DefaultCaret) log.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
	}

	private static void createAndShowGUI() {
		// Make sure we have nice window decorations.
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Create and set up the window.
		JFrame frame = new JFrame("Common Cloud");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Create and set up the content pane.
		JComponent newContentPane = new Main();
		newContentPane.setOpaque(true); // content panes must be opaque
		frame.setContentPane(newContentPane);

		// Display the window.
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}
