using System.Diagnostics;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas.Interface;
using Fantasy.Helper;

namespace NB.Game;

public class PlayerBasicCacheManageComponentAwakeSystem : AwakeSystem<PlayerBasicCacheManageComponent>
{
    protected override void Awake(PlayerBasicCacheManageComponent self)
    {
        // var timerId = self.Scene.TimerComponent.Net.RepeatedTimer(1000 * 60, new TestEvent());
    }
}

public class PlayerBasicCacheManageComponentDestroySystem : DestroySystem<PlayerBasicCacheManageComponent>
{
    protected override void Destroy(PlayerBasicCacheManageComponent self)
    {
        foreach (var (_, player) in self.Players)
        {
            player.Dispose();
        }

        self.Players.Clear();
    }
}

public static class PlayerBasicCacheManageComponentSystem
{
    /// <summary>
    /// 获取一组玩家基本信息
    /// </summary>
    /// <param name="manage"></param>
    /// <param name="list"></param>
    /// <returns></returns>
    public static async FTask<List<RoleSimpleInfo>> GetPlayerBasicCacheInfos(
        this PlayerBasicCacheManageComponent manage,
        List<long> list)
    {
        var ret = new List<RoleSimpleInfo>();
        var notCacheIdList = new List<long>();
        foreach (var id in list)
        {
            var info = GetPlayerBasicCacheInfos(manage, id);
            if (info != null)
            {
                ret.Add(info);
            }
            else
            {
                notCacheIdList.Add(id);
            }
        }

        if (notCacheIdList.Count > 0)
        {
            //查数据库
            var dbDataList = await LoadPlayerBasicCacheByDB(manage, notCacheIdList);
            foreach (var playerBasicCache in dbDataList)
            {
                ret.Add(playerBasicCache.ToInfo());
            }
        }


        return ret;
    }

    /// <summary>
    /// 获取缓存的单个玩家基本信息
    /// </summary>
    /// <param name="manage"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public static RoleSimpleInfo? GetPlayerBasicCacheInfos(this PlayerBasicCacheManageComponent manage,
        long id)
    {
        if (manage.Players.TryGetValue(id, out var cache))
        {
            return cache.ToInfo();
        }

        return null;
    }

    /// <summary>
    /// 从数据库中加载玩家基本信息
    /// </summary>
    /// <param name="manage"></param>
    /// <param name="ids"></param>
    /// <returns></returns>
    public static async FTask<List<PlayerBasicCache>> LoadPlayerBasicCacheByDB(
        this PlayerBasicCacheManageComponent manage,
        List<long> ids)
    {
        // Stopwatch stopwatch = new Stopwatch();
        // stopwatch.Start();
        var ret = new List<PlayerBasicCache>();
        //TODO:需要考虑如果数量过大是否需要分页
        List<Player> players =
            await manage.Scene.World.DataBase.QueryByPage<Player>(d => ids.Contains(d.Id), 1, ids.Count);
        foreach (var player in players)
        {
            var cache = manage.UpdateCache(player);
            ret.Add(cache);
        }
        
        return ret;
    }

    public static PlayerBasicCache UpdateCache(this PlayerBasicCacheManageComponent manage, Player player)
    {
        if (!manage.Players.TryGetValue(player.Id, out var cache))
        {
            cache = PlayerBasicCacheFactory.Create(manage.Scene, player.Id);
            manage.Players.Add(player.Id, cache);
        }

        cache.NickName = player.NickName;
        cache.Country = player.Country;
        cache.Head = player.Head;
        cache.Level = player.Level;
        cache.Vip = player.Vip;
        cache.ExpirationTime = TimeHelper.Now + TimeHelper.OneDay; //更新则过期时间增加一天

        return cache;
    }
}