using UnityEngine;
using NeonTea.Quakeball.TeaNet.Packets;

using System.Collections.Generic;

namespace NeonTea.Quakeball.Networking.Packets {
    public class MultiplePlayerUpdatesPckt : Packet {

        public List<PlayerUpdatePckt> Updates = new List<PlayerUpdatePckt>();

        public MultiplePlayerUpdatesPckt() { }

        public MultiplePlayerUpdatesPckt(List<NetPlayer> players) {
            foreach (NetPlayer p in players) {
                if (p.Controlled == null) {
                    continue;
                }
                Updates.Add(p.Controlled.CreateUpdatePacket(p.Id));
            }
        }

        public override void Read(ByteBuffer buffer) {
            Updates.Clear();
            int count = buffer.ReadInt();
            for (int i = 0; i < count; i++) {
                PlayerUpdatePckt pckt = new PlayerUpdatePckt();
                pckt.Read(buffer);
                Updates.Add(pckt);
            }
        }

        public override void Write(ByteBuffer buffer) {
            buffer.Write(Updates.Count);
            foreach (PlayerUpdatePckt p in Updates) {
                p.Write(buffer);
            }
        }
    }

    public class MultipleSyncsPckt : Packet {

        public List<PlayerSyncPacket> Syncs = new List<PlayerSyncPacket>();

        public MultipleSyncsPckt() { }

        public MultipleSyncsPckt(List<NetPlayer> players) {
            foreach (NetPlayer p in players) {
                if (p.Controlled == null) {
                    continue;
                }
                PlayerSyncPacket pckt = p.Controlled.CreateSyncPacket(p.Id, p.Unsynced);
                p.Unsynced = false;
                Syncs.Add(pckt);
            }
        }

        public override void Read(ByteBuffer buffer) {
            Syncs.Clear();
            int count = buffer.ReadInt();
            for (int i = 0; i < count; i++) {
                PlayerSyncPacket pckt = new PlayerSyncPacket();
                pckt.Read(buffer);
                Syncs.Add(pckt);
            }
        }

        public override void Write(ByteBuffer buffer) {
            buffer.Write(Syncs.Count);
            foreach (PlayerSyncPacket p in Syncs) {
                p.Write(buffer);
            }
        }
    }
}
