using System;
using Server.Data;
using System.Collections.Generic;
using MrWu.Debug;
using System.IO;
using System.Text;
using MrWu.Basic;
using Server;

namespace Game.Shadow
{
    /// <summary>
    /// 影子池
    /// </summary>
    public class ShadowPool
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            ShadowManager.instance.LoadShadows();
        }

        /// <summary>
        /// 随机获取一个名字
        /// </summary>
        /// <returns></returns>
        public Shadow GetRandomShadow()
        {
            return ShadowManager.instance.GetRandomName();
        }

        /// <summary>
        /// 放入影子
        /// </summary>
        /// <param name="shadow"></param>
        public void PutShadows(Shadow shadow)
        {
            ShadowManager.instance.PutShadows(shadow);
        }

        /// <summary>
        /// 放入影子
        /// </summary>
        /// <param name="sds"></param>
        public void PutShadows(IEnumerable<Shadow> sds)
        {
            ShadowManager.instance.PutShadows(sds);
        }

        /// <summary>
        /// 当玩家上桌的时候 使用
        /// </summary>
        public virtual bool SetPlayerShadow(PlayerDataAsyc pl, int roomid, int roomCnt, RoomManager[] rooms)
        {
            Shadow[] shadows = null;
            PlayerDataAsyc[] sds = null;
            if (pl.shadowsInfo == null)
            {
                //没影子就去取影子
                shadows = ShadowManager.instance.GetShadows(roomCnt);
                sds = new PlayerDataAsyc[roomCnt];
            }
            else
            {
                shadows = pl.shadowsInfo;
                sds = pl.shadows;
            }

            //取新的
            if (shadows == null)
            {
                Debug.Error("未获取到影子!!!!");
                return false;
            }

            int maxMoney = 0;
            bool isNew = false;
            //为影子创建玩家
            for (int i = 0; i < roomCnt; i++)
            {
                isNew = false; //重置状态
                if (sds[i] != null &&
                    (sds[i].money < rooms[i].config.MinMoney || sds[i].money >= rooms[i].config.MaxMoney))
                {
                    //影子的钱不在此房间范围,换影子 
                    ShadowManager.instance.PutShadows(shadows[i]);
                    sds[i] = null;
                    var tmp = ShadowManager.instance.GetShadows(1);
                    if (tmp == null)
                    {
                        Debug.Error("第二次取。。。。未取到影子?????");
                        return false;
                    }

                    shadows[i] = tmp[0];
                }

                if (sds[i] == null)
                {
                    //影子没人
                    sds[i] = new PlayerDataAsyc();
                    sds[i].nickname = shadows[i].name;
                    sds[i].sex = shadows[i].sex;
                    sds[i].avatar = shadows[i].photo;
                    sds[i].area = "江西南昌";
                    isNew = true;
                }


                if (isNew)
                {
                    //新的影子, 重新设置金额
                    maxMoney = rooms[i].config.MaxMoney;
                    if (maxMoney > 1000000000) //最大值大于10亿， 应该是最后一个房间
                    {
                        if (rooms[i].config.MinMoney < 200000000) //如果房间的最小金额小于2亿
                            maxMoney = 200000000; //让鬼的金额不至于太大
                    }

                    //矫正金额
                    sds[i].money = BaseFun.random.Next(rooms[i].config.MinMoney, maxMoney);
                }
            }

            pl.shadows = sds;
            pl.shadowsInfo = shadows;

            return true;
        }

        /// <summary>
        /// 影子管理
        /// </summary>
        protected class ShadowManager
        {
            /// <summary>
            /// 实例
            /// </summary>
            public static ShadowManager instance { get; private set; }

            private ShadowManager()
            {
            }

            static ShadowManager()
            {
                instance = new ShadowManager();
            }

            /// <summary>
            /// 所有的影子
            /// </summary>
            protected readonly List<Shadow> shadows = new List<Shadow>();

            /// <summary>
            /// 影子剩余数量
            /// </summary>
            public int count
            {
                get { return shadows.Count; }
            }

            /// <summary>
            /// 加载影子
            /// </summary>
            public void LoadShadows()
            {
                string[] lines = File.ReadAllLines(TableManager.GetRobotInfoPath(), Encoding.UTF8);

                int len = lines.Length;
                
                //没开金币场
                if (ServerConfigManager.Instance.OpenGold == 0)
                {
                    len = Math.Min(len, 10);
                }
                
                Debug.Info($"读取到替身名字{len}个");

                for (int i = 0; i < len; i++)
                {
                    string[] items = lines[i].Split(',');
                    string phtoto = null;
                    if (items.Length > 1)
                    {
                        phtoto = $"https://cs1.jxhappy.top/GameRes/Avatar/{items[1]}";
                    }
                    Shadow shadow = new Shadow(items[0], phtoto, i % 2);
                    shadows.Add(shadow);
                }
            }

            static int namecnt;

            /// <summary>
            /// 随机一个影子的名字
            /// </summary>
            /// <returns></returns>
            public Shadow GetRandomName()
            {
                int cnt = 10;
                for (int i = 0; i < cnt; i++)
                {
                    int idx = BaseFun.random.Next(0, count);
                    var sd = shadows[idx];
                    if (!sd.isGetName) //名字没被使用过
                    {
                        sd.isGetName = true;
                        return sd;
                    }
                }

                return null;
            }

            /// <summary>
            /// 获取影子
            /// </summary>
            /// <param name="count"></param>
            /// <returns></returns>
            public Shadow[] GetShadows(int count)
            {
                Debug.Log("影子的数量:" + shadows.Count + "," + count);

                if (shadows.Count < count)
                    return null;

                Shadow[] sds = new Shadow[count];

                for (int i = 0; i < count; i++)
                {
                    int idx = BaseFun.random.Next(0, shadows.Count);
                    sds[i] = shadows[idx];
                    shadows.RemoveAt(idx);
                }

                // Debug.Log("剩余的替身数量:" + shadows.Count);
                return sds;
            }

            /// <summary>
            /// 放入影子
            /// </summary>
            /// <param name="sds"></param>
            public void PutShadows(IEnumerable<Shadow> sds)
            {
                shadows.AddRange(sds);
            }

            /// <summary>
            /// 放入影子
            /// </summary>
            /// <param name="shadow"></param>
            public void PutShadows(Shadow shadow)
            {
                shadows.Add(shadow);
            }
        }
    }
}