package storagesim;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.border.EmptyBorder;

import lib.SQLConnection;

/**
 * Holds the window setup
 */
public class StorageWindow extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	
	private JTextField txtName;
	private JTextField txtMarke;
	private JTextField txtKaufpreis;
	private JTextField txtVerkaufspreis;
	private JTextField txtMenge;
	private JTextField txtHaltbarkeit;
	private JToggleButton tglbtnGekuehlt;
	
	private MapPanel mapPanel;
	private JButton btnUpdate;
	private JButton btnReset;
	
	SQLConnection db;

	/**
	 * Initialises the layout
	 */
	public StorageWindow() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(StorageWindow.class.getResource("/javax/swing/plaf/metal/icons/ocean/homeFolder.gif")));

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(200, 100, 960, 540);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] {424, 0, 0};
		gbl_contentPane.rowHeights = new int[] {252, 0, 0};
		gbl_contentPane.columnWeights = new double[] {3.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[] {1.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		setContentPane(contentPane);
		
		this.setTitle("SVEN Lagerkarte");
		
		this.db = new SQLConnection();

		mapPanel = new MapPanel(this);
		GridBagConstraints gbc_mapPanel = new GridBagConstraints();
		gbc_mapPanel.insets = new Insets(0, 0, 5, 0);
		gbc_mapPanel.fill = GridBagConstraints.BOTH;
		gbc_mapPanel.gridx = 0;
		gbc_mapPanel.gridy = 0;
		contentPane.add(mapPanel, gbc_mapPanel);
		
		JPanel sidePanel = new JPanel();
		GridBagConstraints gbc_sidePanel = new GridBagConstraints();
		gbc_sidePanel.fill = GridBagConstraints.VERTICAL;
		gbc_sidePanel.insets = new Insets(10, 10, 15, 10);
		gbc_sidePanel.gridx = 1;
		gbc_sidePanel.gridy = 0;
		contentPane.add(sidePanel, gbc_sidePanel);
		GridBagLayout gbl_sidePanel = new GridBagLayout();
		gbl_sidePanel.columnWidths = new int[]{40, 0};
		gbl_sidePanel.rowHeights = new int[]{15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_sidePanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_sidePanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		sidePanel.setLayout(gbl_sidePanel);
		
		JLabel lblMarke = new JLabel("Marke");
		GridBagConstraints gbc_lblMarke = new GridBagConstraints();
		gbc_lblMarke.insets = new Insets(0, 0, 5, 0);
		gbc_lblMarke.gridx = 0;
		gbc_lblMarke.gridy = 0;
		sidePanel.add(lblMarke, gbc_lblMarke);
		
		txtMarke = new JTextField();
		txtMarke.setEditable(false);
		GridBagConstraints gbc_txtMarke = new GridBagConstraints();
		gbc_txtMarke.insets = new Insets(0, 0, 5, 0);
		gbc_txtMarke.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtMarke.gridx = 0;
		gbc_txtMarke.gridy = 1;
		sidePanel.add(txtMarke, gbc_txtMarke);
		txtMarke.setColumns(10);
		
		JLabel lblName = new JLabel("Name");
		GridBagConstraints gbc_lblName = new GridBagConstraints();
		gbc_lblName.insets = new Insets(0, 0, 5, 0);
		gbc_lblName.gridx = 0;
		gbc_lblName.gridy = 2;
		sidePanel.add(lblName, gbc_lblName);
		
		txtName = new JTextField();
		txtName.setEditable(false);
		GridBagConstraints gbc_txtName = new GridBagConstraints();
		gbc_txtName.insets = new Insets(0, 0, 5, 0);
		gbc_txtName.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtName.gridx = 0;
		gbc_txtName.gridy = 3;
		sidePanel.add(txtName, gbc_txtName);
		txtName.setColumns(10);
		
		JLabel lblKaufpreis = new JLabel("Kaufpreis pro Stück");
		GridBagConstraints gbc_lblKaufpreis = new GridBagConstraints();
		gbc_lblKaufpreis.insets = new Insets(0, 0, 5, 0);
		gbc_lblKaufpreis.gridx = 0;
		gbc_lblKaufpreis.gridy = 4;
		sidePanel.add(lblKaufpreis, gbc_lblKaufpreis);
		
		txtKaufpreis = new JTextField();
		txtKaufpreis.setEditable(false);
		GridBagConstraints gbc_txtKaufpreis = new GridBagConstraints();
		gbc_txtKaufpreis.insets = new Insets(0, 0, 5, 0);
		gbc_txtKaufpreis.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtKaufpreis.gridx = 0;
		gbc_txtKaufpreis.gridy = 5;
		sidePanel.add(txtKaufpreis, gbc_txtKaufpreis);
		txtKaufpreis.setColumns(10);
		
		JLabel lblVerkaufspreis = new JLabel("Verkaufspreis");
		GridBagConstraints gbc_lblVerkaufspreis = new GridBagConstraints();
		gbc_lblVerkaufspreis.insets = new Insets(0, 0, 5, 0);
		gbc_lblVerkaufspreis.gridx = 0;
		gbc_lblVerkaufspreis.gridy = 6;
		sidePanel.add(lblVerkaufspreis, gbc_lblVerkaufspreis);
		
		txtVerkaufspreis = new JTextField();
		txtVerkaufspreis.setEditable(false);
		GridBagConstraints gbc_txtVerkaufspreis = new GridBagConstraints();
		gbc_txtVerkaufspreis.insets = new Insets(0, 0, 5, 0);
		gbc_txtVerkaufspreis.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtVerkaufspreis.gridx = 0;
		gbc_txtVerkaufspreis.gridy = 7;
		sidePanel.add(txtVerkaufspreis, gbc_txtVerkaufspreis);
		txtVerkaufspreis.setColumns(10);
		
		JLabel lblMenge = new JLabel("Menge");
		GridBagConstraints gbc_lblMenge = new GridBagConstraints();
		gbc_lblMenge.insets = new Insets(0, 0, 5, 0);
		gbc_lblMenge.gridx = 0;
		gbc_lblMenge.gridy = 8;
		sidePanel.add(lblMenge, gbc_lblMenge);
		
		txtMenge = new JTextField();
		txtMenge.setEditable(false);
		GridBagConstraints gbc_txtMenge = new GridBagConstraints();
		gbc_txtMenge.insets = new Insets(0, 0, 5, 0);
		gbc_txtMenge.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtMenge.gridx = 0;
		gbc_txtMenge.gridy = 9;
		sidePanel.add(txtMenge, gbc_txtMenge);
		txtMenge.setColumns(10);
		
		JLabel lblHaltbarkeit = new JLabel("Haltbar bis");
		GridBagConstraints gbc_lblHaltbarkeit = new GridBagConstraints();
		gbc_lblHaltbarkeit.insets = new Insets(0, 0, 5, 0);
		gbc_lblHaltbarkeit.gridx = 0;
		gbc_lblHaltbarkeit.gridy = 10;
		sidePanel.add(lblHaltbarkeit, gbc_lblHaltbarkeit);
		
		txtHaltbarkeit = new JTextField();
		txtHaltbarkeit.setEditable(false);
		GridBagConstraints gbc_txtHaltbarkeit = new GridBagConstraints();
		gbc_txtHaltbarkeit.insets = new Insets(0, 0, 5, 0);
		gbc_txtHaltbarkeit.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtHaltbarkeit.gridx = 0;
		gbc_txtHaltbarkeit.gridy = 11;
		sidePanel.add(txtHaltbarkeit, gbc_txtHaltbarkeit);
		txtHaltbarkeit.setColumns(10);
		
		tglbtnGekuehlt = new JToggleButton("Gekühlt");
		tglbtnGekuehlt.setEnabled(false);
		GridBagConstraints gbc_tglbtnGekuehlt = new GridBagConstraints();
		gbc_tglbtnGekuehlt.insets = new Insets(0, 0, 5, 0);
		gbc_tglbtnGekuehlt.gridx = 0;
		gbc_tglbtnGekuehlt.gridy = 12;
		sidePanel.add(tglbtnGekuehlt, gbc_tglbtnGekuehlt);
		
		JLabel lblClickToSelect = new JLabel("Click to select a storage tile!");
		GridBagConstraints gbc_lblClickToSelect = new GridBagConstraints();
		gbc_lblClickToSelect.insets = new Insets(0, 0, 5, 0);
		gbc_lblClickToSelect.gridx = 0;
		gbc_lblClickToSelect.gridy = 13;
		sidePanel.add(lblClickToSelect, gbc_lblClickToSelect);
		
		btnUpdate = new JButton("update");
		GridBagConstraints gbc_btnUpdate = new GridBagConstraints();
		gbc_btnUpdate.insets = new Insets(0, 0, 5, 0);
		gbc_btnUpdate.gridx = 0;
		gbc_btnUpdate.gridy = 15;
		sidePanel.add(btnUpdate, gbc_btnUpdate);
		btnUpdate.addActionListener(e -> {
			mapPanel.map.updateMap(db);
			repaint();
		});
		
		btnReset = new JButton("reset");
		GridBagConstraints gbc_btnReset = new GridBagConstraints();
		gbc_btnReset.gridx = 0;
		gbc_btnReset.gridy = 16;
		sidePanel.add(btnReset, gbc_btnReset);
		btnReset.addActionListener(e -> {
			mapPanel.resetView();
			repaint();
		});
		
		this.pack();

		// reset initial position
		mapPanel.resetView();
	}
	
	/**
	 * Shows the selected tile's product information
	 */
	public void updateSidebar(SQLConnection db) {
		int lagerplatz = mapPanel.map.selectedTile + 1;
		ArrayList<HashMap<String, String>> query = db.queryToMap("SELECT t_lager.Lagerplatz, t_artikel.Marke, t_artikel.Name, t_artikel.Einkaufspreis, " +
				"t_artikel.Verkaufspreis, t_lager.Menge, t_lager.Ankunft, t_artikel.`MHK in Tagen`, t_artikel.Gekühlt, t_artikel.Anzahl_pro_Palette FROM t_lager " + 
				"INNER JOIN t_artikel ON t_lager.f_Artikel_ID = t_artikel.ID " + 
				"WHERE Lagerplatz = " + lagerplatz);
		if (query.size() == 1) {
			HashMap<String, String> storage = query.get(0);
			
			this.txtMarke.setText(storage.get("Marke"));
			this.txtName.setText(storage.get("Name"));
			this.txtKaufpreis.setText(String.format(Locale.GERMAN, "%.2f €", Float.parseFloat(storage.get("Einkaufspreis"))));
			this.txtVerkaufspreis.setText(String.format(Locale.GERMAN, "%.2f €", Float.parseFloat(storage.get("Verkaufspreis"))));
			this.txtMenge.setText(storage.get("Menge") + " / " + storage.get("Anzahl_pro_Palette"));
			int haltbarkeit = Integer.parseInt(storage.get("MHK in Tagen"));
			LocalDate ankunft = LocalDate.parse(storage.get("Ankunft"));
			LocalDate ablauf = ankunft.plusDays(haltbarkeit);
			this.txtHaltbarkeit.setText(ablauf.toString());
			this.tglbtnGekuehlt.setSelected(storage.get("Gekühlt").equals("1"));
		} else {
			// reset side
			this.txtMarke.setText("");
			this.txtName.setText("");
			this.txtKaufpreis.setText("");
			this.txtVerkaufspreis.setText("");
			this.txtMenge.setText("");
			this.txtHaltbarkeit.setText("");
			this.tglbtnGekuehlt.setSelected(false);
		}
	}
}
