package com.lea.ui.ftp;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.lea.exception.CheckException;
import com.lea.exception.WritingException;
import com.lea.service.FtpService;
import com.lea.ui.Facade;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 * Auto config generator dialog.
 * 
 */

public class AutoConfiguratorPanel extends JDialog {
	private JLabel labelHost;
	private JLabel labelPort;
	private JTextField textFieldAddonsRemotePath;
	private JLabel labelAddons;
	private JLabel labelPbos;
	private JTextField textFieldPbosRemotePath;
	private JLabel labelProfilesPath;
	private JLabel labelMissionsPath;
	private JTextField textFieldRemoteProfilesPath;
	private JTextField textFieldRemoteMissionsPath;
	private JCheckBox checkBoxAnonymous;
	private JTextField textFieldLogin;
	private JTextField textFieldPort;
	private JLabel labelLogin;
	private JButton buttonClose;
	private JButton buttonGenerate;
	private JTextField textFieldHost;
	private JLabel labelPassword;
	private JTextField textFieldPassword;
	private Facade facade;

	public AutoConfiguratorPanel(Facade facade) {
		super(facade.getMissionEditor(), "Auto-config generator", true);

		this.facade = facade;
		this.setSize(364, 340);
		this.setLayout(null);
		setResizable(false);

		this.setLocation((int) facade.getMissionEditor().getLocation().getX()
				+ facade.getMissionEditor().getWidth() / 2 - this.getWidth()
				/ 2, (int) facade.getMissionEditor().getLocation().getY()
				+ facade.getMissionEditor().getHeight() / 2 - this.getHeight()
				/ 2);

		Image icone = Toolkit
				.getDefaultToolkit()
				.getImage(
						java.lang.ClassLoader
								.getSystemResource("resources/pictures/system/lea.png"));
		setIconImage(icone);

		{
			labelHost = new JLabel();
			getContentPane().add(labelHost);
			labelHost.setBounds(10, 23, 53, 25);
			labelHost.setText("Host or url");
		}
		{
			labelPassword = new JLabel();
			getContentPane().add(labelPassword);
			labelPassword.setText("Password");
			labelPassword.setBounds(10, 92, 57, 24);
			textFieldPassword = new JTextField();
			getContentPane().add(textFieldPassword);
			textFieldPassword.setBounds(73, 92, 176, 24);
			labelPassword.setLabelFor(textFieldPassword);
		}
		{
			textFieldHost = new JTextField();
			getContentPane().add(textFieldHost);
			textFieldHost.setBounds(73, 24, 176, 24);
			textFieldHost.setText("ftp://");
		}
		{
			buttonGenerate = new JButton();
			getContentPane().add(buttonGenerate);
			buttonGenerate.setText("Generate");
			buttonGenerate.setBounds(169, 274, 80, 28);
		}
		{
			buttonClose = new JButton();
			getContentPane().add(buttonClose);
			buttonClose.setText("Close");
			buttonClose.setBounds(260, 274, 77, 28);
		}
		{
			labelLogin = new JLabel();
			labelLogin.setText("Login");
			getContentPane().add(labelLogin);
			labelLogin.setBounds(10, 57, 51, 24);
		}
		{
			labelPort = new JLabel();
			getContentPane().add(labelPort);
			labelPort.setText("Port");
			labelPort.setBounds(259, 26, 28, 20);
		}
		{
			textFieldPort = new JTextField();
			getContentPane().add(textFieldPort);
			textFieldPort.setText("21");
			textFieldPort.setBounds(287, 25, 37, 22);
		}
		{
			textFieldLogin = new JTextField();
			getContentPane().add(textFieldLogin);
			textFieldLogin.setBounds(73, 58, 176, 23);
		}
		{
			checkBoxAnonymous = new JCheckBox();
			getContentPane().add(checkBoxAnonymous);
			checkBoxAnonymous.setText("Anonymous");
			checkBoxAnonymous.setBounds(254, 59, 82, 20);
		}
		{
			textFieldRemoteMissionsPath = new JTextField();
			getContentPane().add(textFieldRemoteMissionsPath);
			textFieldRemoteMissionsPath.setBounds(73, 127, 176, 25);
		}
		{
			textFieldRemoteProfilesPath = new JTextField();
			getContentPane().add(textFieldRemoteProfilesPath);
			textFieldRemoteProfilesPath.setBounds(73, 164, 176, 24);
		}
		{
			labelMissionsPath = new JLabel();
			getContentPane().add(labelMissionsPath);
			labelMissionsPath.setText("/../missions");
			labelMissionsPath.setBounds(10, 132, 59, 15);
		}
		{
			labelProfilesPath = new JLabel();
			getContentPane().add(labelProfilesPath);
			labelProfilesPath.setText("/../profiles");
			labelProfilesPath.setBounds(10, 168, 53, 17);
		}
		{
			textFieldPbosRemotePath = new JTextField();
			getContentPane().add(textFieldPbosRemotePath);
			textFieldPbosRemotePath.setBounds(73, 199, 176, 25);
		}
		{
			labelPbos = new JLabel();
			getContentPane().add(labelPbos);
			labelPbos.setText("/../pbos");
			labelPbos.setBounds(10, 200, 53, 22);
		}
		{
			labelAddons = new JLabel();
			getContentPane().add(labelAddons);
			labelAddons.setText("/../addons");
			labelAddons.setBounds(12, 240, 61, 17);
		}
		{
			textFieldAddonsRemotePath = new JTextField();
			getContentPane().add(textFieldAddonsRemotePath);
			textFieldAddonsRemotePath.setBounds(73, 237, 176, 25);
		}
		buttonGenerate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				generateButtonPerformed();
			}
		});
		buttonClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				closeButtonPerformed();
			}
		});
		checkBoxAnonymous.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				checkBoxAnonymousPerformed();
			}
		});
		// Add Listeners
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				menuExitPerformed();
			}
		});
	}

	private void checkBoxAnonymousPerformed() {
		if (checkBoxAnonymous.isSelected()) {
			textFieldLogin.setText("anonymous");
			textFieldLogin.setEnabled(false);
			textFieldPassword.setText("");
			textFieldPassword.setEnabled(false);
		} else {
			textFieldLogin.setText("");
			textFieldLogin.setEnabled(true);
			textFieldPassword.setEnabled(true);
		}
	}

	private void generateButtonPerformed() {

		String url = textFieldHost.getText().trim();
		String port = textFieldPort.getText().trim();
		String login = textFieldLogin.getText().trim();
		String password = textFieldPassword.getText();
		String remoteMissionsFolderPath = textFieldRemoteMissionsPath.getText()
				.trim();
		String remoteProfilesFolderPath = textFieldRemoteProfilesPath.getText()
				.trim();
		String remotePbosFolderPath = textFieldPbosRemotePath.getText().trim();
		String remoteAddonsFolderPath = textFieldAddonsRemotePath.getText()
				.trim();

		FtpService ftpService = new FtpService();

		try {
			ftpService.checkData(url, port, login, password);
			JFileChooser fc = new JFileChooser();
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int returnVal = fc.showOpenDialog(AutoConfiguratorPanel.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();
				ftpService.generateAutoConfig(url, port, login, password,
						file.getAbsolutePath(), remoteMissionsFolderPath,
						remoteProfilesFolderPath, remotePbosFolderPath,
						remoteAddonsFolderPath);
				JOptionPane.showMessageDialog(this,
						"Auto-config successfully generated.", "Auto-config",
						JOptionPane.INFORMATION_MESSAGE);

				textFieldPassword.setText("");
				dispose();
				facade.getMissionEditor().setEnabled(true);
				facade.getMissionEditor().toFront();

			}
		} catch (CheckException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error",
					JOptionPane.WARNING_MESSAGE);
		} catch (WritingException e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	private void closeButtonPerformed() {
		textFieldPassword.setText("");
		dispose();
		facade.getMissionEditor().setEnabled(true);
		facade.getMissionEditor().toFront();
	}

	private void menuExitPerformed() {
		textFieldPassword.setText("");
		facade.getMissionEditor().setEnabled(true);
		facade.getMissionEditor().toFront();
	}
}
