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

public class Beast : Monster {

    const float ATTACK_RANGE = 15f;

    public float chargeActivation = 1f;

    public float chargeRecover = 2f;

    public float moveSpeed = 2.5f;

    public float chargeSpeed = 20f;

    public float turnSpeed = 150;

    public float damage = 20f;

    bool attacking;

    float attackTime;

    float chargeTravelled;

    Animator anim;

    void Awake()
    {
        anim = GetComponentInChildren<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!GlobalUpdate())
            return;

        if (attackTime > 0)
            attackTime -= Time.deltaTime;

        if (!attacking)
        {
            if (attackTime <= 0)
            {
                transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(playerReference.transform.position - transform.position, Vector3.up), turnSpeed * Time.deltaTime);
            }
            /*else
            {
                transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(transform.position - playerReference.transform.position, Vector3.up), turnSpeed * Time.deltaTime);
            }*/
        }
        else
        {
            if (attackTime <= 0)
            {
                transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(playerReference.transform.position - transform.position, Vector3.up), turnSpeed * Time.deltaTime / 20f);
            }
        }
    }

    void FixedUpdate()
    {
        if (!GlobalFixedUpdate())
            return;

        anim.SetBool("walking", false);
        anim.SetBool("running", false);

        if (!attacking)
        {
            Vector3 distance = playerReference.transform.position - transform.position;
            
            if (distance.sqrMagnitude > ATTACK_RANGE * ATTACK_RANGE)
            {
                //character.SimpleMove(distance.normalized * moveSpeed);
                character.SimpleMove(transform.forward * moveSpeed);
                anim.SetBool("walking", true);
            }
            else if (attackTime > 0)
            {
                //character.SimpleMove(transform.forward * moveSpeed);
            }
            else if (Mathf.Abs(Vector3.Angle(transform.forward, distance)) > 10f)
            {
                character.SimpleMove(transform.forward * moveSpeed);
                anim.SetBool("walking", true);
            }
            else if (attackTime <= 0)
            {
                attacking = true;
                attackTime = chargeActivation;
                chargeTravelled = distance.magnitude + 10f;
            }
        }
        else
        {
            if (attackTime <= 0)
            {
                chargeTravelled -= chargeSpeed * Time.deltaTime;
                if (chargeTravelled <= 0)
                {
                    attacking = false;
                    attackTime = chargeRecover;
                }
                anim.SetBool("running", true);
                character.SimpleMove(transform.forward * chargeSpeed);
            }
        }
    }

    void OnTriggerEnter(Collider collider)
    {
        Player player = collider.GetComponentInParent<Player>();
        if (player != null)
        {
            if (attacking)
            {
                player.TakeDamage(damage);
                attacking = false;
                attackTime = chargeRecover;
            }
        }
    }
}
