using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets._2D;

public class SwitchTrigger : MonoBehaviour {

	public List <GameObject> HumansInRange = new List<GameObject>();
	public GameObject Selected;
	public GameObject Flame;
	public GameObject EnterKey;
	public Collider2D[] CollidersToChange;

	public Animator animator;

	RaycastHit2D hit;

	public bool IsPossessed = false;
	public bool IsBarred = false;
    public bool canBeJumpedTo = false;
	void Start () {

		animator = EnterKey.GetComponent<Animator>();
		
	}
	
	void Update () {


		
		
		//if (hit.collider != null)
		//	print("HIT: "+hit.collider.name);
		//else IsBarred = false;
		

		
		if(this.gameObject.tag == "UnPossessed") {
			
			

			Selected = null;
			
		}

		if(this.gameObject.tag == "Possessed") {
			
			HumansInRange.Sort(delegate (GameObject a, GameObject b) {

				return Vector3.Distance(this.transform.position, a.transform.position).CompareTo
                 ((Vector3.Distance(this.transform.position, b.transform.position)));  

			});

		}

		if (Input.GetKeyDown(KeyCode.Tab) && HumansInRange.Count >= 2 && this.gameObject.tag == "Possessed") {

			int CurrentIndex;
			
			CurrentIndex = HumansInRange.IndexOf(Selected);

			CurrentIndex++;

			if (CurrentIndex >= HumansInRange.Count)
				CurrentIndex = 0;
			
			HumansInRange[HumansInRange.IndexOf(Selected)].GetComponent<SwitchTrigger>().EnterKey.SetActive(false);
			

			
			HumansInRange[CurrentIndex].gameObject.GetComponent<SwitchTrigger>().EnterKey.SetActive(true);
			
			Selected = HumansInRange[CurrentIndex];

			

		}

		if (Input.GetKeyDown(KeyCode.KeypadEnter) && HumansInRange.Count >= 1 && this.gameObject.tag == "Possessed" && Selected != null) {

            if (!Selected.gameObject.GetComponent<SwitchTrigger>().canBeJumpedTo)
            {
                return;
            }

			Collider2D[] collidersNew = Selected.GetComponents<Collider2D>();
			
			this.GetComponent<Platformer2DUserControl>().enabled = false;
			Flame.SetActive(false);
			Selected.GetComponent<Platformer2DUserControl>().enabled = true;
			Selected.GetComponent<SwitchTrigger>().EnterKey.SetActive(false);
			Selected.GetComponent<SwitchTrigger>().Flame.SetActive(true);
			Selected.tag = "Possessed";
			HumansInRange.Clear();
			this.GetComponent<Animator>().SetFloat("Speed",0);
			this.GetComponent<Rigidbody2D>().isKinematic = true;
			this.GetComponent<Rigidbody2D>().velocity = new Vector3(0,0,0);
			foreach(Collider2D i in CollidersToChange) {

				i.enabled = false;

			}
			Selected.GetComponent<Rigidbody2D>().isKinematic = false;
			foreach(Collider2D i in collidersNew) {

				if (i.enabled == false)
					i.enabled = true;

			}
			this.tag = "UnPossessed";

		}
        foreach(GameObject humanInRange in HumansInRange)
        {

        hit = Physics2D.Linecast(transform.position, humanInRange.transform.position);

        //Debug.Log(hit.collider.gameObject.name);

        Debug.DrawLine(transform.position, humanInRange.transform.position);

        if (hit.collider != null)
        {

            /*if(hit.collider.tag == "Barrier" && IsBarred == true) {

                return;

            }*/

            if (hit.collider.tag == "Barrier")
            {
                humanInRange.GetComponent<SwitchTrigger>().canBeJumpedTo = false;
                //HumansInRange.Add(col.gameObject);
                //IsBarred = true;
                //return;
            }
            else
            {
                humanInRange.GetComponent<SwitchTrigger>().canBeJumpedTo = true;
                
            }
        }
            else
            {
                humanInRange.GetComponent<SwitchTrigger>().canBeJumpedTo = true;

            }



        }

    }

