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

public enum EnemyType {
    Patrolling,
    Seeking,
    Navigating
}

public class Enemy : MonoBehaviour {

    public Transform player;
    public Transform[] waypoint;
    public float patrolSpeed = 3;
    public bool loop = true;
    public float dampingLook = 6.0f;
    public float pauseDuration = 0.0f;
    public EnemyType type = EnemyType.Patrolling;
    public Text gameOverText;

    public Camera cam1;
    public Camera cam2;

    // public text

    private Vector3 navTarget;

    private float curTime;
    private int currentWaypoint = 0;
    private CharacterController character;
    private bool canMove;
    private bool playerInteraction;
    private bool spotlightActive = false;
    private Transform targetTransform;

    // Use this for initialization
    void Start () {
        canMove = false;
        transform.position = new Vector3(transform.position.x, 1.5f, transform.position.z);
        character = GetComponent<CharacterController>();
        playerInteraction = false;
        transform.FindChild("Spotlight").gameObject.SetActive(spotlightActive);


    }
	
	// Update is called once per frame
	void Update () {
        if (Time.timeScale == 0)
        {
            if (Input.GetKeyDown(KeyCode.R) || Input.GetKeyDown(KeyCode.Joystick1Button7))
            {
                Time.timeScale = 1.0f;
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            }
        }

        if (type == EnemyType.Patrolling)
        {

            if (canMove && !playerInteraction)
            {
                if (currentWaypoint < waypoint.Length)
                {
                    patrol();
                }
                else if (loop)
                {
                    currentWaypoint = 0;
                }
            }
            canMove = false;
        }
        else if (type == EnemyType.Seeking)
        {
            if (targetTransform != null && canMove && !playerInteraction)
            {
                follow();
            }
            canMove = false;
        }
        else
        {
            if(canMove && !playerInteraction)
            {
                navigate();
            }
            canMove = false;
        }
	}

    void navigate()
    {
        var target = navTarget;
        target.y = transform.position.y;
        var moveDirection = target - transform.position;

        if (moveDirection.magnitude > 0.5)
        {
            var rotation = Quaternion.LookRotation(target - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
            character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
        }
    }

    void follow()
    {
        var target = targetTransform.position;
        target.y = transform.position.y;
        var moveDirection = target - transform.position;

        if (moveDirection.magnitude >  0.5)
        {
            var rotation = Quaternion.LookRotation(target - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
            character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
        }
    }

    void patrol()
    {
        var target = waypoint[currentWaypoint].position;
        target.y = transform.position.y;
        var moveDirection = target - transform.position;

        if (moveDirection.magnitude < 0.5)
        {
            if (curTime == 0)
            {
                curTime = Time.time;
            }
            if ((Time.time - curTime) >= pauseDuration)
            {
                currentWaypoint++;
                curTime = 0;
            }
        }
        else
        {
            var rotation = Quaternion.LookRotation(target - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
            character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
        }
    }

    void OnTriggerStay(Collider other)
    {
        if (other.tag == "Light")
        {
            canMove = true;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            playerInteraction = true;
            spotlightActive = true;
            transform.LookAt(other.gameObject.transform);
            //other.gameObject.transform.rotation = Quaternion.LookRotation(transform.position - other.gameObject.transform.position);

            cam1.enabled = false;
            cam2.enabled = true;
            gameOverText.enabled = true;
            transform.FindChild("Spotlight").gameObject.SetActive(spotlightActive);
            GetComponent<AudioSource>().Play();
            //other.gameObject.SendMessage("kill", transform);
            Time.timeScale = 0;
        }
        else if(other.tag == "Light")
        {
            targetTransform = other.transform;
            navTarget = player.transform.position;
        }
    }
}
