using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;

namespace NB.Game;

public sealed class ItemContainerDestroySystem : DestroySystem<PlayerItemContainerComponent>
{
    protected override void Destroy(PlayerItemContainerComponent self)
    {
        self.CellCountMax = 0;

        foreach (var (_, item) in self.Items)
        {
            item.Dispose();
        }

        self.Items.Clear();
        // self.ItemsByConfigId.Clear();
        // self.ItemsByType.Clear();
    }
}

public static class PlayerItemContainerComponentSystem
{
    #region Save

    public static async FTask Save(this PlayerItemContainerComponent self)
    {
        await self.Scene.World.DataBase.Save(self);
    }

    #endregion

    #region Get

    /// <summary>
    /// 通过唯一id获取
    /// </summary>
    /// <param name="self"></param>
    /// <param name="id"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public static bool GetItemById(this PlayerItemContainerComponent self, long id, out Item? item)
    {
        return self.Items.TryGetValue(id, out item);
    }

    /// <summary>
    /// 通过配置id获取物品
    /// </summary>
    /// <param name="self"></param>
    /// <param name="configId"></param>
    /// <param name="item"></param>
    public static bool GetFistItemByConfigId(this PlayerItemContainerComponent self, int configId, out Item? item)
    {
        foreach (var (_, it) in self.Items)
        {
            if (it.ConfigId == configId)
            {
                item = it;
                return true;
            }
        }

        item = null;
        return false;
        // if (!self.ItemsByConfigId.TryGetValue(configId, out var itemList))
        // {
        //     item = null;
        //     return;
        // }
        //
        // item = itemList.First();
    }

    // /// <summary>
    // /// 通过配置id获取物品
    // /// </summary>
    // /// <param name="self"></param>
    // /// <param name="configId"></param>
    // /// <param name="items"></param>
    // public static void GetItemByConfigId(this ItemContainer self, uint configId, List<Item> items)
    // {
    //     if (!self.ItemsByConfigId.TryGetValue(configId, out var itemList))
    //     {
    //         return;
    //     }
    //
    //     items.AddRange(itemList);
    // }

    // /// <summary>
    // /// 通过类型获取物品
    // /// </summary>
    // /// <param name="self"></param>
    // /// <param name="type"></param>
    // /// <param name="items"></param>
    // public static void GetItemByType(this ItemContainer self, ItemType type, List<Item> items)
    // {
    //     if (!self.ItemsByType.TryGetValue((uint)type, out var itemList))
    //     {
    //         return;
    //     }
    //
    //     items.AddRange(itemList);
    // }

    #endregion

    #region Add

    public static async FTask Add(this PlayerItemContainerComponent self, Dictionary<uint, int> items)
    {
        foreach (var (configId, count) in items)
        {
            await self.Add(configId, count, false);
        }

        await self.Save();
    }

    public static async FTask Add(this PlayerItemContainerComponent self, uint configId, int count, bool needSave = true)
    {
        var item = ItemFactory.Create(self.Scene, configId, count);
        self.Items.Add(item.Id, item);
        if (needSave)
        {
            await self.Save();
        }
    }

    #endregion

    #region 结构转换

    public static List<ItemInfo> GetItemInfos(this PlayerItemContainerComponent self)
    {
        List<ItemInfo> ret = new List<ItemInfo>();
        foreach (var (_, item) in self.Items)
        {
            ret.Add(item.ToItemInfo());
        }

        return ret;
    }

    public static List<GearInfo> GetGearInfos(this PlayerItemContainerComponent self)
    {
        List<GearInfo> ret = new List<GearInfo>();

        foreach (var (rod, rigs) in self.FishingRig)
        {
            GearInfo gearInfo = new GearInfo();
            gearInfo.Rod = rod;
            gearInfo.Rigs.AddRange(rigs);
            ret.Add(gearInfo);
        }

        return ret;
    }

    #endregion
}