package gui.simulator;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.IOException;

/**
 * Created by gjoosen on 07/03/15.
 */
public abstract class Draw {

    private Image image;
    private double x, y, rotation, scale;
    private double distanceToOtherObjects;

    public Draw(String image, int x, int y, double scale, double distanceToOtherObjects){
        try{
            //System.out.println(getClass().getResource(image));
            this.image = ImageIO.read(getClass().getResource(image));
        }catch(IOException ex){
            ex.printStackTrace();
        }
        this.x = x;
        this.y = y;
        this.scale = scale;
        this.distanceToOtherObjects = distanceToOtherObjects;
    }

    public AffineTransform getAffineTransform(){
        AffineTransform transform = new AffineTransform();
        transform.translate(this.x, this.y);
        transform.scale(this.scale, this.scale);
        transform.rotate(Math.toRadians(this.rotation), this.image.getWidth(null) /2 , this.image.getHeight(null) / 2);
        return transform;
    }

    public void draw(Graphics2D g){
        g.drawImage(this.image, this.getAffineTransform(), null);
    }

    public boolean contains(Point point){
        Shape shape = new Rectangle2D.Double(0, 0, image.getWidth(null), image.getHeight(null));
        return this.getAffineTransform().createTransformedShape(shape).contains(point);
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public void setX(double x){
        this.x = x;
    }

    public void setY(double y){
        this.y = y;
    }

    public void setRotation(double rotation) {
        this.rotation = rotation;
    }

    public double getScale() {
        return scale;
    }

    public void setScale(double scale) {
        this.scale = scale;
    }

    public int getWidth(){
        return this.image.getWidth(null);
    }

    public int getHeight(){
        return this.image.getHeight(null);
    }
    public double getRotation() {
        return rotation;
    }

    public double getDistanceToOtherObjects() {
        return distanceToOtherObjects;
    }
}
