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

public class AnimationEntity : MonoBehaviour
{
    enum EntityAnimationState
    {
        Idle = 0,
        Walking = 1,
        Attacking = 2,
        Dying = 3,
        Charging = 4
    }
    private EntityAnimationState entityState;

    private Animator _animatorEntity;
    private bool _doSomething = false;
    private bool _isDead = false;
    private bool _isWalking = false;

    void Start()
    {
        AttackSpeedMultiplier = 10;
        SpeedMultiplier = 10;
        _animatorEntity = GetComponentInChildren<Animator>();
    }

    void Update()
    {
        if (!_animatorEntity) return;
        if (_doSomething && _animatorEntity.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1f)
        {
            PlayIdleAnim();
            _doSomething = false;
            _isWalking = false;
        }
        _animatorEntity.speed = GetAnimatorSpeed();
    }

    private float GetAnimatorSpeed()
    {
        return entityState switch
        {
            EntityAnimationState.Attacking => AttackSpeedMultiplier,
            EntityAnimationState.Walking => SpeedMultiplier,
            _ => 1,
        };
    }

    public void PlayIdleAnim()
    {
        if (!_isDead)
        {
            _animatorEntity.speed = 1;
            _animatorEntity.Play("idle", 0, 0f);
            entityState = EntityAnimationState.Idle;
        }
    }

    public void PlayWalkAnim()
    {
        if (!_isDead)
        {
            _animatorEntity.speed = SpeedMultiplier;
            _animatorEntity.Play("walk", 0, 0f);
            entityState = EntityAnimationState.Walking;
            _isWalking = true;
        }
    }

    public void PlayAttackAnim()
    {
        if (!_isDead && _animatorEntity != null)
        {
            _animatorEntity.speed = AttackSpeedMultiplier;
            _animatorEntity.Play("attack", 0, 0f);
            entityState = EntityAnimationState.Attacking;
            _doSomething = true;
        }
    }

    public void ToggleChargeAnim(bool isCharging)
    {
        if (!_isDead && _animatorEntity != null)
        {
            _animatorEntity.speed = AttackSpeedMultiplier;
            _animatorEntity.SetBool("Charging", isCharging);
            if (isCharging)
            {
                entityState = EntityAnimationState.Charging;
            }
            else
            {
                PlayIdleAnim();
            }
        }
    }

    public void PlayDieAnim()
    {
        // Not every entity needs an animator
        if (_animatorEntity != null)
        {
            _animatorEntity.speed = 1;
            _animatorEntity.Play("die", 0, 0f);
        }

        entityState = EntityAnimationState.Dying;
        _doSomething = true;
        _isDead = true;
    }

    //SETTER GETTER
    public bool IsWalking
    {
        get { return _isWalking; }
        set { _isWalking = value; }
    }
    public float AttackSpeedMultiplier
    {
        get; set;
    }
    public float SpeedMultiplier
    {
        get; set;
    }
}
