package shape;

import javafx.geometry.Point2D;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;

public class Rectangle implements Shape{

    private double x,y,width, height;
    private boolean isFinished;
    public Color fillColor;

    public Rectangle(double x, double y, double width, double height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.isFinished = true;
    }

    @Override
    public void paint(GraphicsContext graphicsContext) {
        graphicsContext.setStroke(Color.BLACK);
        graphicsContext.setLineWidth(2);
        if(isFinished){
            graphicsContext.setFill(Color.RED.deriveColor(0,1,1,0.5));
            graphicsContext.fillRect(x, y, width, height);
        }
        graphicsContext.strokeRect(x, y, width, height);

    }

    @Override
    public boolean contains(double x, double y) {
        return x>= this.x && x<= this.x + this.width
                && y>= this.y && y<= this.y + this.height;
    }

    @Override
    public void translate(double dx, double dy) {
        x += dx;
        y += dy;
    }

    @Override
    public boolean isFinished() {
        return isFinished;
    }


    public void updateSize(double newX, double newY) {
        this.width = Math.abs(newX - this.x);
        this.height = Math.abs(newY - this.y);
        if (newX < this.x && newY < this.y) {
            this.x = newX;
            this.y = newY;
        }
    }

    public Color getFillColor() {
        return fillColor;
    }

    public void setFinished(boolean finished) {
        isFinished = finished;
    }

    public void setFillColor(Color fillColor) {
        this.fillColor = fillColor;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public void setDimensions(double x, double y, double width, double height) {
        this.x = x;
        this.y = y;
        this.width = Math.abs(width);
        this.height = Math.abs(height);
        if(width < 0 ){ this.x += width;}
        if(height < 0){ this.y += height;}
    }
}