/*
 * Copyright (c) 2016 MCPhoton <http://mcphoton.org> and contributors.
 *
 * This file is part of the Photon API <https://github.com/mcphoton/Photon-API>.
 *
 * The Photon API is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Photon API is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.mcphoton.item;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Objects;
import org.mcphoton.Photon;

/**
 * A stack of items.
 *
 * @author TheElectronWill
 */
public class ItemStack {

	protected ItemType type;
	protected int maxSize, size, damage;

	public ItemStack(ItemType type, int maxSize, int size, int damage) {
		this.type = type;
		this.maxSize = maxSize;
		this.size = size;
		this.damage = damage;
	}

	public ItemStack(ItemType type) {
		this(type, 64, 0, 0);
	}

	/**
	 * @return true if the stack is empty.
	 */
	boolean isEmpty() {
		return size == 0;
	}

	/**
	 * @return true if the stack is full, ie if its size is equal to its max size.
	 */
	boolean isFull() {
		return size == maxSize;
	}

	/**
	 * @return the stack's size.
	 */
	int getSize() {
		return size;
	}

	/**
	 * Adds the given value to the current size value.
	 *
	 * @param delta the value to add
	 */
	void addSize(int delta) {
		size += delta;
	}

	/**
	 * Sets the stack's size.
	 */
	void setSize(int size) {
		this.size = size;
	}

	/**
	 * @return the max stack's size.
	 */
	int getMaxSize() {
		return maxSize;
	}

	/**
	 * @return the item's type.
	 */
	ItemType getType() {
		return type;
	}

	/**
	 * @return the stack's damage.
	 */
	int getDamage() {
		return damage;
	}

	/**
	 * Adds the given value to the current damage value.
	 *
	 * @param delta the value to add
	 */
	void addDamage(int delta) {
		this.damage += delta;
	}

	/**
	 * Sets the stack's damage.
	 */
	void setDamage(int damage) {
		this.damage = damage;
	}

	public static ItemStack readFrom(ByteBuffer buff) throws IOException {
		int typeId = buff.getShort();
		if (typeId == -1) {
			return new ItemStack(Photon.getGameRegistry().getRegisteredItem(0));
		} else {
			int size = buff.get();
			int damage = buff.getShort();
			//TODO read NBT data like enchantments
			return new ItemStack(Photon.getGameRegistry().getRegisteredItem(typeId), 64, size, damage);
		}
	}

	@Override
	public String toString() {
		return "ItemStack{" + "type=" + type + ", maxSize=" + maxSize + ", size=" + size + ", damage=" + damage + '}';
	}

	@Override
	public int hashCode() {
		int hash = 3;
		hash = 47 * hash + Objects.hashCode(this.type);
		hash = 47 * hash + this.maxSize;
		hash = 47 * hash + this.size;
		hash = 47 * hash + this.damage;
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj instanceof ItemStack) {
			ItemStack other = (ItemStack) obj;
			return size == other.size && type.equals(other.type) && maxSize == other.maxSize;
		}
		return false;
	}

}

@@ -1050,17 +1050,16 @@
 Stack %7B%0A
-%0A
 %09protect
@@ -2783,24 +2783,33 @@
 ), 64, size,
+%0A%09%09%09%09%09%09%09%09
  damage);%0A%09%09
@@ -2806,16 +2806,16 @@
 amage);%0A
-
 %09%09%7D%0A%09%7D%0A%0A
@@ -2874,16 +2874,22 @@
 mStack%7B%22
+%0A%09%09%09  
  + %22type
@@ -2894,15 +2894,27 @@
 pe=%22
+%0A%09%09%09  
  + type
+%0A%09%09%09  
  + %22
@@ -2924,16 +2924,22 @@
 axSize=%22
+%0A%09%09%09  
  + maxSi
@@ -2940,16 +2940,22 @@
  maxSize
+%0A%09%09%09  
  + %22, si
@@ -2962,15 +2962,27 @@
 ze=%22
+%0A%09%09%09  
  + size
+%0A%09%09%09  
  + %22
@@ -2991,16 +2991,22 @@
 damage=%22
+%0A%09%09%09  
  + damag
@@ -3006,16 +3006,22 @@
 + damage
+%0A%09%09%09  
  + '%7D';%0A
@@ -3401,17 +3401,16 @@
 emStack)
- 
 obj;%0A%09%09%09
@@ -3518,7 +3518,5 @@
 %0A%09%7D%0A
-%0A%7D%0A
+%7D

Format ItemStack