package automatvgi.components;

import java.awt.Graphics;

import javax.swing.JComponent;

import automatvgi.LatexColor;
import automatvgi.Projection;
import automatvgi.drawing.DrawEdge;
import automatvgi.edit.Edit;
import automatvgi.edit.LabelChooserChooser;
import automatvgi.edit.LabelColorChooser;
import automatvgi.edit.LineColorChooser;
import automatvgi.edit.TransitionShapeChooser;
import automatvgi.tools.Point;


public class EdgeComponent extends TransitionComponent {
	static DrawEdge globalDraw=DrawEdge.getDrawTransition("ArcL");
	DrawEdge specificDraw=null;
	
	private StateComponent sndState;
	
	public EdgeComponent(StateComponent start,StateComponent end){
		state=start; sndState=end;
		kind=Kind.Transition;
	}
	
	public EdgeComponent(StateComponent start,StateComponent end,String l){
		state=start; sndState=end;
		kind=Kind.Transition;
		label=l;
	}
	
	public  DrawEdge getDraw(){
		DrawEdge dt=specificDraw;
		if (dt==null) return globalDraw;
		return dt;
	}

	@Override
	public void draw(Graphics g, Projection j){
		getDraw().drawTransition(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("\\"+getDraw()+"{S"+state.getId()+"}{S"+sndState.getId()+"}{"+label+"}\n");
		if(modifColorLine){
			dir.append("\\RstEdgeLineColor\n");			
		}
		if(modifColorLabel){
			dir.append("\\RstLabelLineColor\n");			
		}
		return dir.toString();
	}

	@Override
	public Point handle(){
		return new Point(.5,getDraw().alpha*.23).convert(state.getCenter(), sndState.getCenter());
	}

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

	@Override
	public void edit() {
		JComponent[] tab={new LineColorChooser(this), new LabelColorChooser(this),
							new TransitionShapeChooser(this), new LabelChooserChooser(this)};
		new Edit("Transition",tab);
	}

	public void setSpecificDraw(DrawEdge specificDraw) {
		this.specificDraw = specificDraw;
	}

}
