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

public class BossAbilityBinder : MonoBehaviour
{
    [SerializeField] protected BaseAbility meleeAbility;
    [SerializeField] protected BaseAbility coreAbility;
    [SerializeField] protected BaseAbility ultimateAbility;
    [SerializeField] protected float ultimateCooldown;

    protected PhotonView user;
    protected Taggable userTag;
    protected Mana mana;

    protected Coroutine ultimateCooldownCoroutine;
    protected bool ultimateOnCooldown = true;

    protected virtual void Awake()
    {
        user = GetComponentInParent<PhotonView>();
        userTag = GetComponentInParent<Taggable>();
        mana = GetComponentInParent<Mana>();
    }

    private void Start()
    {
        if (!user.IsMine)
        {
            this.enabled = false;
            return;
        }

        ultimateCooldownCoroutine = StartCoroutine(UltimateCooldownTimerCoroutine());
    }

    public virtual void UseMeleeAbility()
    {
        if (meleeAbility == null) return;

        if (mana.EnoughMana(meleeAbility.manaCost))
            meleeAbility.Execute(user, userTag);
    }

    public virtual void UseCoreAbility(Vector3 point)
    {
        if (coreAbility == null) return;

        if (mana.EnoughMana(coreAbility.manaCost))
            coreAbility.Execute(user, userTag, point);
    }

    public virtual void UseUltimateAbility(Vector3 point)
    {
        if (ultimateAbility == null) return;

        if (mana.EnoughMana(ultimateAbility.manaCost))
        {
            ultimateAbility.Execute(user, userTag, point);

            ultimateCooldownCoroutine = StartCoroutine(UltimateCooldownTimerCoroutine());
        }
    }
    public virtual void UseUltimateAbility(Transform target)
    {
        if (ultimateAbility == null) return;

        if (mana.EnoughMana(ultimateAbility.manaCost))
        {
            ultimateAbility.Execute(user, userTag, target);

            ultimateCooldownCoroutine = StartCoroutine(UltimateCooldownTimerCoroutine());
        }
    }

    public bool ValidCoreAbility()
    {
        return coreAbility != null;
    }

    public virtual bool CanCastUltimate()
    {
        return (ultimateCooldownCoroutine == null && mana.EnoughMana(ultimateAbility.manaCost) && !ultimateOnCooldown);
    }

    public virtual bool CanCastCoreAbility()
    {
        return mana.EnoughMana(coreAbility.manaCost);
    }

    protected virtual IEnumerator UltimateCooldownTimerCoroutine()
    {
        ultimateOnCooldown = true;

        yield return new WaitForSeconds(ultimateCooldown);

        OnUltimateCooldownReset();
    }
    protected virtual void OnUltimateCooldownReset()
    {
        ultimateCooldownCoroutine = null;
        ultimateOnCooldown = false;
    }
}
