using CommonLang;
using CommonLang.Property;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using XmdsBattleClientBot.Bot;
using XmdsBattleClientWin32;

namespace XmdsBotTest
{
    public static class LaunchArgs
    {
        public static string DefaultBotPrefix { get; private set; }
        public static int DefaultBotCount { get; private set; }
        public static bool IsAuto { get; private set; }

        public static void Init(string[] args)
        {
            Properties argp = new Properties();
            foreach (var arg in args)
            {
                argp.ParseLine(arg);
            }
            string b_name = argp.Get("n");
            string b_count = argp.Get("c");
            if (b_name != null && b_count != null)
            {
                LaunchArgs.IsAuto = true;
                LaunchArgs.DefaultBotPrefix = b_name;
                LaunchArgs.DefaultBotCount = int.Parse(b_count);
            }
            else
            {
                LaunchArgs.IsAuto = false;
            }
        }
    }

    static class Program
    {
        private static bool first_start = true;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(params string[] args)
        {
            /*
            while (true)
            {
                Console.WriteLine("123123");
                BotClient bot = new BotClient("SB1020", "", FormLauncher.LastConfig);
                bot.connect();


                Thread.Sleep(1000);
            }
            */
            
            ReflectionUtil.LoadDlls(new System.IO.DirectoryInfo(Application.StartupPath));
            LaunchArgs.Init(args);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var launcher = new FormLauncher();
            launcher.Shown += Launcher_Shown;
            launcher.OnStart += Launcher_OnStart;
            Application.Run(launcher);
            if (!Application.StartupPath.StartsWith(@"D:\robot\"))
            {
                //TODO 2017/1/22  CommonSecure.ConnectWS.Run(launcher);
            }
        }


        private static void Launcher_Shown(object sender, EventArgs e)
        {
            if (LaunchArgs.IsAuto && first_start)
            {
                first_start = false;
                (sender as FormLauncher).Start();
            }
        }
        private static void Launcher_OnStart(FormLauncher sender, XmdsBattleClientBot.BotConfig config)
        {
            var bot = new FormBotTest(config);
            bot.FormClosed += new FormClosedEventHandler((object sender2, FormClosedEventArgs e2) =>
            {
                sender.Show();
            });
            bot.Show();
        }
    }
}