import java.util.ArrayList;

import edu.southern.computing.oopj.ContextMenu;
import edu.southern.computing.oopj.GraphicalIO;
import edu.southern.computing.oopj.Viewport;


public class LockAndKeyViewport extends Viewport {

    //  Last x position of the mouse cursor
    private int lastX;
    //  Last y position of the mouse cursor
    private int lastY;
    
    //  List of locks that have been added to the viewport
    private ArrayList<LockView> lockList;
    
    public LockAndKeyViewport(String title, int x, int y, int width, int height) {
        super(title, x, y, width, height);
        
        setBackground(WHITE);
        
        lockList = new ArrayList<LockView>();
        
        setContextMenu(new ContextMenu("New Lock", "New Key", "Quit") {
            @Override
            public void handler(String item) {
                if (item.equals("New Lock")) {
                    int code = GraphicalIO.nextInt("Enter Lock code");
                    if (code != 0) {
                        LockView lock = new LockView(code, lastX, lastY);
                        add(lock);
                        lockList.add(lock);
                    }
                } else if (item.equals("New Key")) {
                    int code = GraphicalIO.nextInt("Enter Key code");
                    if (code != 0) {
                        add(new KeyView(code, lastX, lastY, lockList));
                    }
                } else if (item.equals("Quit")) {
                    System.exit(0);
                }
            }
        });
    }
    
    public void mouseMoved() {
        //  Remember when to possibly make a new lock 
        lastX = getMouseX();
        lastY = getMouseY();
    }
    
    public static void main(String[] args) {
        new LockAndKeyViewport("Lock and Key", 100, 100, 800, 600);
    }

}
