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

namespace FL.Data
{
    public class PartnersService : DataSingleton<PartnersService>
    {
        /// <summary>
        /// 模拟初始化数据
        /// </summary>
        public void Mock()
        {
            var tableIds = new List<int> { 20511, 20521, 20541, 20531, 20561, 20551 };
            var goneUpMap = PartnersData.Instance.GoneUpMap;
            var partnersMap = PartnersData.Instance.PartnersMap;
            var allTables = PartnerTableRepo.GetAll();
            foreach (var table in allTables)
            {
                var entityAttr = new PartnerAttributes()
                {
                    TableId = table.Id,
                    UID = UIDDefine.New(),
                    Name = table.Name,
                    Radius = table.Size * 0.5f,
                };
                entityAttr.Attr.Hp = 500;
                entityAttr.Attr.HpLimit = 500;
                entityAttr.Attr.MoveSpeed = PlayerData.Instance.Attr.MoveSpeed;
                entityAttr.Attr.Attack = Random.Range(5, 10);
                var idx = tableIds.IndexOf(table.Id);
                if (idx >= 0)
                {
                    goneUpMap.Add(entityAttr.UID, idx);
                }
                partnersMap.Add(entityAttr.UID, entityAttr);
            }
        }
        public void RequestPartnerUp(long partnerUID)
        {
            if (PartnersData.Instance.IsReachedLimit)
            {
                EventSingle.Instance.Notify(EventDefine.ShowTips, "随从已经达到出战数量上限.");
                return;
            }
            var goneUpMap = PartnersData.Instance.GoneUpMap;
            for (var i = 0; i < PartnersData.GoneUpLimit; i++)
            {
                if (goneUpMap.Values.Contains(i))
                {
                    continue;
                }
                goneUpMap.Add(partnerUID, i);
                EventSingle.Instance.Notify(EventDefine.PartnerUp, partnerUID);
                break;
            }
        }
        public void RequestPartnerDown(long partnerUID)
        {
            if (!PartnersData.Instance.GoneUpMap.ContainsKey(partnerUID))
            {
                return;
            }
            PartnersData.Instance.GoneUpMap.Remove(partnerUID);
            EventSingle.Instance.Notify(EventDefine.PartnerDown, partnerUID);
        }
        //public void ExchangePartners()
        //{
        //    var all = PartnerTableRepo.GetAll();
        //    var activePartnerIds = PartnersData.Instance.ActiveUIDs;
        //    var addPartners = new List<int>();
        //    foreach (var table in all)
        //    {
        //        if (activePartnerIds.Contains(table.Id))
        //        {
        //            continue;
        //        }
        //        addPartners.Add(table.Id);
        //    }
        //    EventSingle.Instance.Notify(EventDefine.GameMainMapExchangePartner, addPartners);
        //}
    }
}
