using System.Collections.Generic;
using System.Linq;
using Sirenix.OdinInspector;
using UnityEngine;
using VOID.Generic.Objects.Abstract;
using VOID.Generic.Objects.Door.Events;
using VOID.Generic.Objects.Unit;

namespace VOID.Generic.Objects.Door
{
    [RequireComponent(typeof(Animator))]
    public class DoorObject : AbstractObject
    {
        [Title("=== Door properties ===")]
        public DoorObjectEvents doorEvents;
        public DoorObjectState doorState;
        public DoorObjectAnimator doorAnimator;

        protected new void OnValidate()
        {
            base.OnValidate();
            gameObject.GetComponent<Animator>().hideFlags = HideFlags.NotEditable;
        }

        protected override List<ObjectComponent> GetObjectComponents()
        {
            return base.GetObjectComponents()
                .Union(new List<ObjectComponent>
                {
                    doorEvents,
                    doorState,
                    doorAnimator
                })
                .ToList();
        }

        [Button]
        public void Open(UnitObject unit = null)
        {
            if (doorState.isLocked) return;
            
            doorEvents.onOpen.Invoke(new DoorEvent
            {
                door = this,
                unit = unit
            });
        }

        [Button]
        public void Close(UnitObject unit = null)
        {
            doorEvents.onClose.Invoke(new DoorEvent
            {
                door = this,
                unit = unit
            });
        }

        [Button]
        public void Lock(UnitObject unit = null)
        {
            if (doorState.isOpen) return;
            
            doorEvents.onLock.Invoke(new DoorEvent
            {
                door = this,
                unit = unit
            });
        }

        [Button]
        public void Unlock(UnitObject unit = null)
        {
            doorEvents.onUnlock.Invoke(new DoorEvent
            {
                door = this,
                unit = unit
            });
        }
    }
}
