using System.Net.Sockets;
using System.Net;
using YapperServer;
using YapperServer.Net.IO;

class Program
{
    static TcpListener _listener;
    static List<Client> _users;
    static void Main(string[] args)
    {
        _users = new List<Client>();
        _listener = new TcpListener(IPAddress.Parse("127.0.0.1"), 7891);
        _listener.Start();

        while (true)
        {
            var client = new Client(_listener.AcceptTcpClient());
            _users.Add(client);

            /* Broadcast the connection to everyone on the server */
            BroadCastConnection();
        }
        
    }

    static void BroadCastConnection()
    {
        foreach (var user in _users)
        {
            foreach (var usr in _users)
            {
                var broadcastPacket = new PacketBuilder();
                broadcastPacket.WriteOpCode(1);
                broadcastPacket.WriteString(usr.UserName);
                broadcastPacket.WriteString(usr.UID.ToString());
                user.ClientSocket.Client.Send(broadcastPacket.GetPacketBytes());
            }
        }
    }

    public static void BroadCastMessage(string message)
    {
        foreach (var user in _users)
        {
            var msgPacket = new PacketBuilder();
            msgPacket.WriteOpCode(5);
            msgPacket.WriteString(message);
            user.ClientSocket.Client.Send(msgPacket.GetPacketBytes());
        }
    }

    public static void DisconnectMessage(string uid) {
        var disconnectedUser = _users.Where(x => x.UID.ToString() == uid).FirstOrDefault();
        _users.Remove(disconnectedUser);

        foreach (var user in _users)
        {
            var broadcastPacket = new PacketBuilder();
            broadcastPacket.WriteOpCode(10);
            broadcastPacket.WriteString(uid);
            user.ClientSocket.Client.Send(broadcastPacket.GetPacketBytes());
        }

        BroadCastMessage($"{disconnectedUser.UserName} lecsatlakozott.");
    }
}