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

namespace DuckHunter
{
    public class DogSide : MonoBehaviour
    {
        public static DogSide Instance;
        DragonBones.UnityArmatureComponent armature;
        public Action onExit;

        void Start()
        {
            Instance = this;
            armature = GetComponentInChildren<DragonBones.UnityArmatureComponent>();
            armature.AddEventListener(DragonBones.EventObject.FRAME_EVENT, OnAnimationEventHandler);
            armature.AddEventListener(DragonBones.EventObject.COMPLETE, OnAnimationEventHandler);
            armature.animation.Play("enter", 1);
        }

        private void OnAnimationEventHandler(string type, DragonBones.EventObject eventObject)
        {
            if (eventObject.type == DragonBones.EventObject.FRAME_EVENT)
            {
                if (eventObject.name == "layer")
                    transform.SetSiblingIndex(transform.parent.Find("BGFront").GetSiblingIndex());
            }
            else if (eventObject.type == DragonBones.EventObject.COMPLETE)
            {
                Destroy(gameObject);
                onExit?.Invoke();
            }
            // Debug.Log(string.Format("animationName:{0},eventType:{1},eventName:{2}", eventObject.animationState.name, type, eventObject.name));
        }

        void OnDestroy()
        {
            if (Instance == this) Instance = null;
        }
    }
}
