import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JSlider;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.awt.event.ActionEvent;
import java.time.LocalDate;
import javax.swing.event.ChangeListener;

import org.mindrot.BCrypt;

import storagesim.Simulation;
import storagesim.StorageWindow;

import javax.swing.event.ChangeEvent;
import java.awt.Toolkit;
import lib.SQLConnection;

public class Grafische_Oberfläche extends JFrame {

	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JTextField textField;
	private JTextField txtpw;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Grafische_Oberfläche frame = new Grafische_Oberfläche();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	LocalDate currentTime;

	/**
	 * Create the frame.
	 */
	public Grafische_Oberfläche() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(Grafische_Oberfläche.class.getResource("/javax/swing/plaf/metal/icons/ocean/homeFolder.gif")));
		setTitle("Lager Verwaltung");
		
		currentTime = LocalDate.parse("2018-01-01");
		
		SQLConnection db = new SQLConnection("102-012/sven");
		
		ArrayList<HashMap<String, String>> MaximaleMenge = db.queryToMap("SELECT * FROM t_artikel");		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 277, 496);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblMenge = new JLabel("Menge =");
		lblMenge.setHorizontalAlignment(SwingConstants.LEFT);
		lblMenge.setBounds(36, 108, 93, 14);
		contentPane.add(lblMenge);
		lblMenge.setVisible(false);
		
		JTextField txtDatum = new JTextField(currentTime.toString());
		txtDatum.setBounds(10, 329, 91, 33);
		contentPane.add(txtDatum);
		txtDatum.setEditable(false);
		txtDatum.setVisible(false);
		
		JButton btnPlus1 = new JButton("+ 1");
		btnPlus1.setBounds(100, 329, 36, 33);
		btnPlus1.addActionListener((a) -> {
			currentTime = currentTime.plusDays(1);
			txtDatum.setText(currentTime.toString());
		});
		contentPane.add(btnPlus1);
		btnPlus1.setVisible(false);
		
		JButton btnPlus7 = new JButton("+ 7");
		btnPlus7.setBounds(136, 329, 36, 33);
		btnPlus7.addActionListener((a) -> {
			currentTime = currentTime.plusDays(7);
			txtDatum.setText(currentTime.toString());
		});
		contentPane.add(btnPlus7);
		btnPlus7.setVisible(false);
		
		JButton btnWchentlichesberprfen = new JButton("Zeitverstreichung simulieren");
		btnWchentlichesberprfen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new Simulation().run(currentTime);
				System.out.println("Fertiggestellt");
			}
		});
		btnWchentlichesberprfen.setBounds(10, 369, 241, 33);
		contentPane.add(btnWchentlichesberprfen);
		btnWchentlichesberprfen.setVisible(false);
		
		JButton btnSimulationStarten = new JButton("Lageransicht öffnen");
		btnSimulationStarten.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					@Override
					public void run() {
						try {
							StorageWindow w = new StorageWindow();
							w.setVisible(true);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
		});
		btnSimulationStarten.setBounds(10, 413, 241, 33);
		contentPane.add(btnSimulationStarten);
		btnSimulationStarten.setVisible(false);
		
		JSlider slider = new JSlider();
		slider.setMinimum(1);
		slider.setMaximum(1);
		slider.setPaintTicks(true);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				lblMenge.setText("Menge = " + slider.getValue());
			}
		});
		slider.setOrientation(SwingConstants.VERTICAL);
		slider.setMajorTickSpacing(25);
		slider.setMinorTickSpacing(1);
		slider.setBounds(149, 20, 102, 328);
		contentPane.add(slider);
		slider.setVisible(false);
		
		JLabel lblItemId = new JLabel("Item ID");
		lblItemId.setHorizontalAlignment(SwingConstants.CENTER);
		lblItemId.setBounds(36, 20, 46, 20);
		contentPane.add(lblItemId);
		lblItemId.setVisible(false);
		
		JComboBox<Integer> comboBox = new JComboBox<>();
		comboBox.setVisible(false);
		
		
		for(int i = 1; i <= 50; i++)
		{
			comboBox.addItem(i);
		}
		comboBox.setSelectedIndex(0);
		slider.setMaximum(Integer.parseInt(MaximaleMenge.get(comboBox.getSelectedIndex()).get("Anzahl_pro_Palette")));
		lblMenge.setText("Menge = " + slider.getValue());
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				if(Integer.parseInt(MaximaleMenge.get(comboBox.getSelectedIndex()).get("Anzahl_pro_Palette")) != 0)
				{
					slider.setMaximum(Integer.parseInt(MaximaleMenge.get(comboBox.getSelectedIndex()).get("Anzahl_pro_Palette")));
				}
			}
		});
		comboBox.setBounds(36, 59, 42, 27);
		contentPane.add(comboBox);
		
		JButton btnNachbestellen = new JButton("Nachbestellen");
		btnNachbestellen.setVisible(false);
		btnNachbestellen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				//SQLConnection db = new SQLConnection("192.168.102.16/sven");
				
				LocalDate ankunft;
				if((comboBox.getSelectedIndex()+1) > 0)
				{
				ankunft = currentTime.plusDays(Integer.parseInt(MaximaleMenge.get((comboBox.getSelectedIndex())).get("Lieferzeit")));
				db.execute("INSERT INTO `t_bestellungen` (`ID`, `f_Artikel_ID`, `Menge`, `Ankunftsdatum`) VALUES (NULL, " + 
						(comboBox.getSelectedIndex()+1) + ", " + 
						slider.getValue() + ", '" +
						ankunft + "')");
				}
			}
		});
		btnNachbestellen.setBounds(10, 209, 119, 33);
		contentPane.add(btnNachbestellen);
		
		JLabel lblBenutzername = new JLabel("Benutzername:");
		lblBenutzername.setBounds(10, 65, 119, 20);
		contentPane.add(lblBenutzername);
		
		textField = new JTextField();
		textField.setBounds(10, 90, 241, 33);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblPasswort = new JLabel("Passwort:");
		lblPasswort.setBounds(10, 133, 188, 20);
		contentPane.add(lblPasswort);
		
		txtpw = new JTextField();
		txtpw.setBounds(10, 158, 241, 33);
		contentPane.add(txtpw);
		txtpw.setColumns(10);
		
		JButton btnAnmelden = new JButton("Anmelden");
		btnAnmelden.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(textField.getText() != "")
				{
					ArrayList<HashMap<String, String>> benutzer = db.queryToMap("SELECT * FROM t_benutzer WHERE Name = '" + textField.getText() + "'");
					//System.out.println(benutzer);
					if (benutzer.size() == 1) {

						String hash = benutzer.get(0).get("Passwort");
						String password = txtpw.getText();
						//bcrypt prefix backport
						if(hash.toCharArray()[2] == 'y') {
							char[] hashArray = hash.toCharArray();
							hashArray[2] = 'a';
							hash = String.valueOf(hashArray);
						}
					
						if(BCrypt.checkpw(password, hash))
						{
							System.out.println("Eingeloggt");
							lblMenge.setVisible(true);
							btnWchentlichesberprfen.setVisible(true);
							btnSimulationStarten.setVisible(true);
							slider.setVisible(true);
							lblItemId.setVisible(true);
							txtDatum.setVisible(true);
							btnPlus1.setVisible(true);
							btnPlus7.setVisible(true);
							btnNachbestellen.setVisible(true);
							comboBox.setVisible(true);
							lblBenutzername.setVisible(false);
							textField.setVisible(false);
							lblPasswort.setVisible(false);
							txtpw.setVisible(false);
							btnAnmelden.setVisible(false);
							
						}
					}else
					{
						System.out.println("Benutzer nicht vorhanden");
					}
				}
			}
		});
		btnAnmelden.setBounds(61, 209, 126, 33);
		contentPane.add(btnAnmelden);
	}
}
