package mcjty.deepresonance.generatornetwork;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import net.minecraftforge.common.util.Constants;

import java.util.HashMap;
import java.util.Map;

public class DRGeneratorNetwork extends WorldSavedData {

    public static final String GENERATOR_NETWORK_NAME = "DRGeneratorNetwork";
    private static DRGeneratorNetwork instance = null;

    private int lastId = 0;

    private final Map<Integer,Network> networks = new HashMap<Integer,Network>();

    public DRGeneratorNetwork(String identifier) {
        super(identifier);
    }

    public void save(World world) {
        world.mapStorage.setData(GENERATOR_NETWORK_NAME, this);
        markDirty();
    }

    public static void clearInstance() {
        if (instance != null) {
            instance.networks.clear();
            instance = null;
        }
    }

    public static DRGeneratorNetwork getChannels() {
        return instance;
    }

    public static DRGeneratorNetwork getChannels(World world) {
        if (world.isRemote) {
            return null;
        }
        if (instance != null) {
            return instance;
        }
        instance = (DRGeneratorNetwork) world.mapStorage.loadData(DRGeneratorNetwork.class, GENERATOR_NETWORK_NAME);
        if (instance == null) {
            instance = new DRGeneratorNetwork(GENERATOR_NETWORK_NAME);
        }
        return instance;
    }

    public Network getOrCreateNetwork(int id) {
        Network channel = networks.get(id);
        if (channel == null) {
            channel = new Network();
            networks.put(id, channel);
        }
        return channel;
    }

    public Network getChannel(int id) {
        return networks.get(id);
    }

    public void deleteChannel(int id) {
        networks.remove(id);
    }

    public int newChannel() {
        lastId++;
        return lastId;
    }

    @Override
    public void readFromNBT(NBTTagCompound tagCompound) {
        networks.clear();
        NBTTagList lst = tagCompound.getTagList("networks", Constants.NBT.TAG_COMPOUND);
        for (int i = 0 ; i < lst.tagCount() ; i++) {
            NBTTagCompound tc = lst.getCompoundTagAt(i);
            int channel = tc.getInteger("channel");
            int v = tc.getInteger("refcount");
            int energy = tc.getInteger("energy");

            Network value = new Network();
            value.refcount = v;
            value.energy = energy;
            networks.put(channel, value);
        }
        lastId = tagCompound.getInteger("lastId");
    }

    @Override
    public void writeToNBT(NBTTagCompound tagCompound) {
        NBTTagList lst = new NBTTagList();
        for (Map.Entry<Integer, Network> entry : networks.entrySet()) {
            NBTTagCompound tc = new NBTTagCompound();
            tc.setInteger("channel", entry.getKey());
            tc.setInteger("refcount", entry.getValue().getRefcount());
            tc.setInteger("energy", entry.getValue().getEnergy());
            lst.appendTag(tc);
        }
        tagCompound.setTag("networks", lst);
        tagCompound.setInteger("lastId", lastId);
    }

    public static class Network {
        private int refcount = 0;
        private int energy = 0;

        public int getRefcount() {
            return refcount;
        }

        public void setRefcount(int refcount) {
            this.refcount = refcount;
        }

        public void incRefCount() {
            this.refcount++;
        }

        public void decRefCount() {
            this.refcount--;
        }

        public int getEnergy() {
            return energy;
        }

        public void setEnergy(int energy) {
            this.energy = energy;
        }
    }
}

@@ -2355,94 +2355,70 @@
     
-int v = tc.getInteger(%22refcount%22);%0A            int energy = tc.getInteger
+NBTTagCompound networkTag = tc.getCompoundTag
 (%22
-e
 ne
-rgy
+tworkData
 %22);%0A
-%0A
     
@@ -2480,53 +2480,29 @@
 e.re
-fcount = v;%0A            value.energy = energy
+adFromNBT(networkTag)
 ;%0A  
@@ -2919,64 +2919,108 @@
     
-tc.setInteger(%22refcount%22, entry.getValue().getRefcount()
+NBTTagCompound networkTag = new NBTTagCompound();%0A            entry.getValue().writeToNBT(networkTag
 );%0A 
@@ -3040,54 +3040,37 @@
 .set
-Integer
+Tag
 (%22
-e
 ne
-rgy%22, entry.getValue().getEnergy()
+tworkData%22, networkTag
 );%0A 
@@ -3790,21 +3790,400 @@
 y;%0A        %7D
+%0A%0A        public void writeToNBT(NBTTagCompound tagCompound)%7B%0A            tagCompound.setInteger(%22refcount%22, refcount);%0A            tagCompound.setInteger(%22energy%22, energy);%0A        %7D%0A%0A        public void readFromNBT(NBTTagCompound tagCompound)%7B%0A            this.refcount = tagCompound.getInteger(%22refcount%22);%0A            this.energy = tagCompound.getInteger(%22energy%22);%0A        %7D
 %0A    %7D%0A%7D%0A

Make network save its own data