package com.udacity.gamedev.dragoncurve;

import com.badlogic.gdx.math.Vector2;

import java.util.LinkedList;

public class DragonCurveGenerator {

    enum Direction {
        LEFT,
        RIGHT;

        public static Vector2 turn(Vector2 heading, Direction turn){
            Vector2 newHeading = new Vector2();
            switch (turn){
                case LEFT:
                    newHeading.x = -heading.y;
                    newHeading.y = heading.x;
                    break;
                case RIGHT:
                    newHeading.x = heading.y;
                    newHeading.y = -heading.x;
            }
            return newHeading;
        }
    }

    static LinkedList<Direction> dragonTurns(int recursions){
        LinkedList<Direction> turns = new LinkedList<Direction>();
        turns.add(Direction.LEFT);

        for (int i = 0; i < recursions; i++){
            // TODO: Create a reversed copy of turns


            // TODO: Add a left turn to turns


            // TODO: Add reflected version of reversed to turns

        }
        return turns;
    }

    static float[] generateDragonCurve(int width, int height, int recursions){

        LinkedList<Direction> turns = DragonCurveGenerator.dragonTurns(recursions);

        Vector2 head = new Vector2(width/2, height/2);
        Vector2 heading = new Vector2(5, 0);

        float[] curve = new float[(turns.size() + 1) * 2];

        int i = 0;
        curve[i++] = head.x;
        curve[i++] = head.y;

        //TODO: Convert the list of turns into the actual path


        return curve;

    }


}

@@ -891,22 +891,16 @@
       //
- TODO:
  Create 
@@ -920,24 +920,103 @@
 py of turns%0A
+            LinkedList%3CDirection%3E newTurns = new LinkedList%3CDirection%3E(turns);%0A
 %0A%0A          
@@ -1015,30 +1015,24 @@
           //
- TODO:
  Add a left 
@@ -1045,16 +1045,55 @@
 o turns%0A
+            turns.add(Direction.LEFT);%0A
 %0A%0A      
@@ -1104,14 +1104,8 @@
   //
- TODO:
  Add
@@ -1143,16 +1143,503 @@
 to turns
+%0A            java.util.Iterator%3CDirection%3E reverseNewTurns = newTurns.descendingIterator();%0A            while (reverseNewTurns.hasNext()) %7B%0A                Direction turn = reverseNewTurns.next();%0A                switch (turn)%7B%0A                    case LEFT:%0A                        turns.add(Direction.RIGHT);%0A                        break;%0A                    case RIGHT:%0A                        turns.add(Direction.LEFT);%0A                        break;%0A                %7D%0A            %7D
 %0A%0A      
@@ -2089,14 +2089,8 @@
   //
-TODO: 
 Conv
@@ -2132,16 +2132,214 @@
 al path%0A
+        for (Direction turn: turns) %7B%0A            head.add(heading);%0A            curve%5Bi++%5D = head.x;%0A            curve%5Bi++%5D = head.y;%0A            heading = Direction.turn(heading, turn);%0A        %7D%0A
 %0A%0A      

Complete Dragon Curve Challenge