﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Runtime.CompilerServices;

namespace Gaazar
{
    public interface INettedNetwork
    {
        Updater Updaters { get; set; }
        int[] ConnectionsID { get; set; }
        void Broadcast(byte[] data);
        void SendTo(byte[] data,int id);
        void ConnectionsUpdate();
        void Init(string address,int port);

    }

    /*public class NettedNetwork : INettedNetwork
    {
        public Updater Updaters { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public int[] ConnectionsID { get; set; }

        Socket socket; //目标socket
        EndPoint clientEnd; //客户端
        IPEndPoint ipEnd; //侦听端口
        Thread connectThread; //连接线程
        List<Updater> _updaters = new List<Updater>();

        void InitSocket(string ip,int port)
        {
            // 定义侦听端口,侦听任何IP
            ipEnd = new IPEndPoint(IPAddress.Any, 0);
            //定义套接字类型,在主线程中定义
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            //服务端需要绑定ip
            socket.Bind(ipEnd);
            //定义客户端
            IPEndPoint sender = new IPEndPoint(IPAddress.Parse(ip), port);
            clientEnd = (EndPoint)sender;
            //开启一个线程连接，必须的，否则主线程卡死
            connectThread = new Thread(new ThreadStart(SocketReceive));
            connectThread.Start();
        }
        void SocketSend(byte[] Data)
        {
            //清空发送缓存
            //sendData = new byte[4096];
            //数据类型转换
            //发送给指定客户端
            socket.SendTo(Data, Data.Length, SocketFlags.None, clientEnd);
        }
        void SocketReceive()
        {
            //进入接收循环
            byte[] recvData;
            int recvLen;
            while (true)
            {
                //对data清零
                recvData = new byte[4096];
                //获取客户端，获取客户端数据，用引用给客户端赋值
                recvLen = socket.ReceiveFrom(recvData, ref clientEnd);
                //MemoryStream ms = new MemoryStream(recvData);
                string command = Encoding.ASCII.GetString(recvData, 0, 4);
                if (command == "ASID")//服务器返回的自己的ID
                {
                    connectionID = Bit.ToInt32(recvData, 4);
                    continue;
                }
                if (command == "OLST")//从服务器返回在线玩家列表
                {
                    List<int> online = new List<int>();
                    int count = BitConverter.ToInt32(recvData, 4);
                    for (int i = 0; i < count; i++)
                    {
                        online.Add(BitConverter.ToInt32(recvData, 8 + i * 4));
                    }
                    CheckOnlinePlayer(online);
                    continue;
                }
                int senderID;
                if (command == "GTNS")
                {
                    using (MemoryStream m = new MemoryStream(recvData))
                    {
                        BinaryReader br = new BinaryReader(m);
                        senderID = br.ReadInt32();
                        int count = br.ReadInt32();
                        string cmpName;
                        for (int i = 0; i < count; i++)
                        {

                        }
                    }
                    continue;
                }
                using (MemoryStream m = new MemoryStream(recvData))
                {
                    BinaryReader br = new BinaryReader(m);
                    senderID = br.ReadInt32();
                    int objcount = br.ReadInt32();
                    for (int i = 0; i < objcount; i++)
                    {
                        Updater u = new Updater();
                        u.objID = br.ReadInt32();
                        int intdcount = br.ReadInt32();
                        List<NettedInfo> nfs = new List<NettedInfo>();
                        for (int n = 0; i < intdcount; n++)
                        {
                            NettedInfo nf = new NettedInfo();
                            nf.index = Bit.ToInt32(new byte[] { br.ReadByte(), 0, 0, 0 }, 0);
                            int len = br.ReadInt32();
                            nf.ms = new MemoryStream(br.ReadBytes(len));
                            nfs.Add(nf);
                        }
                        u.netteds = nfs.ToArray();
                        lock (lockobj)
                        {
                            updaters.Add(u);
                        }
                    }
                }
            }
        }


        public void Broadcast(byte[] data)
        {
            throw new NotImplementedException();
        }

        public void ConnectionsUpdate()
        {
            throw new NotImplementedException();
        }

        public void Init(string address, int port)
        {
            //"127.0.0.1",6680
            InitSocket(address,port);
            SocketSend(Encoding.ASCII.GetBytes("CONT"));

        }

        public void SendTo(byte[] data, int id)
        {
            throw new NotImplementedException();
        }
    }*/
}
