using CommonLang;
using CommonLang.Property;
using CommonServer.Command;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XmdsServerTestBots
{
    public class BotConsoleCommand : CommandList<BotConsoleCommand.Cmd>
    {
        public static BotConsoleCommand Instance { get; private set; }

        public BotConsoleCommand()
        {
            Instance = this;
        }

        public void Run()
        {
            Console.WriteLine("使用cmdlist列出所有指令");
            while (BotRunner.Instance.IsRunning)
            {
                string input = Console.In.ReadLine();
                if (!DoCommand(input, Console.Out))
                {
                    Console.WriteLine();
                    Console.WriteLine("未知的命令:" + input);
                    Console.WriteLine("使用cmdlist列出所有指令");
                }
                else
                {
                    Console.WriteLine();
                }
            }
        }

        // -------------------------------------------------------------------------------------------------


        public abstract class Cmd : AbstractCommand
        {
        }

        [DescAttribute("列出所有命令")]
        public class CMD_LIST : Cmd
        {
            public override string Key { get { return "cmdlist"; } }
            public override string Help
            {
                get
                {
                    return
                        "列出所有命令\n" +
                        "  cmdlist 列出所有控制台命令\n" +
                        "  cmdlist [前缀] 列出所有前缀相符的控制台命令";
                }
            }
            public override void DoCommand(string arg, TextWriter output)
            {
                output.WriteLine(BotConsoleCommand.Instance.ListCommand(arg));
            }
        }

        [DescAttribute("关闭")]
        public class CMD_EXIT : Cmd
        {
            public override string Key { get { return "exit"; } }
            public override void DoCommand(string arg, TextWriter output)
            {
                BotRunner.Instance.Shutdown();
            }
        }


        [DescAttribute("清理内存")]
        public class CMD_GC : Cmd
        {
            public override string Key { get { return "gc"; } }
            public override void DoCommand(string arg, TextWriter output)
            {
                GC.Collect();
                output.WriteLine(string.Format("HEAP = {0}", CUtils.ToBytesSizeString(GC.GetTotalMemory(false))));
            }
        }

        //---------------------------------------------------------------------------------------------------
        [DescAttribute("添加机器人", "机器人")]
        public class CMD_AddBots : Cmd
        {
            public override string Key { get { return "add"; } }
            public override string Help { get { return "  add <数量> <阵营> <TemplateID>"; } }
            public override void DoCommand(string arg, TextWriter output)
            {
                int count = 1;
                int force = 2;
                List<int> templates = null;
                var args = ToArgs(arg);
                if (args.Length >= 1)
                {
                    count = int.Parse(args[0]);
                }
                if (args.Length >= 2)
                {
                    force = int.Parse(args[1]);
                }
                if (args.Length >= 3)
                {
                    Parser.StringToObject(args[2], out templates);
                }
                BotRunner.Instance.AddBots(count, force, templates);
            }
        }
        [DescAttribute("清理机器人", "机器人")]
        public class CMD_CleanupBots : Cmd
        {
            public override string Key { get { return "clean"; } }
            public override void DoCommand(string arg, TextWriter output)
            {
                BotRunner.Instance.StopAllBots();
                BotRunner.Instance.CleanupBots();
            }
        }
        [DescAttribute("机器人状态", "机器人")]
        public class CMD_ListBots : Cmd
        {
            public override string Key { get { return "ls"; } }
            public override void DoCommand(string arg, TextWriter output)
            {
                output.WriteLine(BotRunner.Instance.BotsStatus);
            }
        }

        //---------------------------------------------------------------------------------------------------
        [DescAttribute("开始批量增加定时器", "机器人")]
        public class CMD_StartTimer : Cmd
        {
            private static AtomicState<bool> mIsRunning = new AtomicState<bool>(false);
            public static bool IsRunning
            {
                get { return mIsRunning.Value; }
                set { mIsRunning.ChangeState(value); }
            }

            public override string Key { get { return "start"; } }
            public override void DoCommand(string arg, TextWriter output)
            {
                Random rd = new Random();
                IsRunning = true;
                BotRunner.Instance.AddBots(100, rd.Next(10, 20), new List<int>(new int[] { 100861, 100862, 100863, 100864, 100865 }));
                System.Threading.Thread timer = new System.Threading.Thread((o) =>
                {
                    System.Threading.Thread.Sleep(1000);
                    while (IsRunning)
                    {
                        var list = BotRunner.Instance.BotsList;
                        foreach (var bot in list)
                        {
                            if (bot.IsRunning)
                            {
                                BotRunner.Instance.StopBot(bot);
                                break;
                            }
                        }
                        System.Threading.Thread.Sleep(1000);
                        BotRunner.Instance.AddBots(1, rd.Next(10, 20), new List<int>(new int[] { 100861, 100862, 100863, 100864, 100865 }));
                    }
                });
                timer.Start();
            }
        }
        [DescAttribute("停止批量增加定时器", "机器人")]
        public class CMD_StopTimer : Cmd
        {
            public override string Key { get { return "stop"; } }
            public override void DoCommand(string arg, TextWriter output)
            {
                CMD_StartTimer.IsRunning = false;
            }
        }
        //---------------------------------------------------------------------------------------------------


    }
}