package gui.simulator;

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.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class VisitorsDialogPanel extends JDialog{

	    private JTextField visitorstextfield;
	    private Terrain terrain;
	    
	    public VisitorsDialogPanel(Terrain terrain){
	    	this.terrain = terrain;
	        this.getContentPane().setBackground( Color.WHITE );
	        JPanel main = new JPanel();
	        main.setOpaque(false);
	        main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
	        Label label = new Label("Choose max visitors");
	        label.setBackground(Color.WHITE);
	        label.setFont(new Font("Arial", Font.PLAIN, 14));
	        main.add(label);
	        main.add(this.lengthPanel());
	        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)-100;
	        int y = ((screenSize.height - this.getHeight()) / 2)-100;
	        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("Visitors");
	        label.setBackground(Color.WHITE);
	        label.setFont(new Font("Arial", Font.PLAIN, 11));
	        name.add(label);
	        this.visitorstextfield = new JTextField();
	        name.add(this.visitorstextfield);
	        return name;
	    }
	  

	    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.visitorstextfield.getText().equals("")){
					JOptionPane.showMessageDialog(null, "Visitors can't be empty!", "Warning", JOptionPane.WARNING_MESSAGE);
	                return;
	            }else{
	            	int visitors = Integer.parseInt(this.visitorstextfield.getText());
					if(visitors <0){
						JOptionPane.showMessageDialog(null, "Visitors can't be less than 0!", "Warning", JOptionPane.WARNING_MESSAGE);
					}
	            	terrain.setMaxVisitors(visitors);
	                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;
	    }
	}

