package gui.simulator.facilities;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;

import agenda.Act;
import agenda.Stage;
import gui.simulator.DrawEngine;
import gui.simulator.Images;
import gui.simulator.SimulatorPane;
import gui.simulator.Terrain;

public class SimulatorStage extends DrawEngine implements Serializable {

	private static Images.ImageType images = Images.ImageType.Tent;
	private static String facilityName = "Stage";
    private Stage stage;
    private Act playingact;
    private int movement1,movement2;
    private long lastTime;
	private Rectangle2D.Double walkArea = new Rectangle2D.Double(20, 250, 350, 200);

	//walk area
	private static final int WALKAREAHEIGHT = 200;

    public SimulatorStage(int x, int y, Terrain terrain, boolean drawWalkArea) {
        super(images, x, y, 1, 0, SimulatorPane.Objects.STAGE, terrain, new Rectangle2D.Double(20, 250, 350, WALKAREAHEIGHT), drawWalkArea);
        changeLightbeam();
    }

    public SimulatorStage(int x, int y, double scale, double distance, Terrain terrain, boolean drawWalkArea) {
        super(images, x, y, scale, distance, SimulatorPane.Objects.STAGE, terrain, new Rectangle2D.Double(20, 250, 350, WALKAREAHEIGHT), drawWalkArea);
        changeLightbeam();
    }

	public SimulatorStage(int x, int y, double scale, double distance, Terrain terrain) {
		super(images, x, y, scale, distance, SimulatorPane.Objects.STAGE, terrain, new Rectangle2D.Double(20, 250, 350, WALKAREAHEIGHT), false);
		changeLightbeam();
	}


	public SimulatorStage(int x, int y, Terrain terrain) {
		super(images, x, y, 1, 0,SimulatorPane.Objects.STAGE, terrain, new Rectangle2D.Double(20, 250, 350, WALKAREAHEIGHT), false);
		changeLightbeam();
	}
    
    public String getFacilityName(){
    	return facilityName;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

    public Stage getStage() {
        return stage;
    }
    
    public Act getPlayingact() {
		return playingact;
	}

	public void setPlayingact(Act playingact) {
		this.playingact = playingact;
	}

	public void draw(Graphics2D g){
    	if(playingact != null){
        	if(System.currentTimeMillis() - lastTime > 1000) 
        		changeLightbeam();
	    	GeneralPath lightbeam = new GeneralPath();
	    	int xobject = getWidth()/2;
	    	int yobject = getHeight()-20;
	    	lightbeam.moveTo(xobject-90, yobject);
	    	lightbeam.lineTo(xobject-90-60+movement1, yobject+160);
	    	lightbeam.lineTo(xobject-90+60+movement1, yobject+160);
	    	lightbeam.lineTo(xobject-90, yobject);
	    	lightbeam.moveTo(xobject+90, yobject);
	    	lightbeam.lineTo(xobject+90-60+movement2, yobject+160);
	    	lightbeam.lineTo(xobject+90+60+movement2, yobject+160);
	    	lightbeam.lineTo(xobject+90, yobject);
	    	lightbeam.closePath();   	
	    	g.setColor(new Color(255,0,0,60));
	    	g.fill(this.getAffineTransform().createTransformedShape(lightbeam));
    	}
    	super.draw(g);

    }

    public void changeLightbeam(){
    	lastTime = System.currentTimeMillis();
    	movement1 = (int) (Math.random()*70)-35;
    	movement2 = (int) (Math.random()*70)-35;
    }
}
