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

public class FireDude : MonoBehaviour 
{
    [SerializeField] private Animator _anim;
    private NavMeshAgent _agent;
    private Portal _startPortal;

	// Use this for initialization
	void Awake () 
    {
        _agent = GetComponent<NavMeshAgent>();
        _agent.enabled = false;

		_anim.SetFloat("fMoveAmount", 1);
		_anim.SetFloat("fRunAmount", 1);
	}

    public void Setup(Portal portal)
    {
        _startPortal = portal;
        StartCoroutine(JumpOutPortal());
    }

    void Update()
    {
		_anim.SetFloat("fLeanAmount", (Mathf.Sin(Mathf.PI * (Time.time * 3) - (Mathf.PI / 2f))));
		//if(_agent.enabled) _anim.SetFloat("fMoveAmount", _agent.velocity.magnitude / _agent.speed);
    }
	

    IEnumerator JumpOutPortal()
    {
        Vector3 startPos = transform.position;
        Vector3 endPos = startPos + _startPortal.transform.forward * 4.8f;
        endPos.y = 0;

        transform.LookAt(new Vector3(endPos.x, transform.position.y, endPos.z));

        float progress = 0f;
        while(progress < 1)
        {
            progress += Time.deltaTime * 0.75f;

            Vector3 target = Vector3.Lerp(startPos, endPos, progress);
            target.y += Mathf.Sin(progress * Mathf.PI);

            transform.position = target;

            yield return null;
        }

        transform.position = new Vector3(transform.position.x, 0, transform.position.z);
        StartCoroutine(Chaos());
    }


    IEnumerator Chaos()
    {
        _agent.enabled = true;
        float timer = 100000;
        float wanderRadius = 8f;

        float startTime = Time.time;
        while (Time.time - startTime < 14)
        {

            timer += Time.deltaTime;

            if (timer >= 1f || _agent.remainingDistance < 0.5f)
            {
                Vector3 newPos = RandomNavSphere(transform.position, wanderRadius, -1);
                _agent.SetDestination(newPos);
                timer = 0;
            }

            yield return null;
        }

        StartCoroutine(JumpInPortal());
    }



    public static Vector3 RandomNavSphere(Vector3 origin, float dist, int layermask)
    {
        Vector3 randDirection = Random.insideUnitSphere * dist;

        randDirection += origin;

        NavMeshHit navHit;

        NavMesh.SamplePosition(randDirection, out navHit, dist, layermask);

        return navHit.position;
    }


    IEnumerator JumpInPortal()
    {
        Portal portalTarget = GameController.Instance.Portals[Random.Range(0, GameController.Instance.Portals.Length)];
        _agent.SetDestination(portalTarget.transform.position);

        while (Vector3.Distance(transform.position, _agent.destination) > 3f)
            yield return null;

        _agent.enabled = false;

        Vector3 startPos = transform.position;
        Vector3 endPos = portalTarget.transform.position - portalTarget.transform.forward * 3f;
        endPos.y += 0.6f;

        endPos.y = 0;

        float progress = 0f;
        while (progress < 1)
        {
            progress += Time.deltaTime * 0.75f;

            Vector3 target = Vector3.Lerp(startPos, endPos, progress);
            target.y += Mathf.Sin(progress * Mathf.PI);

            transform.position = target;

            yield return null;
        }

        Destroy(this.gameObject);
    }


}
