package control;

import java.awt.Graphics2D;

public class CanvasArea {
  private final int width;
  private final int height;
  private Graphics2D graphics;
  
  CanvasArea(int width, int height) {
    this.width = width;
    this.height = height;
    
  }
  
  void inject(Graphics2D graphics) {
    this.graphics = graphics;
  }
  
  private void checkGraphics() {
    if (graphics == null) {
      throw new IllegalStateException();
    }
  }
  
  /**
   * Returns the width of the drawing area.
   * @return the width of the drawing area.
   */
  public int getWidth() {
    return width;
  }
  /**
   * Returns the height of the drawing area.
   * @return the height of the drawing area.
   */
  public int getHeight() {
    return height;
  }
  
  /**
   * Clear the drawing area with the color of the brush.
   */
  public void clear(Brush brush) {
    checkGraphics();
    graphics.setColor(brush.color);
    graphics.fillRect(0, 0, width, height);
  }
  
  /**
   * Draw a line between coordinate (x1,y1) and (x2, y2).
   * 
   * @param x1 the x coordinate of the first point.
   * @param y1 the y coordinate of the first point.
   * @param x2 the x coordinate of the second point.
   * @param y2 the y coordinate of the second point.
   * @param brush the brush used to determine the color.
   */
  public void drawLine(int x1, int y1, int x2, int y2, Brush brush) {
    checkGraphics();
    graphics.setColor(brush.color);
    graphics.drawLine(x1, y1, x2, y2);
  }
  
  /**
   * Draw a rectangle using the coordinate of a upper point and a width and an height.
   * If the brush passed as parameter is {@link Brush#isOpaque()} 
   * 
   * @param x the x coordinate of the upper left of the rectangle.
   * @param y the y coordinate of the upper left of the rectangle.
   * @param width the width of the rectangle.
   * @param height the height of the rectangle.
   * @param brush if the brush is opaque, the rectangle will be filled
   *              by the color of the brush otherwise only the edges will
   *              be drawn.
   */
  public void drawRectangle(int x, int y, int width, int height, Brush brush) {
    checkGraphics();
    graphics.setColor(brush.color);
    if (brush.isOpaque()) {
      graphics.fillRect(x, y, width, height);
    } else {
      graphics.drawRect(x, y, width, height);
    }
  }
  
  /**
   * Draw an ellipse using the coordinate of a upper point and a width and an height
   * of the rectangle containing the ellipse.
   * 
   * @param x the x coordinate of the upper left of the rectangle.
   * @param y the y coordinate of the upper left of the rectangle.
   * @param width the width of the rectangle.
   * @param height the height of the rectangle.
   * @param brush if the brush is opaque, the ellipse will be filled
   *              by the color of the brush otherwise only the contour will
   *              be drawn.
   */
  public void drawEllipse(int x, int y, int width, int height, Brush brush) {
    checkGraphics();
    graphics.setColor(brush.color);
    if (brush.isOpaque()) {
      graphics.fillOval(x, y, width, height);
    } else {
      graphics.drawOval(x, y, width, height);
    }
  }
}