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

public class Enemy : Grabable 
{
    enum State
    {
        Moving,
        Grabbed,
        Attacking
    }

    private State _currentState = State.Moving;

    private NavMeshAgent _agent;

    [SerializeField] Animator _animator;

    private Package _target = null;
    private float _searchRange = 4f;

    private float _attackRange = 1.8f;

    private float _attackPrev = -10000;
    private float _attackSpeed = 0.6f;

    private bool _active = false;

    public override void Setup(Vector3 force)
    {
        base.Setup(force);
        _agent = GetComponent<NavMeshAgent>();


        _agent.enabled = false;
        Invoke("EnableAgent", 3f);

        //_collider.enabled = false;
        //TargetType = targetType;

        //Invoke("EnableCollider", 0.4f);

        //var color = GameController.Instance.PortalColorPresets[targetType].BoxColor;
        //_renderer.material.SetColor("_Color", color);
    }

    void EnableAgent(){   	

        _currentState = State.Moving;
        _agent.enabled = true;
        _active = true;

        RigidBody.isKinematic = true;
    }





	// Use this for initialization
    public void Start() 
    {
        _agent = GetComponent<NavMeshAgent>();  
        GetTarget();
	}

    void GetTarget()
    {
        Package _closestTarget = null;
        float closestDist = 100000;

        List<Package> _packagesInRange = new List<Package>();

        for(int i = 0; i < GameController.Instance.Packages.Count; i++)
        {
            float dist = Vector3.Distance(transform.position, GameController.Instance.Packages[i].transform.position);

            if(dist < closestDist)
            {
                closestDist = dist;
                _closestTarget = GameController.Instance.Packages[i];
            }

            if(dist < _searchRange)
                _packagesInRange.Add(GameController.Instance.Packages[i]);
        }

        if(_packagesInRange.Count > 0)
            _target = _packagesInRange[Random.Range(0, _packagesInRange.Count)];
        else
            _target = _closestTarget;
    }
	
	// Update is called once per frame
    public override void Update() 
    {
        base.Update();
        
        if (_active == false)
            return;

        switch(_currentState)
        {
            case State.Moving:
                MoveToTarget();
                break;
            case State.Grabbed:
                Grabbed();
                break;
            case State.Attacking:
                AttackTarget();
                break;
        }

        _animator.SetFloat("fMoveAmount", _agent.velocity.magnitude / _agent.speed);
	}

    void MoveToTarget()
    {
        _agent.enabled = true;
        RigidBody.isKinematic = true;

        if (_target == null)
            GetTarget();

        if (_target == null)
            return;


        _agent.SetDestination(_target.transform.position);

        float dist = Vector3.Distance(transform.position, _target.transform.position);
        if (dist < _attackRange)
            _currentState = State.Attacking;
    }

    void Grabbed()
    {
        //logic is taken care of kinda
        _agent.enabled = false;

		if(!_animator.GetBool("bStruggling")) _animator.SetBool("bStruggling", true);


        if (_grabbed == false && Time.time - _releasedTime > 2){
        	_animator.SetBool("bStruggling", false);
            EnableAgent();
		}
    }

    void AttackTarget()
    {

        //Vector3 LookDir = transform.position - _agent.destination;
        //LookDir.y = 0;

        //if (LookDir != Vector3.zero && LookDir.magnitude > 0.01f)
        //{
        //    LookDir = LookDir.normalized;
        //    var targetRot = Quaternion.LookRotation(LookDir);
        //    transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, 5f * Time.deltaTime);
        //}

        _agent.enabled = false;
        RigidBody.isKinematic = true;

        _animator.SetTrigger("tBite");

        if (_target == null)
        {
            _currentState = State.Moving;
        }
        else
        {
			Vector3 targetDirection = _target.transform.position - transform.position;
        	targetDirection.y = 0;
			//if(targetDirection.magnitude < .001f) targetDirection = transform.forward;
			var targetRotation = Quaternion.LookRotation(targetDirection);
			transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 6f * Time.deltaTime);

            if (Vector3.Distance(transform.position, _target.transform.position) > _attackRange + 0.6f)
            {
                _currentState = State.Moving;
            }
            else
            {
                //attack
                if (Time.time - _attackPrev > _attackSpeed)
                {
                    _attackPrev = Time.time;
                    _target.GetHit();
                    //do attack anim
                }
            }
        }
    }

    public override void PickedUp()
    {
        if (_active)
        {
            base.PickedUp();
            _currentState = State.Grabbed;
        }
    }

    
     
}
