import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;
/*
 * NewJFrame.java
 *
 * Created on 18 May 2009, 19:03
 */


/**
 *
 * @author  arvad04
 */
public class GUI extends javax.swing.JFrame {
    
    final static boolean shouldFill = true;
    final static boolean shouldWeightX = true;
    final static boolean RIGHT_TO_LEFT = false;

    private static JFrame mainFrame;
    private static JFrame loginFrame;
    private static Container pane;
    
    //MAIN
    private static JScrollPane jScrollPane;
    private static JTextArea outputTextArea;
    private static JTextField inputTextField;
    private static JButton commitInputButton;
    private static JTextArea infoTextArea;
    
    //LOGIN
    private static JLabel usernameLabel;
    private static JTextField usernameTextField;
    private static JLabel passwordLabel;
    private static JPasswordField passwordPasswordField;
    private static JButton loginOkButton;
    private static JButton loginCancelButton;
    // End of variables declaration
    
    // Lists containing components for each screen
    private static List<Component> mainScreenComponents = new ArrayList<Component>();
    private static List<Component> loginScreenComponents = new ArrayList<Component>();
    
    /** Creates new form NewJFrame */
    public GUI() {
        initComponents();
        showMainScreen();
    }
    
    private void showMainScreen()
    {
    	mainFrame.getRootPane().setDefaultButton(commitInputButton);
    	mainFrame.setVisible(true);
    	inputTextField.requestFocusInWindow();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents()
    {
    	//Create and set up the main window.
        mainFrame = new JFrame("Hotel Reservation System");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setLocation(200, 150);
        
        //Size the window.
        Insets insets = mainFrame.getInsets();
        Dimension mainFrameDimension = new Dimension(800 + insets.left + insets.right,600 + insets.top + insets.bottom);
        mainFrame.setSize(mainFrameDimension.width, mainFrameDimension.height);

        
        //Set up the content pane.
        pane = mainFrame.getContentPane();
        pane.setLayout(null);

        // Define elements
        inputTextField = new JTextField();
        mainScreenComponents.add(inputTextField);
        outputTextArea = new JTextArea();
        mainScreenComponents.add(outputTextArea);
        jScrollPane = new JScrollPane(outputTextArea);
        mainScreenComponents.add(jScrollPane);
        commitInputButton = new JButton("Send command");
        mainScreenComponents.add(commitInputButton);
        infoTextArea = new JTextArea();
        mainScreenComponents.add(infoTextArea);
        
        //Set dimensions etc.
        commitInputButton.setPreferredSize(new Dimension(150,30));
        outputTextArea.setColumns(50);
        outputTextArea.setRows(25);
        infoTextArea.setBackground(new java.awt.Color(200, 200, 200));
        infoTextArea.setForeground(new java.awt.Color(255, 0, 0));
        //infoTextArea.setDisabledTextColor(new java.awt.Color(255, 0, 0));  
        infoTextArea.setEditable(false);
        //infoTextArea.setFont(new java.awt.Font("AlArabiya", 1, 48));
        infoTextArea.setText("DISABLED");
        
              
        
        infoTextArea.setFont(new java.awt.Font("DejaVu Sans", 0, 24));
        outputTextArea.setEditable(false);
        //jScrollPane.setViewportView(outputTextArea);
        
        //Add listeners
        commitInputButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                commitInputButtonActionPerformed();
            }
        });
        
        /*inputTextField.addKeyListener(new KeyAdapter() {
           public void keyPressed(KeyEvent e) {
        	   inputTextFieldKeyPressed(e.getKeyCode());
             
             }
           }
        );*/

        

     
        
        insets = pane.getInsets();
        /*System.out.println("INSETS: " + insets.left);
        System.out.println("INSETS: " + insets.right);
        System.out.println("INSETS: " + insets.top);
        System.out.println("INSETS: " + insets.bottom);*/
        Dimension sizeTextArea = outputTextArea.getPreferredSize();
        Dimension sizeButton = commitInputButton.getPreferredSize();
        
        Dimension sizeTextField = new Dimension(sizeTextArea.width - sizeButton.width - 10, sizeButton.height);
        inputTextField.setPreferredSize(sizeTextField);

        Dimension sizeInfoLabel = new Dimension(sizeTextArea.width, 100);
        infoTextArea.setPreferredSize(sizeInfoLabel);
        
        
        
        
        
        
        // Place the components
        outputTextArea.setBounds(25 + insets.left, 25 + insets.top,
        		sizeTextArea.width, sizeTextArea.height);
        
        /*jScrollPane.setBounds(25 + insets.left, 25 + insets.top,
        		sizeTextArea.width, sizeTextArea.height);*/
                
        inputTextField.setBounds(25 + insets.left, 25 + insets.top + sizeTextArea.height + 10,
        		sizeTextField.width, sizeTextField.height);
                
        commitInputButton.setBounds(25 + insets.left + sizeTextField.width + 10, 25 + insets.top + sizeTextArea.height + 10,
                     sizeButton.width, sizeButton.height);

        infoTextArea.setBounds(25 + insets.left, 25 + insets.top + sizeTextArea.height + 10 + sizeTextField.height + 10, sizeInfoLabel.width, sizeInfoLabel.height);


 
        /*        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        inputTextField = new javax.swing.JTextField();
        jScrollPane = new javax.swing.JScrollPane();
        outputTextArea = new javax.swing.JTextArea();
        commitInputButton = new JButton("Send command");
        
        commitInputButton.setPreferredSize(new Dimension(150,30));
        outputTextArea.setColumns(50);
        outputTextArea.setRows(30);
        jScrollPane.setViewportView(outputTextArea);
        
        
		pane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		if (shouldFill) {
			//natural height, maximum width
			c.fill = GridBagConstraints.HORIZONTAL;
			//c.fill = GridBagConstraints.NONE;
		}
	
		
		if (shouldWeightX) {
		c.weightx = 0.5;
		}
		c.insets = new Insets(20,10,20,10);
		//c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 1;
		c.gridy = 1;
		pane.add(outputTextArea, c);
	
		//button = new JButton("Button 2");
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 0.5;
		c.gridx = 1;
		c.gridy = 2;
		pane.add(inputTextField, c);
	
		//button = new JButton("Button 3");
		c.fill = GridBagConstraints.NONE;
		c.weightx = 0.0;
		c.gridx = 2;
		c.gridy = 2;
		pane.add(commitInputButton, c);
	
		//button = new JButton("Long-Named Button 4");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 40;      //make this component tall
		c.weightx = 0.0;
		c.gridwidth = 3;
		c.gridx = 0;
		c.gridy = 1;
		//pane.add(button, c);
	
		//button = new JButton("5");
		c.fill = GridBagConstraints.HORIZONTAL;
		c.ipady = 0;       //reset to default
		c.weighty = 1.0;   //request any extra vertical space
		c.anchor = GridBagConstraints.PAGE_END; //bottom of space
		c.insets = new Insets(10,0,0,0);  //top padding
		c.gridx = 1;       //aligned with button 2
		c.gridwidth = 2;   //2 columns wide
		c.gridy = 2;       //third row
		//pane.add(button, c);
*/

        // Add components to main screen content pane
        pane.add(inputTextField);
        pane.add(outputTextArea);
        pane.add(commitInputButton);
        //pane.add(jScrollPane);
        pane.add(infoTextArea);
        //outputTextArea.add(jScrollPane);

        //pane.remove(inputTextField);
        
        //Display the window.
        //frame.pack();
        
        //Display the window.
      
        /*
         * Setup loginFrame
         */
        //Create and set up the login window.
        loginFrame = new JFrame("LOGIN TITLE OF WINDOW");
        loginFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        loginFrame.setLocation(350, 250);
        
        //Size the login window
        insets = loginFrame.getInsets();
        Dimension loginFrameDimension = new Dimension(290 + insets.left + insets.right,185 + insets.top + insets.bottom);
        loginFrame.setSize(loginFrameDimension.width, loginFrameDimension.height);

        //Create components for login screen
        usernameLabel = new JLabel("Username:");
        loginScreenComponents.add(usernameLabel);
        usernameTextField = new JTextField();
        loginScreenComponents.add(usernameTextField);
        passwordLabel = new JLabel("Password:");
        loginScreenComponents.add(passwordLabel);
        passwordPasswordField = new JPasswordField();
        loginScreenComponents.add(passwordPasswordField);
        loginOkButton = new JButton("Login");
        loginScreenComponents.add(loginOkButton);
        loginCancelButton = new JButton("Cancel");
        loginScreenComponents.add(loginCancelButton);
        

        //Set dimensions etc.
        Dimension sizeUsernameTextField = new Dimension(150,25);
        usernameTextField.setPreferredSize(sizeUsernameTextField);
        
        Dimension sizeUsernameLabel = new Dimension(80,25);
        usernameLabel.setPreferredSize(sizeUsernameLabel);
        
        Dimension sizePasswordPasswordField = new Dimension(150,25);
        passwordPasswordField.setPreferredSize(sizePasswordPasswordField);
        
        Dimension sizePasswordLabel = new Dimension(80,25);
        passwordLabel.setPreferredSize(sizePasswordLabel);
        
        Dimension sizeLoginOkButton = new Dimension(80, 30);
        loginOkButton.setPreferredSize(sizeLoginOkButton);
        //loginOkButton.setSelected(true);
        loginOkButton.setDefaultCapable(true);
    
        
        Dimension sizeLoginCancelButton = new Dimension(80, 30);
        loginCancelButton.setPreferredSize(sizeLoginCancelButton);
        
        //Login - set bound, place components
        usernameLabel.setBounds(25, 25, sizeUsernameLabel.width, sizeUsernameLabel.height);
        
        usernameTextField.setBounds(100, 25, sizeUsernameTextField.width, sizeUsernameTextField.height);
        
        passwordLabel.setBounds(25, 50, sizeUsernameLabel.width, sizeUsernameLabel.height);
        
        passwordPasswordField.setBounds(100, 50, sizePasswordPasswordField.width, sizePasswordPasswordField.height);
        
        loginCancelButton.setBounds(50, 100, sizeLoginCancelButton.width, sizeLoginCancelButton.height);

        loginOkButton.setBounds(150, 100, sizeLoginOkButton.width, sizeLoginOkButton.height);

        
        // Add listeners
        loginCancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	loginFrame.setVisible(false);
            	clearLoginInput();
            }
        });
        
        loginOkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginOkButtonActionPerformed();
                loginFrame.setVisible(false);
                clearLoginInput();
            }
        });
        
        passwordPasswordField.addKeyListener(new KeyAdapter() {
           public void keyPressed(KeyEvent e) {
        	   passwordPasswordFieldKeyPressed(e.getKeyCode());
             }
           }
        );
        
        //Set up the content pane.
        pane = loginFrame.getContentPane();
        pane.setLayout(null);

                
        for(Component component : loginScreenComponents) {
    		pane.add(component);
    	}
        
        // Start executing current menu
        MenuSystem menuSystem = new MenuSystem();
        MenuSystem.getCurrentMenu().executeMenu();
        //Set curser in inputfield
        //inputTextField.setCursor(new Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        //inputTextField.setCaretPosition(0);
        
        
        /*jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextField1.setText("jTextField1");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(84, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(86, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(53, 53, 53)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47))
        );

        pack();*/
    }// </editor-fold>
    
    public static void setInfoMessage(String message)
    {
    	infoTextArea.setForeground(new java.awt.Color(0, 0, 0));
    	infoTextArea.setText(message);
    }
    
    public static void setErrorMessage(String message)
    {
    	infoTextArea.setForeground(new java.awt.Color(255, 0, 0));	
    	infoTextArea.setText(message);
    }
    
    public static void setOutputTextArea(String output)
    {
    	outputTextArea.setText(output);
    }
    
    
    //public static void getInputTextField()
    
    //Implement listeners
    private void commitInputButtonActionPerformed() 
    {
        //setErrorMessage("Button pressed");
        
          //pane.add(inputTextField);
        MenuSystem.getCurrentMenu().processInput(inputTextField.getText());
        inputTextField.setText("");
        //inputTextField.setCursor(new Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        inputTextField.requestFocusInWindow();
    }
    
    private void clearLoginInput()
    {
    	usernameTextField.setText("");
		passwordPasswordField.setText("");
		usernameTextField.requestFocusInWindow();
    }
    
	private void inputTextFieldKeyPressed(int key) 
	{
        if (key == KeyEvent.VK_ENTER) {
           //Toolkit.getDefaultToolkit().beep();   
           System.out.println("ENTER pressed");
           commitInputButtonActionPerformed();
        }
	}
	
	private void passwordPasswordFieldKeyPressed(int key) 
	{
        if (key == KeyEvent.VK_ENTER) {
           //Toolkit.getDefaultToolkit().beep();   
           System.out.println("ENTER pressed");
           loginOkButtonActionPerformed();
        }
	}
	
	private void loginOkButtonActionPerformed()
	{
		String password = new String(passwordPasswordField.getPassword());
		if(HandleAuthorization.loginUser(usernameTextField.getText(), password)) {
			GUI.setInfoMessage("Login Succeeded!");
	    }
	    else {
	    	GUI.setErrorMessage("Login Failed!");
	    }
	}
    /**
     * @param args the command line arguments
     */
    /*public static void main(String args[]) {
        
    }*/
    public static void showLoginScreen()
    {
    	/*pane.removeAll();
    	
    	for(Component component : loginScreenComponents) {
    		pane.add(component);
    	}

        pane.repaint();
        usernameTextField.requestFocusInWindow();*/
        /*pane.add(usernameTextField);
        pane.add(usernameLabel);
        pane.add(passwordLabel);
        pane.add(passwordPasswordField);*/


        loginFrame.getRootPane().setDefaultButton(loginOkButton);
        loginFrame.setVisible(true);
        //usernameTextField.requestFocusInWindow();
    }

    public static void showPopUpMessage(String message, String title){
    	JOptionPane.showMessageDialog(null, message, title, JOptionPane.INFORMATION_MESSAGE);
    }
}
