using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.Json;

namespace ET.Server
{
    /// <summary>
    /// 抖音推送回调, 送礼物
    /// </summary>
    [ActorMessageHandler(SceneType.Game)]
    public class R2G_LiveGiftHandler: AMActorHandler<Scene, R2G_LiveGift>
    {
        protected override async ETTask Run(Scene scene, R2G_LiveGift request)
        {
            // 房间是否存在
            Map map = scene.GetComponent<GameMapComponent>().GetMapByRoomId(request.RoomId);

            if (map == null)
            {
                Log.Error($"未找到房间...roomId={request.RoomId}");
                return;
            }

            Struct.UnitPlayerData unitPlayerData = map.GetUnitPlayerByOpenId(request.OpenId);
            // 数据是否存在
            if (unitPlayerData == null)
            {
                Log.Error($"未找到单位玩家数据...openId={request.OpenId}");
                return;
            }

            unitPlayerData.GiftMoney += (int)request.GiftValue;
            // 推送客户端
            if (map.Player != null)
            {
                MessageHelper.SendToClient(map.Player, new G2C_GiftInfoPush
                {
                    NickName = request.NickName,
                    GiftType = DouyinItem.GiftHash[request.GiftId],
                    GiftNum = (int)request.GiftNum,
                    Url = request.Url,
                    TotalMoney = unitPlayerData.GiftMoney,
                    UnitId = unitPlayerData.ObjId
                });
            }

            Struct.TriggerEventNotify notify = null;
            if (DouyinItem.GiftId_1.Equals(request.GiftId) || DouyinItem.GiftId_10.Equals(request.GiftId))
            {
                // 仙女棒 自己死了复活自己, 自己没死复活其他人, 不够则补充单位
                // 能量药丸 自己死了复活自己, 自己没死复活其他3个人, 不够则补充单位
                List<int> objIds = new List<int>();

                notify = new Struct.TriggerEventNotify();

                long cnt = request.GiftNum * (DouyinItem.GiftId_1.Equals(request.GiftId)? 1 : 3);

                // 先判断自己状态
                if (unitPlayerData.DeadState == 1)
                {
                    objIds.Add(unitPlayerData.ObjId);
                    unitPlayerData.DeadState = 0;
                    cnt -= 1;
                }
                // 其他死亡玩家
                if (cnt > 0)
                {
                    for (int i = 0; i < map.DeadUnitPlayer.Count; i++)
                    {
                        int objId = map.DeadUnitPlayer[i];
                        if (objId > 0 && objId != unitPlayerData.ObjId && !objIds.Exists(o => o == objId))
                        {
                            objIds.Add(objId);
                            map.DeadUnitPlayer.Remove(objId);
                            cnt -= 1;
                        }

                        if (cnt <= 0)
                        {
                            break;
                        }
                    }
                }
                // 死亡玩家不够,则补充活着的玩家
                if (cnt > 0)
                {
                    foreach (Struct.UnitPlayerData unitPlayer in map.UnitPlayers.Values)
                    {
                        if (unitPlayer != null && unitPlayer.ObjId != unitPlayerData.ObjId && !objIds.Exists(o => o == unitPlayer.ObjId))
                        {
                            objIds.Add(unitPlayer.ObjId);
                            cnt -= 1;
                        }
                        if (cnt <= 0)
                        {
                            break;
                        }
                    }
                }

                notify.message = DouyinItem.GiftId_1.Equals(request.GiftId)? BattleNotify.TiktokGift_1.ToString() : BattleNotify.TiktokGift_10.ToString();
                notify.TriggerUnits = "";
                for (int i = 0; i < objIds.Count; i++)
                {
                    int objId = objIds[i];
                    if (objId > 0)
                    {
                        notify.TriggerUnits += objId;
                        if (i < objIds.Count - 1)
                        {
                            notify.TriggerUnits += ",";
                        }
                    }
                }
                map.GetXmdsManager().notifyBattleServer(map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(notify));

                Log.Debug($"============================================仙女棒/能量药丸 notifyBattleServer - data={JsonSerializer.Serialize(notify)}");
            }
            else
            {
                for (int i = 0; i < request.GiftNum; i++)
                {
                    switch (request.GiftId)
                    {
                        case DouyinItem.GiftId_52:
                            // 甜甜圈
                            const long maxLevel = 3; // 等级上限
                            notify = new Struct.TriggerEventNotify();
                            if (unitPlayerData.Level < maxLevel)
                            {
                                ++unitPlayerData.Level;
                                notify.message = BattleNotify.TiktokGift_52.ToString();
                            }
                            else
                            {
                                notify.message = BattleNotify.TiktokGift_52_ext.ToString();
                            }
                            notify.TriggerUnits = unitPlayerData.ObjId.ToString();
                            break;
                        case DouyinItem.GiftId_99:
                            // 能量电池
                            notify = new Struct.TriggerEventNotify();
                            notify.message = BattleNotify.TiktokGift_99.ToString();
                            notify.TriggerUnits = unitPlayerData.ObjId.ToString();
                            // 对应的贡献值
                            map.AddContributeValue(request.OpenId, 99);
                            break;
                        case DouyinItem.GiftId_199:
                            // 恶魔炸弹
                            notify = new Struct.TriggerEventNotify();
                            notify.message = BattleNotify.TiktokGift_199.ToString();
                            notify.TriggerUnits = unitPlayerData.ObjId.ToString();
                            // 对应的贡献值
                            map.AddContributeValue(request.OpenId, 199);
                            break;
                        case DouyinItem.GiftId_520:
                            // 神秘空投
                            notify = new Struct.TriggerEventNotify();
                            notify.message = BattleNotify.TiktokGift_520.ToString();
                            notify.TriggerUnits = unitPlayerData.ObjId.ToString();
                            // 对应的贡献值
                            map.AddContributeValue(request.OpenId, 520);
                            break;
                    }

                    map.GetXmdsManager().notifyBattleServer(map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(notify));
                    //间隔30ms,触发一次礼物效果
                    await TimerComponent.Instance.WaitAsync(30);
                }
            }
        }
    }
}