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

import java.io.*;

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

  public ASTProgram(drawphase3 p, int id) {
    super(p, id);
  }

  public void generateFile()
  {
    String file = "tester.svg";
    try
    {
    	FileWriter fw = new FileWriter(file);
    	BufferedWriter bw = new BufferedWriter(fw);
    	PrintWriter outFile = new PrintWriter(bw);
    	
    	outFile.println("<svg>\n");    	
   	    System.out.println("<svg>");      	    
   	    
   	    String colour = "black";
   	    int brushSize = 1, offsetX = 0, offsetY = 0, scaleH = 1, scaleV = 1, repeatNum = 1, repeatX = 0, repeatY = 0, offsetXold = 0, offsetYold = 0;
  		boolean doubleUp = false;
        
        //Generates each child in the program and outputs the results to a file.
        for(int i = 2; i < jjtGetNumChildren(); i++)
    	{    		
    		String childName = jjtGetChild(i).getName();    		
    		
    		//Determines the type of each command before generating it.
    		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++)
    			{    				
    				outFile.println(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 after each command in the base program. 
    			colour = "black";
    			brushSize = 1;
    			offsetX = 0;
    			offsetY = 0;
    			scaleH = 1;
    			scaleV = 1;
    			repeatX = 0;
    			repeatY = 0;    			
    			repeatNum = 1;
    		}   
    				
    	}
    	outFile.println("</svg>\n");
    	System.out.println("</svg>");
    	outFile.close();
    }
    catch(IOException e)
    {
    	System.exit(1);
    }
    
   
    	
  }
  
  public String getName()
  {
     return name;
  }
}
