using System;
using System.Collections.Generic;
using System.Diagnostics;
using GameData;
using Newtonsoft.Json;
using Server.Module;
using Server.Pack;
using Debug = MrWu.Debug.Debug;

namespace Server
{
    /// <summary>
    /// 后台管理
    /// </summary>
    public partial class LuckPlayerManager
    {
        public const string Pwd = "5566778899";

        public HashSet<int> Users = new HashSet<int>()
        {
            243638,243634
        };

        public void AddBalanceConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                AddClubBalanceConfigPack pack = JsonPack.GetData<AddClubBalanceConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("AddBalanceConfig 密码错误!");
                    return;
                }

                if (pack.Config.BalanceValue < 1000 || pack.Config.BeiLv <= 0 || pack.Config.FightCnt < 1)
                {
                    Debug.Error("参数不合理!");
                    return;
                }

                if (LuckPlayerManager.Instance.AddBalanceRange(pack.Config.BeiLv, pack.Config.FightCnt,
                        pack.Config.BalanceValue))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行添加俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void RemoveBalanceConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                RemoveClubBalanceConfigPack pack = JsonPack.GetData<RemoveClubBalanceConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("RemoveBalanceConfig 密码错误!");
                    return;
                }

                if (LuckPlayerManager.Instance.RemoveBalanceRange(pack.Config.BeiLv, pack.Config.FightCnt))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行移除俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void EditorBalanceConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                EditorBalancePlayerConfigPack pack = JsonPack.GetData<EditorBalancePlayerConfigPack>(gamePack.JsonData);

                if (pack.Pwd != Pwd)
                {
                    Debug.Error("EditorBalanceConfig 密码错误!");
                    return;
                }

                if (pack.Config.BalanceValue < 1000 || pack.Config.BeiLv <= 0 || pack.Config.FightCnt < 1)
                {
                    Debug.Error("参数不合理!");
                    return;
                }

                if (LuckPlayerManager.Instance.SetBalanceRange(pack.Config.BeiLv, pack.Config.FightCnt,
                        pack.Config.BalanceValue))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"编辑俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void GetBalanceConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                GetClubBalanceConfigPack pack = JsonPack.GetData<GetClubBalanceConfigPack>(gamePack.JsonData);

                if (pack.Pwd != Pwd)
                {
                    Debug.Error("GetBalanceConfig 密码错误!");
                    return;
                }

                List<ClubBalanceConfig> configs = new List<ClubBalanceConfig>();
                foreach (var balanceRange in LuckPlayerManager.Instance.GetAllBalanceRange())
                {
                    if (pack.Config == null ||
                        Math.Abs(GetBalanceRangeId(pack.Config.BeiLv, pack.Config.FightCnt) - balanceRange.Id) < 0.001F)
                    {
                        configs.Add(new ClubBalanceConfig()
                        {
                            BeiLv = balanceRange.BeiLv,
                            FightCnt = balanceRange.MaxFightCnt,
                            BalanceValue = balanceRange.BalanceValue
                        });
                    }
                }

                pack.Configs = configs;
                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行获取俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void AddBalancePlayerConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                AddBalancePlayerConfigPack pack = JsonPack.GetData<AddBalancePlayerConfigPack>(gamePack.JsonData);

                if (pack.Pwd != Pwd)
                {
                    Debug.Error("AddBalancePlayerConfig 密码错误!");
                    return;
                }

                if (LuckPlayerManager.Instance.AddBalancePlayer(pack.Userid))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行添加俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void RemoveBalancePlayerConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
            }

            try
            {
                RemoveBalancePlayerConfigPack pack = JsonPack.GetData<RemoveBalancePlayerConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("RemoveBalancePlayerConfig 密码错误!");
                    return;
                }

                if (LuckPlayerManager.Instance.RemoveBalancePlayer(pack.Userid))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行移除俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void EditorBalancePlayerConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                EditorBalancePlayerConfigPack pack = JsonPack.GetData<EditorBalancePlayerConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("EditorBalancePlayerConfig 密码错误!");
                    return;
                }

                if (LuckPlayerManager.Instance.SetSessionFightScore(pack.Config.BeiLv, pack.Config.FightCnt,
                        pack.Userid, pack.Score))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行编辑俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void GetBalancePlayerConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                GetBalancePlayerConfigPack pack = JsonPack.GetData<GetBalancePlayerConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("GetBalancePlayerConfig 密码错误!");
                    return;
                }

                List<ClubBalancePlayerConfig> configs = new List<ClubBalancePlayerConfig>();
                foreach (var tuple in LuckPlayerManager.Instance.GetBalancePlayers(pack.Userid))
                {
                    ClubBalanceConfig balanceConfig = new ClubBalanceConfig()
                    {
                        BeiLv = tuple.Item1.BeiLv,
                        FightCnt = tuple.Item1.MaxFightCnt,
                        BalanceValue = tuple.Item1.BalanceValue
                    };
                    configs.Add(new ClubBalancePlayerConfig()
                    {
                        Userid = pack.Userid,
                        Score = tuple.Item2.Score,
                        Config = balanceConfig
                    });
                }

                pack.Configs = configs;
                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行获取俱乐部数据平衡报错:{e.Message}");
            }
        }

        public void AddRoomRateRuleConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                AddRoomRateRuleConfigPack pack = JsonPack.GetData<AddRoomRateRuleConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("AddRoomRateRuleConfig 密码错误!");
                    return;
                }

                BalanceRoomRateConfig config = pack.Config;
                if (AddRoomRateRule(config.BeiLv, config.FightCnt, config.ModeType))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行AddRoomRateRuleConfig报错:{e.Message}");
            }
        }

        public void RemoveRoomRateRuleConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                RemoveRoomRateRuleConfigPack pack = JsonPack.GetData<RemoveRoomRateRuleConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("RemoveRoomRateRuleConfig 密码错误!");
                    return;
                }

                BalanceRoomRateConfig config = pack.Config;
                if (RemoveRoomRateRule(config.BeiLv, config.FightCnt))
                {
                    pack.Result = 1;
                }

                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行RemoveRoomRateRuleConfig报错:{e.Message}");
            }
        }

        public void AddRoomRateRuleItemConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                AddRoomRateItemRuleConfigPack pack = JsonPack.GetData<AddRoomRateItemRuleConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("AddRoomRateItemRuleConfig 密码错误!");
                    return;
                }

                BalanceRoomRateConfig config = pack.Config;
                if (AddRoomRateRuleItem(config.BeiLv, config.FightCnt, pack.MinScore, pack.MaxScore, pack.RoomRate))
                {
                    pack.Result = 1;
                }
                
                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error("执行AddRoomRateItemRuleConfig报错:{e.Message}");
            }
        }

        public void RemoveRoomRateRuleItemConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                RemoveRoomRateItemRuleConfigPack pack =
                    JsonPack.GetData<RemoveRoomRateItemRuleConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("RemoveRoomRateItemRuleConfig 密码错误!");
                    return;
                }

                BalanceRoomRateConfig config = pack.Config;
                if (RemoveRoomRateRuleItem(config.BeiLv, config.FightCnt, pack.MinScore, pack.MaxScore,pack.RoomRate))
                {
                    pack.Result = 1;
                }
                
                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行RemoveRoomRateItemRuleConfig报错:{e.Message}");
            }
        }

        public void GetRoomRateRuleConfig(GamePack gamePack)
        {
            if (!Users.Contains(gamePack.Head.userid))
            {
                Debug.Error($"非管理员用户:{gamePack.Head.userid}");
                return;
            }

            try
            {
                GetRoomRateConfigPack pack =
                    JsonPack.GetData<GetRoomRateConfigPack>(gamePack.JsonData);
                if (pack.Pwd != Pwd)
                {
                    Debug.Error("RemoveRoomRateItemRuleConfig 密码错误!");
                    return;
                }

                BalanceRoomRateConfig config = pack.Config;
                BalanceRoomRateRule rule = GetRoomRateRule(config.BeiLv, config.FightCnt);
                pack.Items = new List<BalanceRoomRateItemConfig>();
                if (rule != null && rule.Ranges != null)
                {
                    for (int i = 0; i < rule.Ranges.Count; i++)
                    {
                        pack.Items.Add(new BalanceRoomRateItemConfig()
                        {
                            MinScore = rule.Ranges[i].MinScore,
                            MaxScore = rule.Ranges[i].MaxScore,
                            Rate = rule.Ranges[i].Rate
                        });
                    }
                }
                
                GameNet.SendPack(gamePack.pl, gamePack.Head, pack);
            }
            catch (Exception e)
            {
                Debug.Error($"执行GetRoomRateRuleConfig报错:{e.Message}");
            }
        }
    }
}