package gui.simulator;

import gui.frames.MainFrame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SimulatorDialogPane extends JDialog{

	    private JTextField length;
	    private JTextField width;
	    private JComboBox<String> terrain;
	    private MainFrame mainFrame;
	    private int terrainInt;
	    
	    
	    public SimulatorDialogPane(MainFrame mainFrame){
	    	this.mainFrame = mainFrame;
	    	this.terrainInt = 1;
	        this.getContentPane().setBackground( Color.WHITE );
	        JPanel main = new JPanel();
	        main.setOpaque(false);
	        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
	        Label label = new Label("Simulator Specification");
	        label.setBackground(Color.WHITE);
	        label.setFont(new Font("Arial", Font.PLAIN, 14));
	        main.add(label);
	        main.add(this.widthPanel());
	        main.add(this.lengthPanel());
	        main.add(this.terrainChoosePanel());
	        main.add(this.buttons());
	        main.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
	        
	        Toolkit toolkit = Toolkit.getDefaultToolkit();
	        Dimension screenSize = toolkit.getScreenSize();
	        int x = (screenSize.width - this.getWidth()) / 2;
	        int y = (screenSize.height - this.getHeight()) / 2;
	        this.setLocation(x, y);
	        
	        super.add(main);
	        super.setVisible(true);
	        super.pack();
	    } 
	    
	    private JPanel lengthPanel(){
	        JPanel name = new JPanel();
	        name.setOpaque(false);
	        name.setLayout(new BoxLayout(name, BoxLayout.X_AXIS));
	        name.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
	        Label label = new Label("Length");
	        label.setBackground(Color.WHITE);
	        label.setFont(new Font("Arial", Font.PLAIN, 11));
	        name.add(label);
	        this.length = new JTextField();
	        name.add(this.length);
	        return name;
	    }
	    private JPanel widthPanel(){
	        JPanel name = new JPanel();
	        name.setOpaque(false);
	        name.setLayout(new BoxLayout(name, BoxLayout.X_AXIS));
	        name.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
	        Label label = new Label("Width");
	        label.setBackground(Color.WHITE);
	        label.setFont(new Font("Arial", Font.PLAIN, 11));
	        name.add(label);
	        this.width = new JTextField();
	        name.add(this.width);
	        return name;
	    }
	    private JPanel terrainChoosePanel(){
	        JPanel name = new JPanel();
	        name.setOpaque(false);
	        name.setLayout(new BoxLayout(name, BoxLayout.X_AXIS));
	        name.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
	        Label label = new Label("Terrain");
	        label.setBackground(Color.WHITE);
	        label.setFont(new Font("Arial", Font.PLAIN, 11));
	        name.add(label);
	        String[] terrainStrings = { "Beach", "Grass", "Urban"};
	        this.terrain = new JComboBox<String>(terrainStrings);
	        this.terrain.setSelectedIndex(1);
	        this.terrain.addActionListener(new ActionListener(){
	            public void actionPerformed(ActionEvent e) {
	                JComboBox<String> cb = (JComboBox<String>)e.getSource();
	                String terrainName = (String)cb.getSelectedItem();
	                switch(terrainName){
	                case "Beach": setTerrainInt(1);
	                			  break;
	                case "Grass": setTerrainInt(2);
	                			  break;
	                case "Urban": setTerrainInt(3);
	                		 	  break;
	                }
	            }});
	        name.add(this.terrain);
	        return name;
	    }
	    
	    private void setTerrainInt(int number){
	    	this.terrainInt = number;
	    }


	    private JPanel buttons(){
	        JPanel buttons = new JPanel();
	        buttons.setOpaque(false);
	        JButton save = new JButton("Save");
	        save.addActionListener(new ActionListener() {
	            @Override
	            public void actionPerformed(ActionEvent e) {
	                save();
	            }
	        });
	        buttons.add(save);
	        
	        JButton cancel = new JButton("Cancel");
	        cancel.addActionListener(new ActionListener() {
	            @Override
	            public void actionPerformed(ActionEvent e) {
	                cancel();
	            }
	        });
	        buttons.add(cancel);
	        return buttons;
	    }
	    
	    private void save(){
	            if(this.length.getText().equals("") || this.width.getText().equals("")){
	                JOptionPane.showMessageDialog(null, "Width or height can't be empty!", "Warning", JOptionPane.WARNING_MESSAGE);
	                return;
	            }else{
	            	int length = Integer.parseInt(this.length.getText());
	            	int width = Integer.parseInt(this.width.getText());
	            	this.mainFrame.setLength(length);
	            	this.mainFrame.setWidth(width);
	            	this.mainFrame.setTerrain(terrainInt);
	            	this.mainFrame.changeView(MainFrame.Views.SIMULATOR);
	                dispose();
	            }
	    }

	    private void cancel(){
	        dispose();
	    }
	    
	    protected JButton saveButton() {
	        JButton button = new JButton("Save");
	        button.addActionListener(new ActionListener() {
	            @Override
	            public void actionPerformed(ActionEvent e) {
	                save();
	            }
	        });
	        return button;
	    }
	}

