using System;
using System.Collections.Generic;
using UnityEngine;
using XGame.Framework.Data;

namespace FL.Data
{
    public class PartnersData : DataSingleton<PartnersData>, IDisposable
    {
        /// <summary>
        /// 激活上限
        /// </summary>
        public const byte GoneUpLimit = 6;

        private Dictionary<long, int> _goneUpMap;
        /// <summary>
        /// 已上阵的随从->位置
        /// key:随从的uid
        /// value:位置索引(0-5)
        /// </summary>
        public Dictionary<long, int> GoneUpMap => _goneUpMap ??= new Dictionary<long, int>();

        private Dictionary<long, PartnerAttributes> _partnersMap;
        /// <summary>
        /// 拥有的所有随从
        /// </summary>
        public Dictionary<long, PartnerAttributes> PartnersMap => _partnersMap ??= new Dictionary<long, PartnerAttributes>();
        /// <summary>
        /// 剑的偏移值，只记录左边的，右边对称获取
        /// </summary>
        private Vector3[] _partnerOffsets = new Vector3[] { new Vector3(-4.5f, 1.5f, 0), new Vector3(-3.5f, 0.6f, 0), new Vector3(-2.5f, -0.3f, 0) };

        public Vector3 GetPartnerOffset(long partnerUID)
        {
            var index = GoneUpMap[partnerUID];
            var offset = _partnerOffsets[index % 3];
            if (index >= 3)
            {
                offset.x *= -1;
            }
            return offset;
        }
        /// <summary>
        /// 是否达到激活上限
        /// </summary>
        public bool IsReachedLimit => GoneUpMap.Count == GoneUpLimit;
        void IDisposable.Dispose()
        {
            _goneUpMap?.Clear();
            if (_partnersMap != null)
            {
                foreach (var val in _partnersMap.Values)
                {
                    (val as IDisposable)?.Dispose();
                }
                _partnersMap.Clear();
            }
        }
    }
}
