package team.unstudio.udpl.ui;

import java.util.function.Consumer;

import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.ItemStack;

/**
 * 界面槽
 */
public class Slot implements Cloneable{

	private UI parent;
	private ItemStack itemStack;
	private int slot;
	private boolean operable;
	private Consumer<InventoryClickEvent> onLeftClick;
	private Consumer<InventoryClickEvent> onRightClick;
	private Consumer<InventoryClickEvent> onShiftLeftClick;
	private Consumer<InventoryClickEvent> onShiftRightClick;
	private Consumer<InventoryClickEvent> onDoubleClick;
	private Consumer<InventoryClickEvent> onMiddleClick;
	
	public Slot() {}
	
	public Slot(int slot){
		this.slot = slot;
	}
	
	public Slot(ItemStack itemStack,int slot){
		this(slot);
		this.itemStack = itemStack;
	}
	
	public void onClick(InventoryClickEvent event){
		switch (event.getClick()) {
		case LEFT:
			if(onLeftClick != null)
				onLeftClick.accept(event);
			break;
		case RIGHT:
			if(onRightClick != null)
				onRightClick.accept(event);
			break;
		case SHIFT_LEFT:
			if(onShiftLeftClick != null)
				onShiftLeftClick.accept(event);
			break;
		case SHIFT_RIGHT:
			if(onShiftRightClick != null)
				onShiftRightClick.accept(event);
			break;
		case DOUBLE_CLICK:
			if(onDoubleClick != null)
				onDoubleClick.accept(event);
			break;
		case MIDDLE:
			if(onMiddleClick != null)
				onMiddleClick.accept(event);
			break;
		default:
			break;
		}
	}
	
	/**
	 * 获取物品
	 */
	public ItemStack getItemStack() {
		return parent != null ? parent.getInventory().getItem(slot) : itemStack;
	}
	
	/**
	 * 设置物品
	 * @param itemstack
	 */
	public void setItemStack(ItemStack itemstack) {
		this.itemStack = itemstack;
		updateItem();
	}
	
	/**
	 * 获取所在格子
	 * @return
	 */
	public int getSlot() {
		return slot;
	}
	
	/**
	 * 设置所在格子
	 * @param slot
	 */
	public void setSlot(int slot) {
		this.slot = slot;
		updateItem();
	}

	/**
	 * 获取父UI
	 * @return
	 */
	public UI getParent() {
		return parent;
	}

	/**
	 * 设置父UI
	 * @param parent
	 */
	public void setParent(UI parent) {
		this.parent = parent;
		updateItem();
	}

	/**
	 * 是否可以操作
	 * @return
	 */
	public boolean isOperable() {
		return operable;
	}

	/**
	 * 设置是否可以操作
	 * @param operable
	 */
	public void setOperable(boolean operable) {
		this.operable = operable;
	}
	
	public Consumer<InventoryClickEvent> getOnLeftClick() {
		return onLeftClick;
	}

	public void setOnLeftClick(Consumer<InventoryClickEvent> onLeftClick) {
		this.onLeftClick = onLeftClick;
	}

	public Consumer<InventoryClickEvent> getOnRightClick() {
		return onRightClick;
	}

	public void setOnRightClick(Consumer<InventoryClickEvent> onRightClick) {
		this.onRightClick = onRightClick;
	}

	public Consumer<InventoryClickEvent> getOnShiftLeftClick() {
		return onShiftLeftClick;
	}

	public void setOnShiftLeftClick(Consumer<InventoryClickEvent> onShiftLeftClick) {
		this.onShiftLeftClick = onShiftLeftClick;
	}

	public Consumer<InventoryClickEvent> getOnShiftRightClick() {
		return onShiftRightClick;
	}

	public void setOnShiftRightClick(Consumer<InventoryClickEvent> onShiftRightClick) {
		this.onShiftRightClick = onShiftRightClick;
	}

	public Consumer<InventoryClickEvent> getOnDoubleClick() {
		return onDoubleClick;
	}

	public void setOnDoubleClick(Consumer<InventoryClickEvent> onDoubleClick) {
		this.onDoubleClick = onDoubleClick;
	}

	public Consumer<InventoryClickEvent> getOnMiddleClick() {
		return onMiddleClick;
	}

	public void setOnMiddleClick(Consumer<InventoryClickEvent> onMiddleClick) {
		this.onMiddleClick = onMiddleClick;
	}

	protected void updateItem(){
		if(parent!=null)
			parent.getInventory().setItem(slot, itemStack);
	}
	
	@Override
	public Slot clone(){
		Slot button = null;
		try {
			button = (Slot) super.clone();
			button.itemStack = itemStack.clone();
			button.slot = slot;
			button.operable = operable;
			button.onLeftClick = onLeftClick;
			button.onRightClick = onRightClick;
			button.onDoubleClick = onDoubleClick;
			button.onMiddleClick = onMiddleClick;
			button.onShiftLeftClick = onShiftLeftClick;
			button.onShiftRightClick = onShiftRightClick;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return button;
	}
}

@@ -325,16 +325,65 @@
 rable;%0D%0A
+%09private Consumer%3CInventoryClickEvent%3E onClick;%0D%0A
 %09private
@@ -934,16 +934,64 @@
 vent)%7B%0D%0A
+%09%09if(onClick!=null)%0D%0A%09%09%09onClick.accept(event);%0D%0A
 %09%09switch
@@ -2537,32 +2537,210 @@
 erable;%0D%0A%09%7D%0D%0A%09%0D%0A
+%09public Consumer%3CInventoryClickEvent%3E getOnClick() %7B%0D%0A%09%09return onClick;%0D%0A%09%7D%0D%0A%0D%0A%09public void setOnClick(Consumer%3CInventoryClickEvent%3E onClick) %7B%0D%0A%09%09this.onClick = onClick;%0D%0A%09%7D%0D%0A%0D%0A
 %09public Consumer

Update UI