package gui.simulator;

import javax.imageio.ImageIO;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;

/**
 * Created by gjoosen on 07/03/15.
 * Edited by master D Mathijssen on 17-3-2015 and some other days
 */
public abstract class DrawEngine {

    private Images.ImageType image;
    private double x, y, rotation, scale;
    private double distanceToOtherObjects;
    public SimulatorPane.Objects type;

    public DrawEngine(Images.ImageType image, int x, int y, double scale, double distanceToOtherObjects, SimulatorPane.Objects objecttype){
        this.image = image;
        this.type = objecttype;
        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), Images.getImage(image).getWidth(null) /2 , Images.getImage(image).getHeight(null) / 2);
        return transform;
    }

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

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

    public Point2D.Double getCenter(){
        Point2D.Double center = new Point2D.Double(x + Images.getImage(image).getWidth(null)*scale / 2, y + Images.getImage(image).getHeight(null)*scale/2);
        return center;
    }

    //getters and setters down here
    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 Images.getImage(image).getWidth(null);
    }

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

    public double getDistanceToOtherObjects() {
        return distanceToOtherObjects;
    }

    public Rectangle2D.Double getRect(){
        return new Rectangle2D.Double(-distanceToOtherObjects, - distanceToOtherObjects, Images.getImage(image).getWidth(null)  + 2* distanceToOtherObjects, Images.getImage(image).getHeight(null) + 2*distanceToOtherObjects);
    }


}
