package openperipheral.core.integration;

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

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import openperipheral.core.AdapterManager;
import openperipheral.core.adapter.mystcraft.AdapterWritingDesk;

import cpw.mods.fml.common.FMLLog;

public class ModuleMystcraft {

  public static void init() {
    AdapterManager.addPeripheralAdapter(new AdapterWritingDesk());

  }

  public static void appendMystcraftInfo(Map map, ItemStack stack) {
	  if (stack != null){
	    Item item = stack.getItem();
	    if (item != null){
	      
	      if ("item.myst.page".equals(item.getUnlocalizedName())){
	        addStringFromNBT(map, stack, "symbol", "symbol");
	      }else if ("item.myst.linkbook".equals(item.getUnlocalizedName())){
	        addStringFromNBT(map, stack, "destination", "agename");
	        addLinkingBookFlags(map, stack);
	      }
	    }
	  }
	}
	
  private static void addLinkingBookFlags(Map map, ItemStack stack) {
    Map<String,Boolean> flags = new HashMap<String,Boolean>();
    map.put("flags", flags);
    if (stack.hasTagCompound()) {
      NBTTagCompound tag = stack.getTagCompound();
      if (tag.hasKey("Flags")){
        for(NBTBase s:(Collection<NBTBase>)tag.getCompoundTag("Flags").getTags()){
          flags.put(s.getName(), Boolean.TRUE);
        }
        
      }
    }         
  }
  
  private static void addStringFromNBT(Map map, ItemStack stack, String outputName, String nbtTagName) {
    if (stack.hasTagCompound()) {
      NBTTagCompound tag = stack.getTagCompound();
      if (tag.hasKey(nbtTagName)) {
        map.put(outputName, tag.getString(nbtTagName));
      }
    }
  }
}

@@ -35,16 +35,66 @@
 ation;%0A%0A
+import java.awt.List;%0Aimport java.util.ArrayList;%0A
 import j
@@ -581,17 +581,16 @@
 esk());%0A
-%0A
   %7D%0A%0A  p
@@ -743,17 +743,48 @@
 %0A%09      
-%0A
+if (stack.hasTagCompound()) %7B%0A  
 %09      i
@@ -831,32 +831,34 @@
 alizedName()))%7B%0A
+  
 %09        addStri
@@ -896,24 +896,26 @@
  %22symbol%22);%0A
+  
 %09      %7Delse
@@ -976,16 +976,18 @@
 me()))%7B%0A
+  
 %09       
@@ -1043,16 +1043,18 @@
 name%22);%0A
+  
 %09       
@@ -1087,16 +1087,57 @@
 stack);%0A
+  %09        addCoordinates(map, stack);%0A  
 %09      %7D
@@ -1146,19 +1146,415 @@
     
+  
 %7D%0A%09  
+  
 %7D%0A%09
+  
 %7D%0A%09
+%7D%0A%0A  private static void addCoordinates(Map map, ItemStack stack) %7B%0A    NBTTagCompound tag = stack.getTagCompound();%0A    HashMap%3CInteger, Integer%3E pos = new HashMap%3CInteger, Integer%3E();%0A    map.put(%22spawn%22, pos);%0A    pos.put(1,tag.getInteger(%22SpawnX%22));%0A    pos.put(2,tag.getInteger(%22SpawnY%22));%0A    pos.put(3,tag.getInteger(%22SpawnZ%22));%0A    map.put(%22spawnYaw%22,tag.getFloat(%22SpawnYaw%22));%0A  %7D%0A
 %0A  p
@@ -1716,44 +1716,8 @@
 s);%0A
-    if (stack.hasTagCompound()) %7B%0A  
     
@@ -1753,34 +1753,32 @@
 tTagCompound();%0A
-  
     if (tag.hasK
@@ -1797,18 +1797,16 @@
 %7B%0A      
-  
 for(NBTB
@@ -1880,18 +1880,16 @@
         
-  
 flags.pu
@@ -1924,22 +1924,18 @@
 ;%0A      
-  %7D%0A  
+%7D%0A
       %0A 
@@ -1941,26 +1941,9 @@
     
-  %7D%0A    %7D         
+%7D
 %0A  %7D
@@ -2055,44 +2055,8 @@
 ) %7B%0A
-    if (stack.hasTagCompound()) %7B%0A  
     
@@ -2100,26 +2100,24 @@
 ound();%0A    
-  
 if (tag.hasK
@@ -2140,18 +2140,16 @@
 %7B%0A      
- 
- 
 map.put(
@@ -2188,24 +2188,16 @@
 Name));%0A
-      %7D%0A
     %7D%0A  

add coordinates to linking books