using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Sirenix.OdinInspector;
using UDB;
using GameCreator.Runtime.Behavior;
using static WaveSystem;
using DinoFracture;

public class SpiderEnemy : CachedMonoBehaviour, IWaveEnemy
{
    public enum SpiderState {
        Uninitialized = 0,
        UnSpawnded,
        Alive,
        Dead
    }

    public int state = (int)SpiderState.Uninitialized;
    private int prevState = (int)SpiderState.Uninitialized;

    public Transform attachedModel;


    private bool _isAuthoritative = false;
    public bool isAuthoritative {
        get {
            return _isAuthoritative;
        }
        set {
            _isAuthoritative = value;
        }
    }

    public new MeshFilter meshFilter;
    [SerializeField] private Processor _processor;
    public Processor processor {
        get {
            if(_processor == null) {
                _processor = GetComponent<Processor>();
            }
            return _processor;
        }
    }

    [SerializeField] private GooglyEyeAdder _googlyEyeAdder;
    public GooglyEyeAdder googlyEyeAdder {
        get {
            if(_googlyEyeAdder == null) {
                _googlyEyeAdder = GetComponent<GooglyEyeAdder>();
            }
            return _googlyEyeAdder;
        }
    }
    public UnityEvent OnBecomeSpider;
    public UnityEvent OnBecomeSpiderAuthoriative;

    public event IWaveEnemy.WaveEnemyDelegate OnWaveEnemyDied;

    void Update()
    {
        if (state != prevState) {
            switch (state) {
                case (int)SpiderState.UnSpawnded:
                    UnspawnSpider();
                    break;
                case (int)SpiderState.Alive:
                    BecomeSpider();
                    break;
                case (int)SpiderState.Dead:
                    KillSpider();
                    break;
            }
            prevState = state;
        }
    }

    [Button]
    public void ParentSpider(Transform attachedModel)
    {
        this.attachedModel = attachedModel;
    }

    [Button]
    public void BecomeSpider()
    {
        if (meshFilter == null) {
            meshFilter = attachedModel.GetComponent<MeshFilter>();
        }
        OnBecomeSpider.Invoke();
        if (isAuthoritative) {
            OnBecomeSpiderAuthoriative.Invoke();
        }
        googlyEyeAdder.AddEyes(meshFilter);

        attachedModel.tag = "BodyShot";
        attachedModel.gameObject.layer = 7;//Enemy Layer
    }

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

    public void OnDeath()
    {
        state = (int)SpiderState.Dead;
    }

    void KillSpider()
    {
        FractureGeometry fracture = attachedModel.GetComponent<FractureGeometry>();
        if (fracture != null) {
            try {
                fracture.FractureAndForget();
            } catch (System.Exception e) {
                Debug.Log(e);
            }
        }
        OnWaveEnemyDied?.Invoke(this.gameObject);
        StartCoroutine(UnspawnSpiderAfterDelay(10f));
    }

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

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