	void OnTriggerStay2D (Collider2D col) {
		
		if (col.gameObject.tag == "UnPossessed" && this.gameObject.tag == "UnPossessed" && animator.GetBool("Enabled") == false)
			EnterKey.SetActive(false);

		if(col.gameObject.tag == "UnPossessed" && this.gameObject.tag == "Possessed" && HumansInRange.Count == 0 && !HumansInRange.Contains(col.gameObject))
        {
			print("InPossessionZone");
            Selected = col.gameObject;
            HumansInRange.Add(col.gameObject);
			//col.gameObject.transform.GetChild(0).gameObject.SetActive(true);
			//animator.SetBool("Enabled", true);
			//EnterKey.SetActive(true);
			
			
			/*if (Selected.GetComponent<SwitchTrigger>().animator.GetBool("Enabled") == false && Selected.GetComponent<SwitchTrigger>().animator != null)
				Selected.GetComponent<SwitchTrigger>().animator.SetBool("Enabled",true);*/
             Selected.GetComponent<SwitchTrigger>().EnterKey.SetActive(true);
            /*if (Selected.GetComponent<SwitchTrigger>().canBeJumpedTo)
            {
                Selected.GetComponent<SwitchTrigger>().animator.SetBool("Enabled", true);
            }
            else
            {
                Selected.GetComponent<SwitchTrigger>().animator.SetBool("Enabled", false);
            }*/
           

        }
    
		

		if(col.gameObject.tag == "UnPossessed" && this.gameObject.tag == "Possessed" && HumansInRange.Count >= 1 && !HumansInRange.Contains(col.gameObject)) {
			
			hit = Physics2D.Linecast(transform.position, col.transform.position);

			if (hit.collider != null) {

				if(hit.collider.tag == "Barrier") {
					
                    col.GetComponent<SwitchTrigger>().canBeJumpedTo = false;
                    HumansInRange.Add(col.gameObject);
                    return;
				}

			}

			print("InPossessionZone");
            col.GetComponent<SwitchTrigger>().canBeJumpedTo = true;
            HumansInRange.Add(col.gameObject);
			
		}

		if (Selected == null && col.gameObject.tag == "UnPossessed" && this.gameObject.tag == "Possessed" && HumansInRange.Count >= 1 && HumansInRange.Contains(col.gameObject)) {

			//if (hit.collider != null) {

			//	if(hit.collider.tag == "Barrier") {
			//		col.GetComponent<SwitchTrigger>().EnterKey.SetActive(true);
			//		col.GetComponent<SwitchTrigger>().animator.SetBool("Enabled", false);
			//		return;
			//	}

			//}

			//animator.SetBool("Enabled", true);
			//EnterKey.SetActive(true);
			Selected = col.gameObject;
			Selected.GetComponent<SwitchTrigger>().EnterKey.SetActive(true);

		}
		

	}

	void OnTriggerExit2D (Collider2D col) {
		
		if(col.gameObject.tag == "UnPossessed" && this.gameObject.tag == "Possessed" && HumansInRange.Contains(col.gameObject)) {
			

			print("OutPossessionZone");
			HumansInRange.Remove(col.gameObject);
			//animator.SetBool("Enabled", false);
			Selected.GetComponent<SwitchTrigger>().EnterKey.SetActive(false);
			if (Selected != null) Selected = null;

		}

		if (col.gameObject.tag == "UnPossessed" && this.gameObject.tag == "Possessed") {

			if (hit.collider != null) {

				if(hit.collider.tag == "Barrier") {
					col.GetComponent<SwitchTrigger>().EnterKey.SetActive(false);
					col.GetComponent<SwitchTrigger>().animator.SetBool("Enabled", false);
					if (IsBarred == true)
						IsBarred = false;
					return;
				}

			}

		}
		

	}
}
