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

public class CharacterAnimatorController : MonoBehaviour
{
    Animator anim;
    NavMeshAgent agent;
    PhotonView photonView;

    bool isCasting;

    private void Awake()
    {
        anim = GetComponent<Animator>();
        agent = GetComponentInParent<NavMeshAgent>();
        photonView = GetComponentInParent<PhotonView>();
    }

    // Start is called before the first frame update
    void Start()
    {
        if (!photonView.IsMine)
        {
            this.enabled = false;
            return;
        }
        CastBarHandler.Instance.OnCastingStateChanged.AddListener(UpdateIsCastingState);
    }

    // Update is called once per frame
    void Update()
    {
        anim.SetFloat("movementSpeed", agent.velocity.magnitude);
    }

    private void UpdateIsCastingState(bool isCasting)
    {
        this.isCasting = isCasting;
        if (isCasting)
        {
            anim.SetFloat("throwingTime", (1 / CastBarHandler.Instance.currentCastTime));
            SetTriggerBasedOnAbility(CastBarHandler.Instance.currentAbility.animationType);
        }
    }

    private void SetTriggerBasedOnAbility(AbilityAnimationType animationType)
    {
        switch (animationType)
        {
            case AbilityAnimationType.Throw:
                anim.SetTrigger("throw");
                break;
            case AbilityAnimationType.Spell:
                anim.SetTrigger("cast");
                break;
            default:
                break;
        }
    }
}


public enum AbilityAnimationType
{
    Throw,
    Spell
}