using System;
using UnityEngine;

namespace FL.Battle
{
    public abstract class Entity : IEntity, IDisposable
    {
        public int TableId => Attr.TableId;
        public long EntityId { get; private set; }
        public EntityAttributes Attr { get; private set; }
        public int[] ActiveSkillIds { get; private set; }
        public int[] PassiveSkillIds { get; private set; }

        /// <summary>
        /// 出生坐标
        /// 相对于地图根节点的坐标
        /// </summary>
        public Vector3 BirthPosition { get; set; }

        //public EEntityState State { get; set; }
        public bool IsIdle => _states == 0;
        public bool IsDead => IsState(EEntityState.Dead);
        private EEntityState _states = 0;

        public bool AddState(EEntityState state)
        {
            var last = _states;
            _states |= state;
            return last != _states;
        }
        public bool RemoveState(EEntityState state)
        {
            var last = _states;
            _states &= ~state;
            return last != _states;
        }
        public bool IsState(EEntityState state)
        {
            return (_states & state) != 0;
        }

        public void Init(long entityId, EntityAttributes attr, int[] activeSkillIds, int[] passiveSkillIds = null)
        {
            EntityId = entityId;
            Attr = attr;
            ActiveSkillIds = activeSkillIds;
            PassiveSkillIds = passiveSkillIds;
            _states = 0;
        }
        void IDisposable.Dispose()
        {
            //_states = 0;
            OnDispose();
            (Attr as IDisposable)?.Dispose();
        }

        public abstract EEntityType EntityType { get; }

        protected virtual void OnDispose()
        {
        }
    }
}
