using System.Net.Http.Headers;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using Fantasy.Entitas.Interface;
using Fantasy.Helper;

#pragma warning disable CS8625 // 无法将 null 字面量转换为非 null 的引用类型。

namespace NB.Game;

public sealed class PlayerDestroySystem : DestroySystem<Player>
{
    protected override void Destroy(Player self)
    {
        self.NickName = string.Empty;
        self.Level = 0;
        self.Exp = 0;
        self.Country = string.Empty;
        self.Head = string.Empty;

        // self.ItemContainer.Dispose();
        // self.ItemContainer = null;
        // self.FishContainer.Dispose();
        // self.FishContainer = null;
        // self.Wallet.Dispose();
        // self.Wallet = null;
        // self.Vip.Dispose();
        // self.Vip = null;
        // self.Statistics.Dispose();
        // self.Statistics = null;

        self.SessionRunTimeId = 0;
    }
}

public static class PlayerSystem
{
    #region 物品

    /// <summary>
    /// 添加物品
    /// </summary>
    /// <param name="self"></param>
    /// <param name="id"></param>
    /// <param name="items"></param>
    public static async FTask AddItems(this Player self, Dictionary<uint, int> items)
    {

        var itemContainer = self.GetComponent<PlayerItemContainerComponent>();
        HashSet<ItemBasicType> addType = new HashSet<ItemBasicType>();
        foreach (var (key, value) in items)
        {
            var itemType = ItemHelper.GetType(key);
            switch (itemType)
            {
                case ItemBasicType.Item:
                    itemContainer.Add(key, value);
                    addType.Add(ItemBasicType.Item);
                    break;
            }
        }
    }

    #endregion

    #region 测试数据

    public static void SetTestData(this Player player)
    {

        Dictionary<int, List<int>> rigDic = new Dictionary<int, List<int>>()
        {
            { 30001, [50001, 60001, 70002, 90001, 100001] },
            { 30002, [40001, 50001, 60001, 70002, 00001, 100001] },
            { 30003, [40001, 60001, 80001] }
        };

        var itemContainer = player.GetComponent<PlayerItemContainerComponent>();
        
        //添加测试数据
        if (itemContainer != null && itemContainer.FishingRig.Count < 1)
        {
            foreach (var (rod, list) in rigDic)
            {
                if (itemContainer.GetFistItemByConfigId(rod, out var item) && item != null)
                {
                    var childs = new List<long>();
                    foreach (var i in list)
                    {
                        if (itemContainer.GetFistItemByConfigId(i, out var itemChild) && itemChild != null)
                        {
                            childs.Add(itemChild.Id);
                        }
                    }
        
                    itemContainer.FishingRig[item.Id] = childs;
                }
            }

            itemContainer.Save();
        }

    }

    #endregion
}