using CommonLang.Log;
using pomelo.area;
using pomelo.connector;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XmdsBattleClient.Client;
using XmdsBattleClientBot.Bot;
using XmdsBattleClientWin32.Func;

namespace XmdsBattleClientWin32.WinForm
{
    public partial class FormBot : Form
    {
        public readonly static Logger log = new ConsoleLogger();
        private readonly BotClient bot;
        private long last_update_time;
        private Battle.BattlePanelContainer battle_view;
        public BotClient Bot { get { return bot; } }

        public FormBot()
        {
            InitializeComponent();
            this.bot = new BotClient("bottest", "123456", FormLauncher.LastConfig, null);
            this.bot.Client.GameSocket.IsSaveResponse = true;
//             this.bot.Client.GateSocket.OnError += on_error;
//             this.bot.Client.GameSocket.OnError += on_error;
            this.bot.Client.OnBeginEnterScene += on_gs_begin_enter_scene;
            this.battle_view = new Battle.BattlePanelContainer(bot);
            this.battle_view.Dock = DockStyle.Fill;
            this.Controls.Add(battle_view);
        }
        private void FormBot_Load(object sender, EventArgs e)
        {
            bot.Start();
        }
        private void FormBot_Shown(object sender, EventArgs e)
        {
            relogin();
        }
        private void timer_Update_Tick(object sender, EventArgs e)
        {
            long curTime = CommonLang.CUtils.CurrentTimeMS;
            if (last_update_time == 0)
            {
                last_update_time = curTime;
            }
            int intervalMS = (int)(curTime - last_update_time);
            last_update_time = curTime;
            try
            {
                bot.Update(intervalMS);
                if (battle_view != null)
                {
                    battle_view.UpdateBattle(intervalMS);
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }


        //-------------------------------------------------------------------------------------------------
        #region Runner

        public static void on_error(Exception err)
        {
            MessageBox.Show(err.Message);
        }

        private void relogin()
        {
            var login = new FormLogin(bot);
            login.OnLogin += on_login;
            login.ShowDialog(this);
        }
        private void select_role()
        {
            var r = new FormCreateRole(bot);
            r.ShowDialog(this);
        }


        private void on_login()
        {
            FormBot.log.Info("on_login...");
            bot.QueueTask(() =>
            {
                start_enter_server();
            });
        }
        private void start_enter_server()
        {
            bot.gate_EnterServer(on_gate_enter_server, (err) =>
            {
                on_error(err);
                relogin();
            });
        }
        protected virtual void on_gate_enter_server(EntryResponse e)
        {
            this.Text = string.Format("帐号:{0} 服务器:{1}", bot.Account, bot.ServerName);
            FormBot.log.Info("on_gate_enter_server : " + this.Text);
            bot.QueueTask(() =>
            {
                if (e.s2c_players.Count > 0 && Program.AUTO_LOGIN)
                {
                    log.Info("auto bind player ");
                    bot.gate_BindPlayer(e.s2c_players[0].id,
                        (rsp) => { log.Info("auto bind player done "); },
                        (err) =>
                        {
                            on_error(err);
                            select_role();
                        });
                }
                else
                {
                    select_role();
                }
            });
        }
        protected virtual void on_gs_begin_enter_scene(XmdsBattleClient.Battle.XmdsBattleClient obj)
        {
            FormBot.log.Info("on_gs_begin_enter_scene : ");
            bot.gs_EnterScene(on_gs_enter_scene, (err) => { on_error(err); relogin(); });
        }
        protected virtual void on_gs_enter_scene(EnterSceneResponse response)
        {
            FormBot.log.Info("on_gs_enter_scene : ");
            if (Program.ARG_TC)
            {
                FormTC form = new FormTC(bot);
                form.Show();
            }
        }

        #endregion
    }
}