import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;


/****************************************************************************
* Class ElevatorGUIFrame                                                    *
* It is the graphical user panel of the IndiGolog elevator control.         *
****************************************************************************/
public class ElevatorGUIFrame extends JFrame implements ActionListener
{
    private JFrame    frame;
    private JPanel    upanel, dpanel, lpanel, rpanel;
    private JLabel[]  light = new JLabel[7];
    private JButton[] button = new JButton[7];

    private ElevatorGUI control;
    private int         lightPos;    // position of the light which is on


    /**
    * Constructor of class ElevatorGUIFrame
    */
    public ElevatorGUIFrame(String name, ElevatorGUI ctrl)
    {
        super(name);
        JLabel[] labels = new JLabel[4];
        int i;
        control = ctrl;
        lightPos = 0;


        /* labels */
        upanel = new JPanel();
        upanel.setLayout(new GridLayout(0, 2, 0, 0));
        upanel.setBorder(BorderFactory.createEmptyBorder(10,10,0,10));
        upanel.setOpaque(true);
        upanel.setBackground(Color.lightGray);
        upanel.setPreferredSize(new Dimension(180, 50));

        labels[0] = new JLabel("floor");
        labels[2] = new JLabel("buttons");
        labels[1] = new JLabel("elevator's");
        labels[3] = new JLabel("position");

        for (i = 0; i <= 3; i++)
	{
            labels[i].setBackground(Color.lightGray);
            labels[i].setPreferredSize(new Dimension(80,20));
            labels[i].setVerticalAlignment(SwingConstants.CENTER);
            labels[i].setHorizontalAlignment(SwingConstants.CENTER);
            upanel.add(labels[i]);
	}

        /* lights */
        rpanel = new JPanel();
        rpanel.setLayout(new GridLayout(6, 0, 0, 0));
        rpanel.setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createEmptyBorder(10,20,10,20),
            BorderFactory.createRaisedBevelBorder()));
        rpanel.setOpaque(true);
        rpanel.setBackground(Color.lightGray);
        rpanel.setPreferredSize(new Dimension(90, 320));

        for (i = 1; i <= 6; i++)
        {
            light[i] = new JLabel("");
            light[i].setOpaque(true);
            light[i].setBackground(Color.black);
            light[i].setPreferredSize(new Dimension(50,50));
            light[i].setVerticalAlignment(SwingConstants.CENTER);
            light[i].setHorizontalAlignment(SwingConstants.CENTER);
        }

        for (i = 6; i > 1; i--)
        {
            light[i].setText(new Integer(i).toString());
            rpanel.add(light[i]);
        }
        light[1].setText("G");
        rpanel.add(light[1]);

        /* buttons */
        lpanel = new JPanel();
        lpanel.setLayout(new GridLayout(6, 0, 0, 10));
        lpanel.setBorder(BorderFactory.createEmptyBorder(15,20,15,10));
        lpanel.setOpaque(true);
        lpanel.setBackground(Color.lightGray);
        lpanel.setPreferredSize(new Dimension(90, 320));

        for (i = 6; i > 0; i--)
        {
            String str = (new Integer(i)).toString();
            button[i] = new JButton(str);
            button[i].setVerticalTextPosition(AbstractButton.CENTER);
            button[i].setHorizontalTextPosition(AbstractButton.CENTER);
            button[i].setBackground(Color.gray);
            button[i].setBorder(BorderFactory.createRaisedBevelBorder());
            button[i].setActionCommand("b" + str);
            button[i].addActionListener(this);
            lpanel.add(button[i]);
        }

        /* connection button */
        dpanel = new JPanel();
        dpanel.setLayout(new GridLayout(0, 1, 0, 10));
        dpanel.setBorder(BorderFactory.createEmptyBorder(10,40,10,40));
        dpanel.setOpaque(true);
        dpanel.setBackground(Color.lightGray);
        dpanel.setPreferredSize(new Dimension(180, 60));

        button[0] = new JButton("Connect");
        button[0].setVerticalTextPosition(AbstractButton.CENTER);
        button[0].setHorizontalTextPosition(AbstractButton.CENTER);
        button[0].setBackground(Color.gray);
        button[0].setActionCommand("b0");
        button[0].addActionListener(this);
        dpanel.add(button[0]);

        /* set up the frame */
        getContentPane().add(upanel, BorderLayout.NORTH);
        getContentPane().add(rpanel, BorderLayout.EAST);
        getContentPane().add(lpanel, BorderLayout.WEST);
        getContentPane().add(dpanel, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }

    /**
    * It turns on the light of the indicated floor
    */
    public void moveLight(int floor)
    {
        if (lightPos > 0)
            light[lightPos].setBackground(Color.black);

        lightPos = floor;
        light[lightPos].setBackground(Color.yellow);
    }

    /**
    * It returns the floor in which its light is on
    */
    public int lightOnPosition()
    {
        return lightPos;
    }

    /**
    * It turns on the button of the indicated floor
    */
    public void turnOnButton(int floor)
    {
        button[floor].setBackground(Color.yellow);
    }

    /**
    * It turns off the button of the indicated floor
    */
    public void turnOffButton(int floor)
    {
        button[floor].setBackground(Color.gray);
    }

    /**
    * It changes the label of the indicated button to text
    */
    public void setButtonText(int floor, String text)
    {
        button[floor].setText(text);
    }

    /**
    * It is the button event handler
    */
    public void actionPerformed(ActionEvent e)
    {
        control.handleButtonEvent(e.getActionCommand());
    }
}
