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

[RequireComponent(typeof(SpriteRenderer))]
public class Enemy : Entity
{
    public enum EnemyAction
    {
        Attack,
        Heal,
        Nothing
    }

    public List<EnemyAction> PossibleActions;

    private int GetAttack()
    {
        return Stat.ATK;
    }
    
    public void TakeAction(Player player, UnityAction done)
    {
        DecideAction(player, done);
        done.Invoke();
    }

    private void DecideAction(Player player, UnityAction done)
    {
        EnemyAction act = PossibleActions[Random.Range(0, PossibleActions.Count)];
        switch (act)
        {
            case EnemyAction.Attack:
            {
                Attack(player, done);
                break;
            }
            case EnemyAction.Heal:
            {
                Heal(done);
                break;
            }
            case EnemyAction.Nothing:
            {
                DoNothing(done);
                break;
            }
        }
    }
    
    private void Attack(Player player, UnityAction done)
    {
        GameObject attack = Instantiate(BattleManager.Instance.BubbleAttack, BattleManager.Instance.HumanPlayer.transform.position, Quaternion.identity);
        StartCoroutine(BattleManager.Instance.Delay(1, () =>
        {
            Destroy(attack.gameObject);
        }));
        int atk = GetAttack();
        player.DecrementHP(atk);
        BattleManager.Instance.ShowMessage(Name + " attacks " + player.Name + " for " + atk + " damage!", () =>
        {
            done.Invoke();
        });
    }

    private void DoNothing(UnityAction done)
    {
        BattleManager.Instance.ShowMessage(Name + " gets distracted by some floating bubbles!", () =>
        {
            done.Invoke();
        });
    }

    private void Heal(UnityAction done)
    {
        int heal = Mathf.Max(Stat.DEF, 1);
        DecrementHP( -heal);
        BattleManager.Instance.ShowMessage(Name + " is visibly regenerating for " + heal + " health!", () =>
        {
            done.Invoke();
        });
    }
}
