using UnityEngine;
using System.Collections;

public class GameWorld : MonoBehaviour {

    public GameObject player;
    public GameObject enemy;
    public GameObject start;
    public GameObject end;
    public GameObject[] checkpoint;
    public float enemyRespawnDistance;
    private bool[] hasCrossed;

	// Use this for initialization
    void Start()
    {
        hasCrossed = new bool[checkpoint.Length];
        for (int i = 0; i < checkpoint.Length; i++)
        {
            hasCrossed[i] = false;
            string debugMSG = "The " + i.ToString() + "Checkpoint has been set up";
            Debug.Log(debugMSG);
        }

    }

    // Update is called once per frame
    void Update()
    {
	    //Check to see if player hit a checkpoint
        for (int i = 0; i < checkpoint.Length; i++)
        {
            if (player.transform.position.x > checkpoint[i].transform.position.x && hasCrossed[i] == false)
            {
                hasCrossed[i] = true;
                string debugMSG = "The " + i.ToString() + "Checkpoint has been crossed";
                Debug.Log(debugMSG);
            }
        }

        //Check to see if the player hit the exit
        if (player.transform.position.x > end.transform.position.x)
        {
            /*if (Application.loadedLevelName == "blockout_test")
                Application.LoadLevel("level1");
            else
                Application.LoadLevel("MainMenu");*/
			if(Application.loadedLevel < (Application.levelCount-1))
				Application.LoadLevel(Application.loadedLevel + 1);
			else
				Application.LoadLevel(0);
        }
	}

    void ReturnToHighestCheckpoint()
    {
        int highest = -1;
        for (int i = 0; i < checkpoint.Length; i++)
        {
            if (hasCrossed[i])
                highest = i;
        }
        if (highest == -1)
        {
            player.transform.position = start.transform.position;
            enemy.transform.position = new Vector3(player.transform.position.x - enemyRespawnDistance, enemy.transform.position.y);
        }
        else
        {
            player.transform.position = checkpoint[highest].transform.position;
            enemy.transform.position = new Vector3(player.transform.position.x - enemyRespawnDistance, enemy.transform.position.y);
        }
    }

    void ReturnToStart()
    {
        player.transform.position = start.transform.position;
    }
}
