/* Generated By:JJTree: Do not edit this line. ASTBlock.java */

public class ASTBlock extends SimpleNode {
  String name = "block";
  
  public ASTBlock(int id) {
    super(id);
  }

  public ASTBlock(drawphase3 p, int id) {
    super(p, id);
  }
  
  public String generate(String colour, int brushSize, int offsetX, int offsetY, int scaleH, int scaleV)
  {
  	String temp = "";
  	int repeatX = 0, repeatY = 0, repeatNum = 1, offsetXold = 0, offsetYold = 0;
  	boolean doubleUp = false;
  	
  	//Generates all the children in the block
  	for(int i = 0; i < jjtGetNumChildren(); i++)
    {    		
    	String childName = jjtGetChild(i).getName(); 
    	  		
    		//This block determines the type of the child before generation.
    		if (childName.equals("colour") == true)
    		{
    			colour = jjtGetChild(i).generate(colour, brushSize, offsetX, offsetY, scaleH, scaleV);
    		}
    		else if (childName.equals("brush") == true)
    		{
    			brushSize = Integer.parseInt(jjtGetChild(i).generate(colour, brushSize, offsetX, offsetY, scaleH, scaleV));
    		}
    		else if (childName.equals("offset") == true)
    		{    			
    			//Gets offset values and determines whether there are 2 offsets
    			//on the same line. Also stores old offset values for later 
    			//calculations.
    			offsetX += jjtGetChild(i).generateX();
    			offsetY += jjtGetChild(i).generateY();
    			if(doubleUp == true)
    			{
    				offsetXold += jjtGetChild(i).generateX();
    				offsetYold += jjtGetChild(i).generateY();
    			}
    			else
    			{
    				offsetXold = jjtGetChild(i).generateX();
    				offsetYold = jjtGetChild(i).generateY();
    			}
    			doubleUp = true;
    		}    		
    		else if (childName.equals("scale") == true)
    		{
    			//Gets the scale values and determines if they need to be added or not.    			
    			if(scaleH == 1 && scaleV == 1)
    			{
    				scaleH = jjtGetChild(i).generateX();
    				scaleV = jjtGetChild(i).generateY();
    			}
    			else
    			{
    				//Ensures that scale values are added correctly 
    				//I.E Scale 1 + Scale 2 = Scale 2, Scale 2 + scale 2 = scale 4 etc.
    				if(scaleH == 1 && jjtGetChild(i).generateX() != 1)
    				{
    					scaleH = jjtGetChild(i).generateX();
    				}
    				else if(scaleH != 1 && jjtGetChild(i).generateX() != 1)
    				{
    					scaleH += jjtGetChild(i).generateX();
    				} 
    				if(scaleV == 1 && jjtGetChild(i).generateY() != 1)
    				{
    					scaleV = jjtGetChild(i).generateY();
    				}
    				else if(scaleV != 1 && jjtGetChild(i).generateY() != 1)
    				{
    					scaleV += jjtGetChild(i).generateY();
    				}    				
    			}
    		}    			    		
    		else if (childName.equals("repeat") == true)
    		{    			
    			//Gets the repeat offset values and the number of repetitions.
    			repeatX = jjtGetChild(i).generateX();
    			repeatY = jjtGetChild(i).generateY();
    			if(repeatNum == 1)
    			{
    				repeatNum = Integer.parseInt(jjtGetChild(i).generate(colour, brushSize, offsetX, offsetY, scaleH, scaleV));
    			}
    			else
    			{
    				repeatNum += Integer.parseInt(jjtGetChild(i).generate(colour, brushSize, offsetX, offsetY, scaleH, scaleV));
    			}
    		}
    		else
    		{    			
    			int offsetXrep = offsetX;
    			int offsetYrep = offsetY;
    			//Repeats the command repeatNum times.
    			for(int j = 0; j < repeatNum; j++)
    			{    				
    				temp += (jjtGetChild(i).generate(colour, brushSize, offsetX, offsetY, scaleH, scaleV));
    				//Modifies the offset for the repeat method offsets.
    				offsetX += repeatX;
    				offsetY += repeatY;
    				doubleUp = false;
    			}
    			repeatNum = 1;
    			
    			//Returns the offsets to pre repeat.
    			offsetX = offsetXrep;
    			offsetY = offsetYrep; 
    			
    			//Resets the variables if returning from a block.   
    			if(childName.equals("block") == true)
    			{    				
    				colour = "black";
    				brushSize = 1;
    				offsetX -= offsetXold;
    				offsetY -= offsetYold;
    				scaleH = 1;
    				scaleV = 1;
    				repeatX = 0;
    				repeatY = 0;    			
    				repeatNum = 1;
    				offsetXold = 0;
    				offsetYold = 0;
    			}  			    			
    		}    
    				
    }
    return temp;
  }
  
  public String getName()
  {
  	return name;
  }

}
