package p5;
import anar.*;



import java.util.*;

import lsys.Grammar;
import processing.core.PApplet;


public class SimpleStructureTurtleC02 extends PApplet {

  /*
   * Example for Anar library by Guillaume LaBelle + Julien Nembrini
   * http://anar.ch
   */


  Pts       ptsA = new Pts();


  Pts       pts;
  Param     param;

  Grammar   grammar;

  Obj       faces;
  Obj       lines;

  Transform T;

  Translate t;
  Transform r;
  Scale     s;

  Sliders   mySlider;


  public void setup(){
    size(800,400,OPENGL);
    Anar.init(this);
    initGrammar();
    initForm();
    interpretGrammar();
  }

  void initGrammar(){
    grammar = new Grammar("fg");
    // here define the rules
    // * means any kind of symbol
    // the example rules below are therefore non contextual
    grammar.addRule("*f*","fg[gf][hfh]");
    grammar.addRule("*g*","gg[hif]");
    grammar.addRule("*h*","ghjh");
    grammar.addRule("*j*","[gih]");
    // this one makes it context dependant
    grammar.addRule("igh","[ghfg]");

    println(grammar);

  }

  void initForm(){
    faces = new Obj();

    t = new Translate(0,0,10);

    param = new Param(10,2,100);
    mySlider = new Sliders(faces);

  }


  void interpretGrammar(){

    faces = new Obj();
    lines = new Obj();

    Pt p = Anar.Pt( -30,0,0);
    Pt q = Anar.Pt(30,0,0);


    Pts seedSeg = new PtsMid(p,q,param);
    Pts currentFace = seedSeg;

    Pts stackA = new Pts();
    int indexA = 0;
    Pt turtleA = seedSeg.pt(indexA);

    ArrayList stackPtsA = new ArrayList();
    Pts ptsCollectorA = new Pts();
    ptsCollectorA.add(turtleA);


    Iterator it = grammar.getSentence().iterator();
    while (it.hasNext()){
      String str = (String)it.next();

      switch(str.charAt(0)){
        case 'f':
          // create translated copy from current face
          Pts pts = new PtsMid(Anar.Pt(currentFace.pt(0),t),Anar.Pt(currentFace.pt(currentFace.size()-1),t),param);
          // pts = new Pts();
          // Iterator<Pt> itFace = currentFace.iterator();
          // while (itFace.hasNext()) {
          // pts.add(Anar.Pt(itFace.next(), t));
          // }
          // TODO (1) add size() method to Face
          // TODO (1) transform the face (missing methods)
          faces.add(pts);

          // move the turtle to the next face
          currentFace = pts;
          indexA %= currentFace.numOfPts();
          turtleA = currentFace.pt(indexA);

        case 'g':
          indexA = (indexA+1)%currentFace.numOfPts();
          turtleA = currentFace.pt(indexA);

        break;
        case 'h':
          indexA = (indexA+currentFace.numOfPts()-1)%currentFace.numOfPts();
          turtleA = currentFace.pt(indexA);

        break;
        case 'i':
          indexA = (indexA+2)%currentFace.numOfPts();
          turtleA = currentFace.pt(indexA);

        break;
        case 'j':
          indexA = (indexA+currentFace.numOfPts()-2)%currentFace.numOfPts();
          turtleA = currentFace.pt(indexA);

        break;
        case '[':
          stackA.add(turtleA);
          stackPtsA.add(ptsCollectorA);
          ptsCollectorA = new Pts();
          ptsCollectorA.add(turtleA);

        break;
        case ']':
          ptsCollectorA.add(turtleA);
          turtleA = stackA.remove(stackA.size()-1);
          lines.add(ptsCollectorA);
          ptsCollectorA = (Pts)stackPtsA.remove(stackPtsA.size()-1);

        break;
        default:
        break;
      }
    }
    lines.pts.render = new RenderPtsLine(new AColor(128));

    Anar.camTarget(faces);
    mySlider = new Sliders(lines);

  }

  public void draw(){
    if(frameCount%2==0)
      background(155);
    else
      background(153);

    faces.draw();
    lines.draw();
    mySlider.draw();

  }


  public void keyPressed(){
    if(key=='a'){
      grammar.step();
      println(grammar);
      interpretGrammar();
    }
    if(key=='s'){
      grammar.reset();
      println(grammar);
      interpretGrammar();

    }
  }

  public static void main(String[] args){
    PApplet.main(new String[]{SimpleStructureTurtleC02.class.getName()});
  }


}

