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

public class BasicEnemyNPCController : NPCControllerBase
{
    [Header("Events:")]
    [SerializeField] protected GameEvent_Float experienceOnDeath;

    protected DropTable dropTable;

    protected override void Awake()
    {
        dropTable = GetComponentInChildren<DropTable>();
        base.Awake();
    }

    protected override void OnDeath()
    {
        photonView.RPC(nameof(RPC_OnDeath), RpcTarget.All, dropTable.CalculateLootDrop());
    }

    [PunRPC]
    protected override void RPC_OnDeath(bool lootDropped)
    {
        if (isDead) return;

        Debug.Log($"{this.gameObject.name} died!");

        isDead = true;

        agent.enabled = false;

        experienceOnDeath.Raise(health.GetMaxValue() * GameConstants.GameBalancing.HealthIntoExperienceMultiplier);

        dropTable.DropLoot(lootDropped);

        animatorController.SetDead();

        if (!photonView.IsMine) return;
    }




    protected override void OnNewTargetIdentifiedAndHasTarget()
    {
        //someone entered sight, npc already has a target
        Debug.Log("New Target available, current target = " + currentTarget.name);
    }

    protected override void OnNewTargetIdentifiedAndNoCurrentTarget()
    {
        //someone entered sight, npc does not have a target yet
        Debug.Log("New Target available, current target = null, updating target with closest");
        UpdateCurrentTarget(GetClosestTarget());
    }

    protected override void OnPossibleTargetLostAndHasTargetAndVision()
    {
        //someone exited sight, npc already has target and vision
    }

    protected override void OnPossibleTargetLostAndHasTargetButNoVision()
    {
        //someone exited sight, npc already has target but no vision of it (possibly his target was the one getting out of sight)
        base.OnPossibleTargetLostAndHasTargetButNoVision();
        UpdateCurrentTarget(GetClosestTarget());
    }

    protected override void OnPossibleTargetLostAndHasNoCurrentTargetButHasAvailableTargets()
    {
        //someone exited sight, npc has no target yet, there are available targets to pick

        UpdateCurrentTarget(GetClosestTarget());
    }

    protected override void OnPossibleTargetLostAndHasNoCurrentTargetAndNoAvailableTargets()
    {
        //someone exited sight, npc has no target yet, there are NO available targets

        PatrolNewPosition();
    }

    protected override void ChasingUpdate()
    {
        base.ChasingUpdate();
    }

    protected override void PatrollingUpdate()
    {
        base.PatrollingUpdate();
    }

    protected override void TryAttack()
    {
        if (waitingForAttackAnimation) return;

        ability = abilityPriorityManager.GetHighestPriorityAvailableAbility();


        if (ability == null)
        {
            Debug.Log("No abilities available for current conditional state");
            SetupAgentStats(currentTarget.transform.position, true);
            SetAgentMoving(true);
            return;
        }

        Debug.Log("Ability with highest priority = " + ability.name);

        waitingForAttackAnimation = true;

        SetAgentMoving(false);

        animatorController.SetTriggerBasedOnAbility(ability.animationType);
    }

    public override void OnAttackAnimationEventTriggered()
    {
        if (!photonView.IsMine) return;

        Debug.Log("Controller on attack animation event triggered, executing ability = " + ability.name);

        if (ability is AreaOfEffectAbility && !((AreaOfEffectAbility)ability).spawnUnderUser)
        {
            ability.Execute(photonView, myTag, currentTarget.transform);
        }
        else
        {
            ability.Execute(photonView, myTag);
        }
        if (ability.cooldown > 0)
            abilityCooldownTracker.StartAbilityCooldown(ability);

        ResetCounterOnAttackPerformed();
        waitingForAttackAnimation = false;
        SetAgentMoving(true);
        Debug.Log("Attack cycle done");
    }
}
