package joshie.harvestmoon.mining;

import static joshie.harvestmoon.HarvestMoon.handler;
import static joshie.harvestmoon.helpers.generic.ServerHelper.getWorld;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import joshie.harvestmoon.blocks.BlockDirt;
import joshie.harvestmoon.crops.WorldLocation;
import joshie.harvestmoon.init.HMBlocks;
import joshie.harvestmoon.npc.EntityNPCMiner;
import joshie.harvestmoon.util.IData;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;

public class MineTrackerServer implements IData {
    private HashSet<MineBlockLocation> mineBlocks = new HashSet();
    private HashSet<MineLocation> mineLocation = new HashSet();

    private MineBlockLocation getKey(World world, int x, int y, int z, int level) {
        return new MineBlockLocation(world.provider.dimensionId, x, y, z, level);
    }
    
    private MineLocation getKey(World world, int x, int y, int z, int level, String name) {
        return new MineLocation(world.provider.dimensionId, x, y, z, level, name);
    }

    public boolean newDay() {
        Iterator<MineBlockLocation> it = mineBlocks.iterator();
        while (it.hasNext()) {
            MineBlockLocation location = it.next();
            int level = location.getLevel();
            if (level > 0) {
                World world = getWorld(location.dimension);
                ArrayList<Integer> metas = BlockDirt.getMeta(level);
                int meta = metas.get(world.rand.nextInt(metas.size()));
                world.setBlock(location.x, location.y, location.z, HMBlocks.dirt, meta, 2);
            }
        }

        return true;
    }

    public void addMineBlock(World world, int x, int y, int z, int level) {
        handler.getServer().markDirty();
    }
    
    /** Call this to add a new mine at the location under the following name **/
    public void addMineLevel(World world, int x, int y, int z, String name, EntityNPCMiner npc) {
        MineLocation key = getKey(world, x, y, z, 0, name);
        MineLocation location = null;
        for (MineLocation local: mineLocation) {
            if(local.equals(key)) {
                location = local;
                break;
            }
        }
        
        if(location == null) {
            location = key;
            mineLocation.add(location);
        } else location.addLevel();

        npc.startBuild(world, x, y, z, location.getLevel());
        handler.getServer().markDirty();
    }

    @Override
    public void readFromNBT(NBTTagCompound nbt) {
        NBTTagList farm = nbt.getTagList("Farmland", 10);
        for (int i = 0; i < farm.tagCount(); i++) {
            NBTTagCompound tag = farm.getCompoundTagAt(i);
            MineBlockLocation location = new MineBlockLocation();
            location.readFromNBT(tag);
            mineBlocks.add(location);
        }
    }

    @Override
    public void writeToNBT(NBTTagCompound nbt) {
        NBTTagList farm = new NBTTagList();
        for (WorldLocation location : mineBlocks) {
            NBTTagCompound tag = new NBTTagCompound();
            location.writeToNBT(tag);
            farm.appendTag(tag);
        }

        nbt.setTag("Farmland", farm);
    }
}

@@ -286,55 +286,8 @@
 rt;%0A
-import joshie.harvestmoon.crops.WorldLocation;%0A
 impo
@@ -870,28 +870,24 @@
 vel);%0A    %7D%0A
-    
 %0A    private
@@ -1722,32 +1722,87 @@
 z, int level) %7B%0A
+        mineBlocks.add(getKey(world, x, y, z, level));%0A
         handler.
@@ -1832,20 +1832,16 @@
 ;%0A    %7D%0A
-    
 %0A    /**
@@ -2141,16 +2141,17 @@
 on local
+ 
 : mineLo
@@ -2170,24 +2170,25 @@
           if
+ 
 (local.equal
@@ -2278,24 +2278,16 @@
       %7D%0A
-        
 %0A       
@@ -2289,16 +2289,17 @@
       if
+ 
 (locatio
@@ -2604,28 +2604,33 @@
  NBTTagList 
-farm
+mineBlock
  = nbt.getTa
@@ -2636,24 +2636,26 @@
 agList(%22
-Farmland
+MineBlocks
 %22, 10);%0A
@@ -2682,20 +2682,25 @@
  0; i %3C 
-farm
+mineBlock
 .tagCoun
@@ -2744,20 +2744,25 @@
 d tag = 
-farm
+mineBlock
 .getComp
@@ -2923,24 +2923,359 @@
 );%0A        %7D
+%0A%0A        NBTTagList locations = nbt.getTagList(%22MineLocations%22, 10);%0A        for (int i = 0; i %3C locations.tagCount(); i++) %7B%0A            NBTTagCompound tag = locations.getCompoundTagAt(i);%0A            MineLocation location = new MineLocation();%0A            location.readFromNBT(tag);%0A            mineLocation.add(location);%0A        %7D
 %0A    %7D%0A%0A    
@@ -3352,20 +3352,25 @@
 TagList 
-farm
+mineBlock
  = new N
@@ -3395,21 +3395,25 @@
    for (
-World
+MineBlock
 Location
@@ -3543,20 +3543,25 @@
         
-farm
+mineBlock
 .appendT
@@ -3604,23 +3604,323 @@
 ag(%22
-Farmland%22, farm
+MineBlocks%22, mineBlock);%0A%0A        NBTTagList locations = new NBTTagList();%0A        for (MineLocation location : mineLocation) %7B%0A            NBTTagCompound tag = new NBTTagCompound();%0A            location.writeToNBT(tag);%0A            locations.appendTag(tag);%0A        %7D%0A%0A        nbt.setTag(%22MineLocations%22, locations
 );%0A 

Save the MineTracker data. And actually add MineBlocks to the set.