import java.util.ArrayList;

import edu.southern.computing.oopj.GraphicalObject;

public class KeyView extends GraphicalObject {

    //  Contains the logic for interacting with locks
    private KeyModel key;
    
    private static final int WIDTH = 70;
    private static final int HEIGHT = 30;
    
    //  Current list of locks
    private ArrayList<LockView> lockList;
    
    public KeyView(int code, int x, int y, ArrayList<LockView> locks) {
        super(WIDTH, HEIGHT);
        key = new KeyModel(code);
        lockList = locks;
        setLocation(x, y);
        setBackground(TRANSPARENT);
        setMovable(true);
    }
    
    public void mouseReleased() {
        int x = getMouseX() + getX(), y = getMouseY() + getY();
        for (LockView lock : lockList) {
            //  At we over of lock object?
            if (lock.getBounds().contains(x, y)) {
                lock.tryLock(key);  //  See if we can open the lock
            }
        }
    }
    
    public void draw() {
        //  Draw the round part of the key
        fillOval(0, 0, HEIGHT - 1, HEIGHT - 1, BLACK);
        //  Draw the little hole for a key ring
        fillOval(4, HEIGHT/2 - 2, 4, 4, WHITE);
        //  Draw the shaft of the key
        fillRectangle(HEIGHT - 1, HEIGHT/3, WIDTH - HEIGHT - 10, HEIGHT/3 - 1, BLACK);
        //  Draw the key's teeth
        int x = HEIGHT - 1,
            h = 2*HEIGHT/3 - 1;
        //  First tooth
        fillRectangle(x, h, 5, 2, BLACK);
        fillRectangle(x += 1, h, 1, 1, BLACK);
        fillRectangle(x += 1, h, 1, 2, BLACK);
        fillRectangle(x += 1, h, 1, 3, BLACK);
        fillRectangle(x += 1, h, 1, 4, BLACK);
        fillRectangle(x += 1, h, 1, 5, BLACK);
        fillRectangle(x += 1, h, 1, 6, BLACK);
        fillRectangle(x += 1, h, 1, 5, BLACK);
        fillRectangle(x += 1, h, 1, 4, BLACK);
        fillRectangle(x += 1, h, 1, 3, BLACK);
        fillRectangle(x += 1, h, 1, 2, BLACK);
        fillRectangle(x += 1, h, 1, 1, BLACK);
        //  Second tooth
        fillRectangle(x += 1, h, 1, 1, BLACK);
        fillRectangle(x += 1, h, 1, 2, BLACK);
        fillRectangle(x += 1, h, 1, 3, BLACK);
        fillRectangle(x += 1, h, 1, 4, BLACK);
        fillRectangle(x += 1, h, 1, 5, BLACK);
        fillRectangle(x += 1, h, 1, 6, BLACK);
        fillRectangle(x += 1, h, 1, 5, BLACK);
        fillRectangle(x += 1, h, 1, 4, BLACK);
        fillRectangle(x += 1, h, 1, 3, BLACK);
        fillRectangle(x += 1, h, 1, 2, BLACK);
        fillRectangle(x += 1, h, 1, 1, BLACK);
        //  Third tooth
        fillRectangle(x += 1, h, 1, 1, BLACK);
        fillRectangle(x += 1, h, 1, 2, BLACK);
        fillRectangle(x += 1, h, 1, 3, BLACK);
        fillRectangle(x += 1, h, 1, 4, BLACK);
        fillRectangle(x += 1, h, 1, 5, BLACK);
        fillRectangle(x += 1, h, 1, 6, BLACK);
        fillRectangle(x += 1, h, 1, 5, BLACK);
        fillRectangle(x += 1, h, 1, 4, BLACK);
        fillRectangle(x += 1, h, 1, 3, BLACK);
        fillRectangle(x += 1, h, 1, 2, BLACK);
        fillRectangle(x += 1, h, 1, 1, BLACK);
        
        //  Draw the rounded end of the shaft
        fillOval(x - 6, HEIGHT/3, 10, HEIGHT/3 - 1, BLACK);
        //  Draw the gray accent line on the shaft
        drawLine(HEIGHT - 1, h - 5, WIDTH - 10, h - 5, GRAY);
    }
}
