package automatvgi.components;

import java.awt.Graphics;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComponent;

import automatvgi.LatexColor;
import automatvgi.Projection;
import automatvgi.drawing.DrawState;
import automatvgi.edit.Edit;
import automatvgi.edit.FillColorChooser;
import automatvgi.edit.LabelColorChooser;
import automatvgi.edit.LineColorChooser;
import automatvgi.edit.FillColorSetter;
import automatvgi.edit.LineColorSetter;
import automatvgi.tools.Point;

/**
 * 
 * @author sylvain
 */

public class StateComponent extends AutomatonComponent implements FillColorSetter, LineColorSetter{
	private static DrawState globalDraw=new DrawState();
	private static int counter=0;
	private int number;
	private DrawState specificDraw=null;
	
	private Point c;
	private String label=null;
	private LatexColor fillColor=null;
	
	private AutomatonStyle.StateSetter defaut;
	
	public int getId(){
		return number;
	}
	
	public StateComponent(Point p,String l, AutomatonStyle.StateSetter d){
		c=p;
		defaut =d;
		number=counter++;
		kind=Kind.State;
		label=l;
		jetons= new LinkedList<StateComponent>();
		jetons.add(this);
	}
	
	public void setLabel(String l){
		label=l;
	}
	
	public String getLabel(){
		return label;
	}
	
	@Override
	public void draw(Graphics g, Projection j){
		getDraw().drawState(this,g,j);
	}

	public Point getCenter(){
		return c;
	}

	public LatexColor getFillColor(){
		if(fillColor==null) return defaut.getFillColor();
		return fillColor;
	}
	
	public void setCenter(Point cf){
		c=cf;
	}
	
	public void setFillColor(LatexColor fillColor) {
		this.fillColor = fillColor;
	}

	public DrawState getDraw(){
		DrawState ds=specificDraw;
		if (ds==null) ds=globalDraw;
		return ds;
	}

	public LatexColor getLineColor() {
		if(lineColor==null) return defaut.getLineColor();
		return lineColor;
	}

	public LatexColor getLabelColor() {
		if(labelColor==null) return defaut.getLabelColor();
		return labelColor;
	}

	@Override
	public String toString(){
		StringBuilder sb= new StringBuilder("\\State");
		boolean modifColorLine=getLineColor()!=defaut.getLineColor();
		boolean modifColorLabel=getLabelColor()!=defaut.getLabelColor();
		boolean modifColorFill=getFillColor()!=defaut.getFillColor();
		if(modifColorLine){
			sb.append("\\ChgStateLineColor{"+getLineColor()+"}\n");
		}
		if(modifColorLabel){
			sb.append("\\ChgStateLabelColor{"+getLabelColor()+"}\n");
		}
		if(modifColorFill){
			sb.append("\\ChgStateFillColor{"+getFillColor()+"}\n");
		}
		if (label !=null)
			sb.append("["+label+"]");
		sb.append("{("+c.getX()+","+c.getY()+")}{S"+number+"}\n");
		if(modifColorLine){
			sb.append("\\RstStateLineColor\n");			
		}
		if(modifColorLabel){
			sb.append("\\RstStateLabelColor\n");			
		}
		if(modifColorFill){
			sb.append("\\RstStateFillColor\n");			
		}
		return sb.toString();
	}

	@Override
	public Point handle(){
		return getCenter();
	}
	
	@Override
	public void edit() {
		JComponent[] tab={new LineColorChooser(this), new LabelColorChooser(this),
							new FillColorChooser(this)};
		new Edit("State",tab);
	}

	public static void setGlobalDraw(DrawState globalDraw) {
		StateComponent.globalDraw = globalDraw;
	}

	public static void setCounter(int counter) {
		StateComponent.counter = counter;
	}

	public void setNumber(int number) {
		this.number = number;
	}

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

	public void setC(Point c) {
		this.c = c;
	}

	public void setDefaut(AutomatonStyle.StateSetter defaut) {
		this.defaut = defaut;
	}

	
	private List<StateComponent> jetons;

	public List<StateComponent> getJetons() {
		return jetons;
	}

	public void setJetons(List<StateComponent> jetons) {
		this.jetons = jetons;
	}
	
	
}

