package automatvgi.components;

import java.awt.Color;
import java.awt.Graphics;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

//import automatvgi.LatexColor;
import automatvgi.Projection;
import automatvgi.tools.Point;

public class Automaton implements Iterable<AutomatonComponent>{

	private LinkedList<AutomatonComponent> lac=new LinkedList<AutomatonComponent>();
	private AutomatonFrame frame;
	
	public Automaton(){
		frame=new AutomatonFrame();
		lac.add(frame);
	}
	
	public void addStateComponent(Point x, String s){
		StateComponent ac=new StateComponent(x,s,frame.getStateDefaultStyle());
		lac.add(ac);
	}

	public void addLineComponent(LineComponent ac){
		ac.setDefaut(frame.getEdgeDefaultStyle());
		lac.add(ac);
	}

	public void addTransitionComponent(TransitionComponent ac){
		for(AutomatonComponent oac : lac)
			if (oac instanceof TransitionComponent) {
				TransitionComponent lc = (TransitionComponent) oac;
				if(lc.getSrc()==ac.getSrc() && lc.getDst()==ac.getDst()){
					lc.setLabel(lc.getLabel()+","+ac.getLabel());
					return;
				}	
			}
		ac.setDefaut(frame.getEdgeDefaultStyle());
		lac.add(ac);
	}

	public Iterator<AutomatonComponent> iterator() {
		return lac.iterator();
	}
	
	private static void drawHandle(Point p, Graphics g, Projection j){
		int x=j.getAbs(p), y=j.getOrd(p);
		g.setColor(Color.RED);
		g.drawRect(x-1,y-1,2,2);
	}

	
	public void draw(Graphics g, Projection j, boolean showHandles){
		for(AutomatonComponent ac : lac){
			ac.draw(g,j);
			if(showHandles)
				if(ac.handle()!=null)
					drawHandle(ac.handle(), g, j);
		}

	}
	
	@Override
	public String toString(){
		StringBuilder sb=new StringBuilder();
		for(AutomatonComponent ac : lac)
			sb.append(ac.toString());
		sb.append("\\end{VCPicture}\n");	
		return sb.toString();
	}

	public void fire(char a){
		HashMap<StateComponent, LinkedList<StateComponent>> map = new HashMap<StateComponent, LinkedList<StateComponent>>();
		for(AutomatonComponent ac : lac){
			if (ac instanceof TransitionComponent) {
				TransitionComponent tac = (TransitionComponent) ac;
				if(tac.getLabel().indexOf(""+a)!=-1){
					System.out.println(tac);
					LinkedList<StateComponent> ls=map.get(tac.getDst());
					if(ls == null){
						ls = new LinkedList<StateComponent>();
						map.put(tac.getDst(), ls);
					}
					for(StateComponent dst : tac.getSrc().getJetons())
						ls.add(dst);
				}
			}
		}
		for(AutomatonComponent ac : lac){
			if (ac instanceof StateComponent) {
				StateComponent sac = (StateComponent) ac;
				LinkedList<StateComponent> ls=map.get(sac);
				if(ls==null)
					ls= new LinkedList<StateComponent>();
				sac.setJetons(ls);					
			}
		}
	}
	
	public void delete(AutomatonComponent s){
		if (s instanceof LineComponent) {
			lac.remove(s);
		}
		else if (s instanceof StateComponent) {
			Iterator<AutomatonComponent> it=lac.iterator();
			while(it.hasNext()){
				AutomatonComponent bc=it.next();
				if (bc instanceof LineComponent){
					LineComponent lc=(LineComponent)bc;
					if(lc.getState().equals(s))
						it.remove();
					else if(bc instanceof EdgeComponent){
						EdgeComponent tc=(EdgeComponent) bc;
						if(tc.getDst().equals(s))
							it.remove();
					}
				}
			}
			lac.remove(s);
		}
	}
}
