package gui;

import java.util.HashMap;
import java.util.ArrayList;

import controller.InstructionList;
import controller.Instruction;

public class ControllerGuiCommunicator
{
	private int instruction_id_;
	private RuleTreePanel rule_tree_panel_;
	private HashMap brick_images_;
	private TUGLabyrinth labyrinth_;

	public ControllerGuiCommunicator( RuleTreePanel rule_tree_panel, HashMap brick_images, TUGLabyrinth labyrinth)
	{
		instruction_id_=0;
		rule_tree_panel_=rule_tree_panel;
		brick_images_=brick_images;
		labyrinth_=labyrinth;
	}

	public void addEmptyRule()
	{
		instruction_id_++;
		rule_tree_panel_.addEmptyRule(instruction_id_);
	}

	public ArrayList instructionListToRuleList(InstructionList instructions)
	{
		ArrayList tmp=new ArrayList();
		for (int index = 0; index < instructions.size(); index++)
        {
	        Instruction dummy = instructions.getInstructionAtPos(index);
	        if (dummy.getId()==0)
	        {
	        	dummy.updateId(instruction_id_);
	        	instruction_id_++;
       	 	}
          	tmp.add(new RulePositionPanel(new RulePanel(brick_images_, labyrinth_, dummy, dummy.getId(), rule_tree_panel_)));
        }
		return tmp;
	}

	public InstructionList ruleListToInstructionList(ArrayList rule_list)
	{
		InstructionList tmp=new InstructionList();
		for (int index = 0; index < rule_list.size(); index++)
        {
	        RulePanel dummy = (RulePanel)((RulePositionPanel)rule_list.get(index)).getRulePanel();
          	tmp.add(dummy.getInstruction());
        }
		return tmp;
	}

	public InstructionList getInstructionListFromRuleTree()
	{
		return ruleListToInstructionList(rule_tree_panel_.getRuleList());
	}

	public void setInstructionListToRuleTree(InstructionList instructions)
	{
		rule_tree_panel_.setRuleList(instructionListToRuleList(instructions));
	}


	public ArrayList ruleListFromInstructionList(InstructionList instructions)
	{
		ArrayList tmp=new ArrayList();
		for (int index = 0; index < instructions.size(); index++)
        {
	        Instruction dummy = instructions.getInstructionAtPos(index);
       	 	RulePanel rule= rule_tree_panel_.getRule(dummy.getId());
          	tmp.add(new RulePositionPanel(rule));
        }
		return tmp;
	}
}