using CommonAI.Zone;
using CommonAI.Zone.ZoneEditor;
using CommonAIClient.Client;
using CommonLang;
using CommonLang.Protocol;
using GameEditorPlugin.Win32.BattleClient;
using GameEditorPlugin.Win32.Runtime;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XmdsServerNode.Node;
using XmdsServerNode.Node.Interface;
using XmdsServerTest.Server;
using CommonAI.ZoneServer;
using CommonAIServer.Connector;
using CommonLang.Concurrent;
using XmdsCommonServer.Message;

namespace XmdsServerTest
{
    public class FormTestClient : GameEditorPluginServer.Client.FormTestClient
    {
        private static AtomicInteger uuid_gen = new AtomicInteger(1);

        public static FormLauncher StartWinForm(int unit_template_id, byte force, string roomID, int scendID, bool autoLaunch = false)
        {
            ClientLoader loader = new ClientLoader();
            FormLauncher.Templates = ZoneNodeManager.Templates;
            var client = new FormLauncher(
                ZoneNodeManager.NodeConfig.GAME_DATA_ROOT_PATH,
                "test_" + uuid_gen.IncrementAndGet(), // player_uuid
                roomID.ToString(), // room_id
                typeof(CommonNetwork.Sockets.NetSession).FullName,
                NodeTestServer.Instance.ClientConnectString,
                ZoneNodeManager.NodeConfig.GAME_UPDATE_INTERVAL_MS, // update interval
                ZoneNodeManager.NodeConfig.CLIENT_SYNC_OBJECT_IN_RANGE, // sync range
                unit_template_id,
                force,
                scendID,
                loader, false, autoLaunch);
            client.OnLaunchOver += Client_OnLaunchOver;
            client.Show();
            return client;
        }

        private static bool Client_OnLaunchOver(FormLauncher launcher)
        {
            var enter = launcher.Loader.GenUnitInfoR2B(launcher.UnitTemplateID);
            enter.Force = launcher.Force;
            var ret = new FormTestClient();
            ret.Init(
                launcher.PlayerUUID,
                launcher.RoomID,
                launcher.ConnectString,
                launcher.NetDirver,
                enter);
            ret.Shown += (sender, e) => { launcher.Visible = false; };
            ret.FormClosed += (sender, e) => { launcher.Visible = true; };
            launcher.Disposed += (sender, e) => { };
            ret.Show();
            return true;
        }


        private class ClientLoader : TestClientLoader
        {
            public override CommonAI.ZoneServer.CreateUnitInfoR2B GenUnitInfoR2B(int unitID)
            {
                CommonAI.ZoneServer.CreateUnitInfoR2B ret = new CommonAI.ZoneServer.CreateUnitInfoR2B();
                ret.UnitTemplateID = unitID;
                return ret;
            }
            public override Type ZoneFactoryType
            {
                get { return typeof(XmdsCommon.Plugin.XmdsZoneFactory); }
            }
        }

        public void Init(string playerUUID, string roomID, string connectString, string netDriver, CreateUnitInfoR2B enter)
        {
            base.Init(ZoneNodeManager.NodeConfig, playerUUID, roomID, connectString, netDriver, enter);
            base.BattlePanel.OnPanelLoaded += BattlePanel_OnPanelLoaded;
        }
        private void BattlePanel_OnPanelLoaded()
        {
            base.BattlePanel.BattleClient.Layer.ActorAdded += Layer_ActorAdded;
        }
        private void Layer_ActorAdded(CommonAI.ZoneClient.ZoneLayer layer, CommonAI.ZoneClient.ZoneActor actor)
        {
            //actor.SendAction(new MarkForTestBotAction());
        }
    }


}