package amerifrance.guideapi.proxy;

import amerifrance.guideapi.api.GuideAPI;
import amerifrance.guideapi.api.IGuideItem;
import amerifrance.guideapi.api.impl.Book;
import amerifrance.guideapi.api.impl.abstraction.CategoryAbstract;
import amerifrance.guideapi.api.impl.abstraction.EntryAbstract;
import amerifrance.guideapi.api.util.NBTBookTags;
import amerifrance.guideapi.gui.GuiCategory;
import amerifrance.guideapi.gui.GuiEntry;
import amerifrance.guideapi.gui.GuiHome;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class CommonProxy implements IGuiHandler {

    @Override
    public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        return null;
    }

    @Override
    public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
        ItemStack stack = player.getHeldItemOffhand();
        if (stack == null || !(stack.getItem() instanceof IGuideItem))
            stack = player.getHeldItemMainhand();

        if (stack != null && stack.getItem() instanceof IGuideItem) {
            Book book = GuideAPI.BOOKS.getValues().get(ID);
            if (stack.hasTagCompound()) {
                NBTTagCompound tagCompound = stack.getTagCompound();
                if (tagCompound.hasKey(NBTBookTags.ENTRY_TAG) && tagCompound.hasKey(NBTBookTags.CATEGORY_TAG)) {
                    CategoryAbstract category = book.getCategoryList().get(tagCompound.getInteger(NBTBookTags.CATEGORY_TAG));
                    EntryAbstract entry = category.entries.get(new ResourceLocation(tagCompound.getString(NBTBookTags.ENTRY_TAG)));
                    int pageNumber = tagCompound.getInteger(NBTBookTags.PAGE_TAG);
                    GuiEntry guiEntry = new GuiEntry(book, category, entry, player, stack);
                    guiEntry.pageNumber = pageNumber;
                    return guiEntry;
                } else if (tagCompound.hasKey(NBTBookTags.CATEGORY_TAG)) {
                    CategoryAbstract category = book.getCategoryList().get(tagCompound.getInteger(NBTBookTags.CATEGORY_TAG));
                    int entryPage = tagCompound.getInteger(NBTBookTags.ENTRY_PAGE_TAG);
                    GuiCategory guiCategory = new GuiCategory(book, category, player, stack);
                    guiCategory.entryPage = entryPage;
                    return guiCategory;
                } else {
                    int categoryNumber = tagCompound.getInteger(NBTBookTags.CATEGORY_PAGE_TAG);
                    GuiHome guiHome = new GuiHome(book, player, stack);
                    guiHome.categoryPage = categoryNumber;
                    return guiHome;
                }
            }

            return new GuiHome(book, player, stack);
        }

        return null;
    }

    public void playSound(ResourceLocation sound) {
    }

    public void openEntry(Book book, CategoryAbstract categoryAbstract, EntryAbstract entryAbstract, EntityPlayer player, ItemStack stack) {
    }

    public void initColors() {
    }
}

@@ -1352,16 +1352,38 @@
 et(ID);%0A
+            try %7B%0A    
         
@@ -1432,16 +1432,20 @@
         
+  
+  
 NBTTagCo
@@ -1485,24 +1485,28 @@
 Compound();%0A
+    
             
@@ -1618,32 +1618,36 @@
                 
+ 
+   
 CategoryAbstract
@@ -1728,32 +1728,36 @@
 CATEGORY_TAG));%0A
+    
                 
@@ -1888,24 +1888,28 @@
             
+ 
+   
 int pageNumb
@@ -1951,32 +1951,36 @@
 Tags.PAGE_TAG);%0A
+    
                 
@@ -2075,16 +2075,20 @@
         
+ 
+   
 guiEntry
@@ -2125,32 +2125,36 @@
                 
+    
 return guiEntry;
@@ -2166,24 +2166,28 @@
             
+ 
+   
 %7D else if (t
@@ -2225,32 +2225,36 @@
 ATEGORY_TAG)) %7B%0A
+    
                 
@@ -2379,24 +2379,28 @@
             
+    
 int entryPag
@@ -2447,32 +2447,36 @@
 NTRY_PAGE_TAG);%0A
+    
                 
@@ -2573,16 +2573,20 @@
         
+    
 guiCateg
@@ -2612,16 +2612,20 @@
 ryPage;%0A
+    
         
@@ -2668,24 +2668,28 @@
             
+    
 %7D else %7B%0A   
@@ -2685,16 +2685,20 @@
  else %7B%0A
+    
         
@@ -2805,16 +2805,20 @@
         
+    
 GuiHome 
@@ -2881,16 +2881,20 @@
         
+   
+ 
 guiHome.
@@ -2936,32 +2936,36 @@
                 
+    
 return guiHome;%0A
@@ -2980,17 +2980,165 @@
         
-%7D
+    %7D%0A                %7D%0A            %7D catch (Exception e) %7B%0A                // No-op: Catching if the saved page/entry/category doesn't exist anymore
 %0A       

Fix crash if book attempts to open non-existent page