package industries.doublehelix.furrynotemanager.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.TreeSet;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import industries.doublehelix.furrynotemanager.Site;
import industries.doublehelix.furrynotemanager.User;
import industries.doublehelix.furrynotemanager.siteImporters.Importable;
import industries.doublehelix.furrynotemanager.siteImporters.SiteImporter;
import layout.TableLayout;

public class AddUserDialog extends JDialog implements ActionListener {
	private static final long serialVersionUID = 6043272317508195684L;
	JButton ok = new JButton("OK");
	JButton abort = new JButton("Abort");
	JComboBox<Site> siteBox;
	JTextField user = new JTextField();
	JPasswordField pass = new JPasswordField();
	
	private User returnUser = null;
	
	
	public AddUserDialog(Component parent) throws SQLException {
		this.setSize(new Dimension(250, 170));
		this.setTitle("Add new user");
		this.setLocationRelativeTo(parent);
		this.setModal(true);
		this.setResizable(false);
		
		// fill site combo
		// TODO: remove check for importer available as soon as all sites have one
		ArrayList<Site> sitesToDisplay = new ArrayList<>();
		TreeSet<Site> sites = Site.getAllSites();
		for (Site s : sites) {
			// default ctor of user does nothing
			Importable i = SiteImporter.getImportableForSite(s);
			if (i == null) continue; // no importer for site
			sitesToDisplay.add(s);
		}
		siteBox = new JComboBox<>(sitesToDisplay.toArray(new Site[sitesToDisplay.size()]));
		
		JPanel mainPanel = new JPanel();
		mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		double sizeSettings[][] = {{TableLayout.PREFERRED, 10, TableLayout.PREFERRED, TableLayout.FILL}, //width
				{TableLayout.PREFERRED, 10, TableLayout.PREFERRED, 10, TableLayout.PREFERRED, 10, TableLayout.PREFERRED}}; // height
		mainPanel.setLayout(new TableLayout(sizeSettings));
		
		JLabel labelUser = new JLabel("User");
		JLabel labelPass = new JLabel("Password");

		ok.addActionListener(this);
		user.addActionListener(this);
		pass.addActionListener(this);
		abort.addActionListener(this);
		
		mainPanel.add(siteBox, "0, 0, 3, 0");
		mainPanel.add(labelUser, "0, 2");
		mainPanel.add(user, "2, 2, 3, 2");
		mainPanel.add(labelPass, "0, 4");
		mainPanel.add(pass, "2, 4, 3, 4");
		mainPanel.add(ok, "0, 6");
		mainPanel.add(abort, "2, 6");
		
		
		this.add(mainPanel);
		
		
		this.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == ok || e.getSource() == user || e.getSource() == pass) {
			String usr = user.getText().trim();
			String pwd = new String(pass.getPassword()).trim();
			if (usr.isEmpty() || pwd.isEmpty()) return;
			
			Site site = (Site)siteBox.getSelectedItem();
			returnUser = new User(site, usr, pwd);
			this.dispose();
		} else {
			this.dispose();
		}
	}

	/**
	 * null if user aborted
	 * @return null if user aborted
	 */
	public User getUser() {
		return returnUser;
	}
}
