using System;
using SimpleJson;
using System.Threading;
using Pomelo.DotNetClient;
using ZeusServerEdgeJS;
using CommonAI.Zone;
using CommonLang;

namespace ZeusServerEdgeJSTest
{
    //class PingStat
    //{
    //    public long max;
    //    public long min;
    //    public long total;
    //    public long count;
    //    public long pingStart;
    //    public double avg;
    //}
    public class ClientTest
    {
        public static PomeloClient pc = null;

        private static string tocken;
        private static JsonObject player;
        private static Timer pingTimer;
        private static Pomelo.DotNetClient.Monitor pingMonitor = new Pomelo.DotNetClient.Monitor("ping",30);

        public static void loginTest(string host, int port)
        {
            pc = new PomeloClient();

            pc.NetWorkStateChangedEvent += (state) =>
            {
                Console.WriteLine(state);
            };


            pc.initClient(host, port, () =>
            {
                pc.connect(null, data =>
                {
                    JsonObject loginRequest = new JsonObject();
                    loginRequest["username"] = "atest1";
                    loginRequest["password"] = "111";

                    pc.request("gate.gateHandler.login", loginRequest, result =>
                    {
                        JsonObject msg = new JsonObject();
                        msg["uid"] = result["uid"];
                        tocken = (string)result["token"];
                        pc.request("gate.gateHandler.queryEntry", msg, OnQuery);
                    });
      
                  //  Console.WriteLine("on data back" + data.ToString());
                  
                });
            });
        }

        public static void OnQuery(JsonObject result)
        {
            if (Convert.ToInt32(result["code"]) == 200)
            {
                pc.disconnect();

                string host = (string)result["host"];
                int port = Convert.ToInt32(result["port"]);
                pc = new PomeloClient();

                pc.NetWorkStateChangedEvent += (state) =>
                {
                    Console.WriteLine(state);
                };

                pc.initClient(host, port, () =>
                {
                    pc.connect(null, (data) =>
                    {
                        JsonObject userMessage = new JsonObject();
                        Console.WriteLine("on connect to connector!");

                        //Login
                        JsonObject msg = new JsonObject();
                        msg["token"] = tocken;

                        pc.request("connector.entryHandler.entry", msg, OnEnter);
                    });
                });
            }
        }

        public static void ping()
        {

            pingMonitor.start(CUtils.CurrentTimeMS);

         //   Console.WriteLine("send Ping:" + pingStat.pingStart);
            JsonObject msg = new JsonObject();

            JsonObject pingAction = new JsonObject();
            pingAction["type"] = "Ping";
            msg["data"] = ZoneService.instance().createAction(pingAction);
            pc.notify("area.playerHandler.battleEvent", msg);
        }

        public static void OnEnter(JsonObject result)
        {
            if (Convert.ToInt32(result["code"]) == 200)
            {
                player = (JsonObject)result["player"];
            //    Console.WriteLine("on login " + result.ToString());

                pc.on("onBattleEvent", data =>
                {
                    byte[] bytes = Convert.FromBase64String((string)data["data"]);

                    object obj = ZoneService.instance().decodeEvent(bytes);
                    if (obj.GetType() == typeof(Pong))
                    {
                        pingMonitor.end(CUtils.CurrentTimeMS);
                     
                    }
                     Console.WriteLine("event: " + data["name"]);
                });

                pingTimer = new Timer(data =>
                {
                    ping();

                },"",1000,1000);

                JsonObject msg = new JsonObject();
                pc.request("area.playerHandler.enterScene", msg, data =>
                {
                    Console.WriteLine("enterScene success!");
                });
            }
            else
            {
                Console.WriteLine("enter error:" + result["code"]);

            }

        }

        public static void onDisconnect(JsonObject result)
        {
            Console.WriteLine("on sockect disconnected!");
        }

        public static void Run()
        {
            string host = "192.168.4.98";
            int port = 3014;
            ZoneService.instance().start( "D:/nodejs/node_global/npm/node_modules/zeus-csharp/lib/dll", "D:/nodejs/node_global/npm/node_modules/zeus-csharp/data");

            loginTest(host, port);

            do
            {
                Thread.Sleep(10);
            }   
            while (true);
        }
    }
}