package automatvgi.components;

import java.awt.Graphics;

import javax.swing.JComponent;

import automatvgi.Direction;
import automatvgi.LatexColor;
import automatvgi.Projection;
import automatvgi.drawing.DrawLoop;
import automatvgi.edit.DirectionChooser;
import automatvgi.edit.Edit;
import automatvgi.edit.LabelChooserChooser;
import automatvgi.edit.LabelColorChooser;
import automatvgi.edit.LineColorChooser;
import automatvgi.tools.Point;
import automatvgi.tools.Vector;


public class LoopComponent extends TransitionComponent{
	static DrawLoop globalDraw=new DrawLoop();
	DrawLoop specificDraw=null;
	
	private Direction direction=Direction.NORTH;

	public LoopComponent(StateComponent state){
		this.state=state;
		kind=Kind.Loop;
	}

	public LoopComponent(StateComponent state, String l){
		this.state=state;
		kind=Kind.Loop;
		label=l;
	}
	
	public  DrawLoop getDraw(){
		DrawLoop dt=specificDraw;
		if (dt==null) return globalDraw;
		return dt;
	}

	@Override
	public void draw(Graphics g, Projection j){
		getDraw().drawLoop(this,g,j);
	}
		
	@Override
	public String toString(){
		StringBuilder dir=new StringBuilder();
		boolean modifColorLine=getLineColor()!=LatexColor.getColorByName("Black");
		boolean modifColorLabel=getLabelColor()!=LatexColor.getColorByName("Black");
		if(modifColorLine){
			dir.append("\\ChgEdgeLineColor{"+getLineColor()+"}\n");
		}
		if(modifColorLabel){
			dir.append("\\ChgEdgeLabelColor{"+getLabelColor()+"}\n");
		}
		dir.append("\\Loop"+direction.capInitials()+"{S"+state.getId()+"}{"+label+"}\n");
		if(modifColorLine){
			dir.append("\\RstEdgeLineColor\n");			
		}
		if(modifColorLabel){
			dir.append("\\RstLabelLineColor\n");			
		}
		return dir.toString();
	}
	
	@Override
	public Point handle(){
		Vector v=new Vector(getDraw().alpha*20*state.getDraw().getRadius()*.25,0);
		v.rot(-direction.angle());
		return state.getCenter().addTo(v);
	}

	@Override
	public void setDirection(Direction d) {
		direction=d;
	}

	@Override
	public Direction getDirection() {
		return direction;
	}
	@Override
	public void edit() {
		JComponent[] tab={new LineColorChooser(this), new LabelColorChooser(this), new DirectionChooser(this), new LabelChooserChooser(this)};
		new Edit("Loop",tab);
	}

	@Override
	public StateComponent getDst() {
		return state;
	}

}
