using System.Collections;
using System.Collections.Generic;
using DinoFracture;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Events;
using static WaveSystem;

public class BossController : MonoBehaviour, IWaveEnemy
{
    public enum BossState {
        Uninitialized = 0,
        UnSpawnded,
        Alive,
        Dead
    }

    public int state = (int)BossState.Uninitialized;
    private int prevState = (int)BossState.Uninitialized;
    
    public FindMyBones findMyBones;

    public UnityEvent OnBecomeEnemy;
    public event IWaveEnemy.WaveEnemyDelegate OnWaveEnemyDied;

    void Update()
    {
        if (state != prevState) {
            switch (state) {
                case (int)BossState.UnSpawnded:
                    Unspawn();
                    break;
                case (int)BossState.Alive:
                    BecomeBoss();
                    break;
                case (int)BossState.Dead:
                    KillBoss();
                    break;
            }
            prevState = state;
        }
    }

    public void StartWaveEnemy()
    {
        state = (int)BossState.Alive;
    }

    void BecomeBoss()
    {
        findMyBones.ConstructBones();
        OnBecomeEnemy.Invoke();
    }

    public void Die()
    {
        state = (int)BossState.Dead;
    }

    void KillBoss()
    {
        findMyBones.DestopryBones();
        OnWaveEnemyDied?.Invoke(this.gameObject);
        StartCoroutine(UnspawnAfterDelay(10f));
    }

    IEnumerator UnspawnAfterDelay(float delay)
    {
        yield return new WaitForSeconds(delay);
        state = (int)BossState.UnSpawnded;
    }

    void Unspawn()
    {
        // Destroy(this.gameObject);
    }
}
