using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {

	public static int state = 0;

	private int currentLevel = 0;
	private GameObject maze;
	private GameObject player;
	private GameObject goal;
	private GameObject[] platformsArr;
	private GameObject[] obstaclesArr;
	private GameObject[] hingesArr;
	private GameObject[] hazzardsArr;

	private bool shrinkGoal = true;

	// Use this for initialization
	void Start () {
			// handle game state
		GameManager.state = GameState.INIT;
			
			// define references to game objects
		maze = GameObject.Find("MazeRoot");
		player = GameObject.Find("Player");
		goal = GameObject.Find("Door");

		platformsArr = new GameObject[7];
			platformsArr[0] = GameObject.Find("Platform_00");
			platformsArr[1] = GameObject.Find("Platform_01");
			platformsArr[2] = GameObject.Find("Platform_02");
			platformsArr[3] = GameObject.Find("Platform_03");
			platformsArr[4] = GameObject.Find("Platform_04");
			platformsArr[5] = GameObject.Find("Platform_05");
			platformsArr[6] = GameObject.Find("Platform_06");

		obstaclesArr = new GameObject[3];
			obstaclesArr[0] = GameObject.Find("Block_00");
			obstaclesArr[1] = GameObject.Find("Block_01");
			obstaclesArr[2] = GameObject.Find("Block_02");

		assignPositionsFromLevels(currentLevel);

	}

	void assignPositionsFromLevels(int lev)
	{
		//maze.transform.rotation = new Quaternion(0.0f, 0.0f, 0.0f, 1.0f); // I don't actually need to set the maze rotation since this is a perfectly square box
		maze.GetComponent<MazeRotationController>().rotationsRemaining = Levels.rotationsArr[lev];
		player.transform.position = Levels.playerPosArr[lev];
		goal.transform.position = Levels.golaPosArr[lev];

		platformsArr[0].transform.position = Levels.plat1PosArr[lev];
		platformsArr[0].transform.rotation = Levels.plat1RotArr[lev];
		platformsArr[0].transform.localScale = Levels.plat1SclArr[lev];
	}

	// Update is called once per frame
	void Update () {
			// this is such a bullshit hack!  But it works!  It basically allows me to change the scale of the goal on 'player touch goal', and it will constantly be lerping back to it's original size!
		if(shrinkGoal)
		{
			goal.transform.localScale = Vector3.Lerp(goal.transform.localScale, new Vector3(1.25f,2.2f,1.8f), 1.65f * Time.deltaTime);
			if(goal.transform.localScale.x < 1.3 )
			{
				shrinkGoal = false;
			}
		}
	}

	public void resetPressed()
	{
		if(false) // true for printing level data  false for resetting the level
		{
			string str = "";
			str += "maze rot: " + maze.transform.rotation + "\n";
			str += "player pos: " + player.transform.position + " \n";
			str += "goal pos: " + goal.transform.position + "\n";

			for( int i = 0 ; i < platformsArr.Length ; i++)
			{
				str += "platform_" + i + " p: " + platformsArr[i].transform.position + "  r: " + platformsArr[i].transform.rotation + "  s: " + platformsArr[i].transform.localScale + "\n";
			}


			print (str);
		}
		else
		{
			assignPositionsFromLevels(currentLevel);
		}
	}

	public void hitGoal()
	{
		//print ("Hit Goal");
		//if( GameManager.state == GameState.GOAL )

		currentLevel++;
		if( currentLevel > Levels.rotationsArr.Length -1)
			currentLevel = 0;

		assignPositionsFromLevels(currentLevel);
		showFlair();
	}

	void showFlair()
	{
		print ( "hit goal");
		goal.transform.localScale = new Vector3(9f, 9f, 9f);
		shrinkGoal = true;
		//goal.collider.renderer.material.color = Color.red;
		//print(GameObject.Find("Platform_00").transform.position );
		//print(GameObject.Find("Platform_00").transform.rotation );
		//print(GameObject.Find("Platform_00").transform.localScale );

		//GameObject pl = GameObject.Find("Platform_00");
		//pl.transform.position = new Vector3( .7f , -6f, 5f );
		//pl.transform.rotation = new Quaternion( 0, 0, -.7f, .7f);
	}
}
