using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class EnterDoor : MonoBehaviour {

    public string sceneName;
    public GameObject player;
    public Transform[] spawnPoint;
    public static int spawnPointIndex = 3;
    public Text doorText;
    public static int townIndex;

    //teleporting player when leaving back to the door they initially entered (to target)
    void OnLevelWasLoaded() {
        player.transform.position = spawnPoint[spawnPointIndex].position;
    }

    void Start()
    {
        if (SceneManager.GetActiveScene().name == "Town01") { townIndex = 1; }
        else if (SceneManager.GetActiveScene().name == "Town02") { townIndex = 2; }
        Debug.Log("Town index: " + townIndex);
    }
    // when creating extra doors, add spawnPoint[] in correct order in the inspector.
    void OnTriggerEnter2D(Collider2D collider)
    {
        if (this.CompareTag("Church"))
        {
            spawnPointIndex = 0;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("Blacksmith"))
        {
            spawnPointIndex = 1;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("Arena"))
        {
            spawnPointIndex = 2;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("MainSpawn"))
        {
            spawnPointIndex = 3;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("NextTown"))
        {
            spawnPointIndex = 4;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("Armoury")) {
            spawnPointIndex = 5;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("DesertChurch"))
        {
            spawnPointIndex = 6;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("DesertBlacksmith"))
        {
            spawnPointIndex = 7;
            Debug.Log("The index is:" + spawnPointIndex);
        }
        else if (this.CompareTag("DesertArena"))
        {
            spawnPointIndex = 8;
            Debug.Log("The index is:" + spawnPointIndex);
        }

        if (collider.CompareTag("Player") && spawnPointIndex != 3)
        {
            doorText.text = ("Press [F] to Enter");
            if (Input.GetKeyDown("f"))
            {
                SceneManager.LoadScene(sceneName);
            }
        }
        if (collider.CompareTag("Player") && spawnPointIndex == 4)
        {
            doorText.text = ("PRESS [F] TO GO TO THE NEXT TOWN");
            if (Input.GetKeyDown("f")) SceneManager.LoadScene(sceneName);
        }
    }
    void OnTriggerStay2D(Collider2D collider) {
        if (collider.tag == "Player" && spawnPointIndex != 3) {
            if (Input.GetKeyDown("f")) {
                SceneManager.LoadScene(sceneName);
            }
        }
    }

    void OnTriggerExit2D(Collider2D collider) {
        if (collider.tag == "Player") {
            doorText.text = (""); // reset the "Press [F]...." text
        }
    }
    //returns the town index.
    int GetTownIndex()
    {
        return townIndex;
    }

 
}