package com.zivilon.cinder_loe.network;

import com.zivilon.cinder_loe.world.event.Warband;
import com.zivilon.cinder_loe.world.event.WarbandFaction;
import com.zivilon.cinder_loe.world.event.WarbandLocationInfo;
import com.zivilon.cinder_loe.world.event.WarbandTracker;

import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import lotr.common.LOTRMod;
import lotr.common.network.LOTRPacketHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTUtil;
import net.minecraft.network.PacketBuffer;
import net.minecraft.world.World;

public class PacketWarbandLocations implements IMessage {
    public List<WarbandLocationInfo> locations = WarbandTracker.get_all();

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeShort(locations.size());
        for (WarbandLocationInfo loc : locations) {
            buf.writeLong(loc.warband.warband_uuid.getMostSignificantBits());
            buf.writeLong(loc.warband.warband_uuid.getLeastSignificantBits());
            buf.writeDouble(loc.x);
            buf.writeDouble(loc.z);
            PacketBuffer packet = new PacketBuffer(buf);
            write_string_to_buffer(packet, loc.warband.faction.name());
        }
    }

    @Override

    public void fromBytes(ByteBuf buf) {
        locations.clear();
        int count = buf.readShort();
        for (int i = 0; i < count; i++) {
            long msb = buf.readLong();
            long lsb = buf.readLong();
            UUID uuid = new UUID(msb, lsb);
            double x = buf.readDouble();
            double z = buf.readDouble();

            PacketBuffer packet = new PacketBuffer(buf);
            String faction_name = read_string_from_buffer(packet, Short.MAX_VALUE);

            WarbandFaction faction = WarbandFaction.get_warband_by_name(faction_name);

            Warband dummy = new Warband();
            dummy.warband_uuid = uuid;
            dummy.faction = faction;
            dummy.x = (int) x;
            dummy.z = (int) z;
            locations.add(new WarbandLocationInfo(dummy, x, z));
        }
    }

    public static class Handler implements IMessageHandler<PacketWarbandLocations, IMessage> {
        @Override
        public IMessage onMessage(PacketWarbandLocations message, MessageContext ctx) {
            if (ctx.side.isServer()) {
                System.out.println("[PacketWarbandLocations] WARNING: Client tried to update locations on server!");
                return null;
            }

            WarbandTracker.clear_locations();
            for (WarbandLocationInfo info : message.locations) {
                WarbandTracker.add(info);
            }
            return null;
        }
    }

    public static void send_warband_locations(World world) {
        for (int i = 0; i < world.playerEntities.size(); i++) {
            EntityPlayer entityplayer = (EntityPlayer)world.playerEntities.get(i);
            send_location(entityplayer, world);
        }
    }

    public static void send_location(EntityPlayer player, World world) {
        PacketWarbandLocations locations = new PacketWarbandLocations();
        LOTRPacketHandler.networkWrapper.sendTo((IMessage)locations, (EntityPlayerMP)player);
    }
    public static void write_string_to_buffer(PacketBuffer buffer, String string) {
        try {
            buffer.writeStringToBuffer(string);
        } catch (IOException e) {
            throw new RuntimeException("[PacketWarbandLocations] WARNING: Failed to write warband faction", e);
        }
    }

    public static String read_string_from_buffer(PacketBuffer buffer, int max_length) {
        try {
            return buffer.readStringFromBuffer(max_length);
        } catch (IOException e) {
            throw new RuntimeException("[PacketWarbandLocations] WARNING: Failed to read warband faction", e);
        }
    }

}
