using CTZLauncher.Modules.Minecraft;
using LitJson;
using System;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Threading;

namespace CTZLauncher.Tools
{
    public class MinecraftServer
    {
        string address;
        int port;
        bool finish;
        ServerInfo serverinfo;

        /// <summary>
        /// 获取服务器详细信息
        /// </summary>
        /// <param name="address">服务器地址</param>
        /// <param name="port">服务器端口</param>
        public MinecraftServer(string address, int port)
        {
            this.address = address;
            this.port = port;
        }
        /// <summary>
        /// 模仿C#的Application.Doevent函数。可以适当添加try catch 模块
        /// </summary>
        public void DoEvent()
        {
            DispatcherFrame frame = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame);
            Dispatcher.PushFrame(frame);
        }
        public object ExitFrame(object f)
        {
            ((DispatcherFrame)f).Continue = false;
            return null;
        }
        /// <summary>
        /// 异步获取服务器信息
        /// </summary>
        /// <returns>服务器信息</returns>
        public ServerInfo DoAsync()
        {
            new Thread(new ThreadStart(GetInfoAsync)).Start();
            while (finish == false) { DoEvent(); }
            return serverinfo;
        }
        /// <summary>
        /// 获取服务器信息
        /// </summary>
        /// <returns>服务器信息</returns>
        public ServerInfo Do()
        {
            return GetInfo();
        }
        public void GetInfoAsync()
        {
            finish = false;
            serverinfo = GetInfo();
            finish = true;
        }
        public ServerInfo GetInfo()
        {
            using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                try
                {
                    client.Connect(address, port);
                    if (client.Connected)
                    {
                        client.Send(new byte[] { 0x0f, 0x00, 0x04, 0x09, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x63, 0xdd, 0x01 });
                        client.Send(new byte[] { 0x01, 0x00 });
                        byte[] header = new byte[5];
                        client.Receive(header, header.Length, SocketFlags.None);
                        byte[] buffer = new byte[1024];
                        int recover = client.Receive(buffer, buffer.Length, SocketFlags.None);
                        byte[] data = new byte[recover];
                        Array.Copy(buffer, data, recover);
                        string info = Encoding.UTF8.GetString(data);
                        return JsonMapper.ToObject<ServerInfo>(info);
                    }
                    Console.WriteLine("网络连接失败!");
                    return null;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    return null;
                }
            }
        }
    }
}
