/*
 * Copyright (c) 2015 Legend Zero LLC
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.legendzero.lzlib.gui;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;

import java.util.function.Consumer;
import java.util.function.Function;

public class GuiContents {

    private final Plugin plugin;
    private final InventoryType type;
    private final String name;
    private final int size;
    private final Consumer<? super Player> consumer;
    private GuiContents parent;
    private final GuiItem[] itemStackFunctions;
    private final GuiClickHandler[] clickHandlers;

    public GuiContents(Plugin plugin, InventoryType type, String name, int size, Consumer<? super Player> consumer) {
        this.plugin = plugin;
        this.type = type;
        this.name = name;
        this.size = size;
        this.consumer = consumer;
        this.parent = null;
        this.itemStackFunctions = new GuiItem[this.size];
        this.clickHandlers = new GuiClickHandler[this.size];
    }

    public GuiContents(Plugin plugin, InventoryType type, String name, int size) {
        this(plugin, type, name, size, null);
    }

    public Plugin getPlugin() {
        return this.plugin;
    }

    public InventoryType getType() {
        return this.type;
    }

    public String getName() {
        return this.name;
    }

    public int getSize() {
        return this.size;
    }

    public Consumer<? super Player> getConsumer() {
        return this.consumer;
    }

    public GuiContents getParent() {
        return this.parent;
    }

    public boolean hasItem(int slot) {
        return this.itemStackFunctions[slot] != null;
    }

    public boolean hasAction(int slot) {
        return this.clickHandlers[slot] != null;
    }

    public ItemStack[] getItems(Player player) {
        ItemStack[] contents = new ItemStack[this.size];
        for (int i = 0; i < this.size; i++) {
            contents[i] = this.itemStackFunctions[i].apply(player);
        }
        return contents;
    }

    public void setParent(GuiContents parent) {
        this.parent = parent;
    }

    public void set(int slot, GuiItem item) {
        this.itemStackFunctions[slot] = item;
    }

    public void set(int slot, GuiClickHandler clickHandler) {
        this.clickHandlers[slot] = clickHandler;
    }

    public void set(int slot, GuiItem item, GuiClickHandler clickHandler) {
        this.set(slot, item);
        this.set(slot, clickHandler);
    }

    public void fillEmpty(GuiItem item) {
        for (int i = 0; i < this.size; i++) {
            if (this.itemStackFunctions[i] == null) {
                this.itemStackFunctions[i] = item;
            }
        }
    }

    public void onClick(InventoryClickEvent event) {
        GuiClickHandler clickHandler = this.clickHandlers[event.getSlot()];
        if (clickHandler != null) {
            clickHandler.accept(event);
        }
    }

    public void open(Player player) {
        InventoryHolder inventoryHolder = new GuiInventoryHolder(this, player);
        Inventory inventory = inventoryHolder.getInventory();
        player.openInventory(inventory);
    }

    public void update(Inventory inventory, Player player) {
        this.consumer.accept(player);
        inventory.setContents(this.getItems(player));
    }

    public void back(Player player) {
        if (this.parent != null) {
            this.parent.open(player);
        }
    }

    public void close(Player player) {
        player.closeInventory();
    }
}

@@ -1482,44 +1482,8 @@
 mer;
-%0Aimport java.util.function.Function;
 %0A%0Apu
@@ -3349,32 +3349,36 @@
  public void set
+Item
 (int slot, GuiIt
@@ -3452,32 +3452,44 @@
  public void set
+ClickHandler
 (int slot, GuiCl
@@ -3659,24 +3659,28 @@
     this.set
+Item
 (slot, item)
@@ -3697,16 +3697,28 @@
 this.set
+ClickHandler
 (slot, c

Remove possible ambiguity in set method.