package fr.epicanard.globalmarketchest.gui;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.NoSuchElementException;

import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import fr.epicanard.globalmarketchest.gui.shops.ShopInterface;

public class InventoryGUI {
  private Inventory inv;
  private Deque<ShopInterface> shopStack = new ArrayDeque<ShopInterface>();

  public InventoryGUI() {
    this.inv = Bukkit.createInventory(null, 54, "§8GlobalMarketChest");
  }

  public Boolean inventoryEquals(Inventory i) {
    return this.inv.equals(i);
  }

  /**
   * Open current inventory for the specified player
   * 
   * @param player
   */
  public void open(Player player) {
    player.openInventory(this.inv);
  }

  /**
   * Close current inventory for the specified player
   * 
   * @param player
   */
  public void close(Player player) {
    player.closeInventory();
  }
  
  /**
   * Unload temporary interface and come back to principal
   */
  public void unloadTempInterface() {
    try {
      do {
        this.shopStack.pop().unload();
      } while(this.shopStack.peek().isTemp());
      this.shopStack.peek().load(this.inv);
    } catch (NoSuchElementException e) {}
  }
  
  /**
   * Unload last loaded Interface and load the previous one
   */
  public void unloadLastInterface() {
    try {
      this.shopStack.pop().unload();
      this.shopStack.peek().load(this.inv);      
    } catch (NoSuchElementException e) {}
  }

  /**
   * Load interface with a specific name
   * 
   * @param name
   */
  public void loadInterface(String name) {
    try {
      ShopInterface shop = (ShopInterface) Class.forName("fr.epicanard.globalmarketchest.gui.shops." + name).newInstance();
      shop.load(this.inv);
      
      this.shopStack.push(shop);
    } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
}

@@ -131,16 +131,43 @@
 ception;
+%0Aimport java.util.Optional;
 %0A%0Aimport
@@ -219,16 +219,47 @@
 Player;%0A
+import org.bukkit.event.Event;%0A
 import o
@@ -1117,24 +1117,50 @@
 %7B%0A    try %7B%0A
+      ShopInterface peek;%0A
       do %7B%0A 
@@ -1207,16 +1207,17 @@
     
-%7D while(
+  peek = 
 this
@@ -1237,47 +1237,104 @@
 ek()
-.isTemp());%0A      this.shopStack.peek()
+;%0A      %7D while(peek != null && peek.isTemp());%0A      Optional.ofNullable(peek).ifPresent(e -%3E e
 .loa
@@ -1340,24 +1340,25 @@
 ad(this.inv)
+)
 ;%0A    %7D catc
@@ -1555,24 +1555,44 @@
 ad();%0A      
+Optional.ofNullable(
 this.shopSta
@@ -1600,16 +1600,34 @@
 k.peek()
+).ifPresent(e -%3E e
 .load(th
@@ -1637,15 +1637,232 @@
 inv)
-;
+);%0A
     
-  
+%7D catch (NoSuchElementException e) %7B%7D%0A  %7D%0A%0A  /**%0A   * Unload all interface%0A   * %0A   * @param name%0A   */%0A  public void unloadAllInterface() %7B%0A    try %7B%0A      this.shopStack.pop().unload();%0A      this.shopStack.clear();
 %0A   
@@ -2154,24 +2154,108 @@
 Instance();%0A
+      Optional.ofNullable(this.shopStack.peek()).ifPresent(ShopInterface::unload);%0A%0A
       shop.l
@@ -2273,15 +2273,8 @@
 v);%0A
-      %0A
     

Update InventoryGUI