import objectdraw.*;
import java.awt.*;

// A class that defines a graphical type that looks a bit
// like a t-shirt
public class FancyTShirt {

    private static final double SIZE = 120;

    private static final double SLEEVE_WIDTH = SIZE;
    private static final double SLEEVE_HEIGHT = 0.2 * SIZE;

    private static final double BODY_WIDTH = 0.6 * SIZE;
    private static final double BODY_HEIGHT = (0.65) * SIZE;

    private static final double BODY_INSET = 0.2 * SIZE;

    private static final double NECK_WIDTH = 0.3 * SIZE;
    private static final double NECK_HEIGHT = 0.06 * SIZE;

    private static final double NECK_INSET = 0.35 * SIZE;

    private static final double BALL_SIZE = BODY_WIDTH * 0.6;
    
    private double startX, startY; // the initial location of the shirt

    // Rectangles that form a border around the shirt
    private FramedRect sleeveTrim, bodyTrim; 
    private FramedOval neckTrim; 
    
    // Rectangles that form the interior color of the shirt
    private FilledRect body, sleeves; 
    private FilledOval neck; 

    // the logo on the shirt
    private BBall logo;
    
    // whether the logo is showing or not
    private boolean logoShowing = false;
    
    // create a new T-shirt with its upper left corner at (x,y) and with
    // a width of size.
    public FancyTShirt(double x, double y, DrawingCanvas canvas) {
                // create boundary rectangles
        sleeveTrim = new FramedRect(x, 
                                    y + NECK_HEIGHT / 2,
                                    SLEEVE_WIDTH, 
                                    SLEEVE_HEIGHT, 
                                    canvas);
        bodyTrim = new FramedRect(x + BODY_INSET,
                                  y + NECK_HEIGHT / 2, 
                                  BODY_WIDTH,
                                    BODY_HEIGHT, 
                                    canvas);

        // create interior rectangles
        sleeves = new FilledRect(x + 1,
                                 y + NECK_HEIGHT / 2 + 1,
                                 SLEEVE_WIDTH - 1, 
                                 SLEEVE_HEIGHT - 1, 
                                 canvas);
        body = new FilledRect(x + BODY_INSET + 1,
                              y + NECK_HEIGHT / 2 + 1, 
                              BODY_WIDTH - 1,
                              BODY_HEIGHT - 1, 
                              canvas);

        // give it a neck hole
        neck = 
            new FilledOval(x + NECK_INSET, y, NECK_WIDTH, NECK_HEIGHT, canvas);
        neckTrim = 
            new FramedOval(x + NECK_INSET, y, NECK_WIDTH, NECK_HEIGHT, canvas);

        // set the interior to white
        body.setColor(Color.white);
        neck.setColor(Color.white);
        sleeves.setColor(Color.white);

        // create the logo.  It starts hidden.
        logo = new BBall(body.getX() + body.getWidth() / 2 - BALL_SIZE / 2,
                         body.getY() + body.getHeight() / 2 - BALL_SIZE / 2,
                         BALL_SIZE,
                         canvas);
        logo.hide();        
        
        // remember the starting location for re-set
        startX = x;
        startY = y;        
    }

    // Move t-shirt back to starting position
    public void reset() {
        this.moveTo(startX, startY);
    }
    
    // move the t-shirt by specified offsets.
    public void move(double dx, double dy) {
        body.move(dx, dy);
        neck.move(dx, dy);
        sleeves.move(dx, dy);
        bodyTrim.move(dx, dy);
        sleeveTrim.move(dx, dy);
        neckTrim.move(dx, dy);
        logo.move(dx, dy);
    }

    // returns true if the t-shirt contains the point;
    // false otherwise
    public boolean contains(Location pt) {
        return body.contains(pt) || sleeves.contains(pt)
               || neck.contains(pt);
    }

    // change color of t-shirt to newColor
    public void setColor(Color newColor) {
        body.setColor(newColor);
        neck.setColor(newColor);
        sleeves.setColor(newColor);
    }

    // move the t-shirt to a new upper-left coordinate position
    public void moveTo(double x, double y) {
        double dx = x - sleeves.getX();
        double dy = y - neck.getY();
        
        this.move(dx, dy);
    }

    // Put t-shirt in front of other objects on canvas
    public void sendToFront() {
        bodyTrim.sendToFront();
        sleeveTrim.sendToFront();
        body.sendToFront();
        neck.sendToFront();
        sleeves.sendToFront();
        neckTrim.sendToFront();
        logo.sendToFront();
    }

    public void toggleLogo() {
        if (logoShowing) {
            logoShowing = false;
            logo.hide();
        } else {
            logoShowing = true;
            logo.show();
        }
    }
}
