package vermilion;

import javax.swing.*;
import java.awt.Color;
import java.awt.event.*;
import relative.*;

/**
 * An extension of the JLabel class that represents an audio plugin
 * on the screen in the mixer view. This can be dragged around and
 * has a context menu allowing the machine to be deleted.
 */
public class JWirePanelMachineBox extends JLabel
{
    /**
     * The plugin that this component is representing.
     */
    public MachineBase machine;

    private int grabX = 0;
    private int grabY = 0;
    private JWirePanel panel = null;
    private JMenuItem delItem;

    /**
     * Constructs a new JMachineBox component using the machine provided.
     * @param m A MachineBase plugin that we want on the screen.
     */
    public JWirePanelMachineBox(MachineBase m, JWirePanel p)
    {
        super(m.machineID);
        machine = m;
        panel = p;

        setHorizontalAlignment(CENTER);
        setBorder(BorderFactory.createLineBorder(Color.BLACK));
        setOpaque(true);

        this.addMouseListener(new MouseListener() {
            public void mouseExited(MouseEvent e){}
            public void mouseEntered(MouseEvent e){}
            public void mouseReleased(MouseEvent e){ panel.unsetNewWire(); }
            public void mousePressed(MouseEvent e){ grabX = e.getX(); grabY = e.getY(); }
            public void mouseClicked(MouseEvent e){ if (e.getButton() == e.BUTTON1 && e.getClickCount() == 2) showMachineWindow(); }
        } );

        this.addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent me)
            {
                JWirePanelMachineBox mb = (JWirePanelMachineBox) me.getComponent();

                if (me.isControlDown())
                {
                    panel.setNewWire(mb, me.getXOnScreen(), me.getYOnScreen());
                }
                else
                {
                    panel.remove(mb);

                    int posX = me.getXOnScreen() - panel.getLocationOnScreen().x - grabX;
                    int posY = me.getYOnScreen() - panel.getLocationOnScreen().y - grabY;

                    int totalX = panel.getWidth();
                    int totalY = panel.getHeight();

                    int percentX = posX * 1000 / totalX;
                    int percentY = posY * 1000 / totalY;

                    if (percentX < 0) percentX = 0;
                    if (percentY < 0) percentY = 0;

                    if (percentX > 1000-JWirePanel.MACHINE_WIDTH) percentX = 1000-JWirePanel.MACHINE_WIDTH;
                    if (percentY > 1000-JWirePanel.MACHINE_HEIGHT) percentY = 1000-JWirePanel.MACHINE_HEIGHT;

                    mb.machine.xLoc = percentX;
                    mb.machine.yLoc = percentY;

                    panel.add(mb, new RelativePlacement(percentX, percentY, JWirePanel.MACHINE_WIDTH, JWirePanel.MACHINE_HEIGHT));

                    panel.revalidate();
                    panel.repaint();
                }
            }
            public void mouseMoved(MouseEvent me) { }
        } );

        JPopupMenu menu = new JPopupMenu();
        delItem = new JMenuItem("Delete");
        
        delItem.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                panel.removeMachine(machine);
            }
        }
        );
        
        menu.add(delItem);
        this.setComponentPopupMenu(menu);
    }

    /**
     * Opens a dialog showing parameters for the machine being represented.
     */
    public void showMachineWindow()
    {
        JParameterFrame f = new JParameterFrame(machine);
        f.setVisible(true);
    }
}
