/*
	PostNuke Wasteland Item Storage
	Author:	NeoPhoenix
	Date:	01.09.2012
*/

#if defined EX_isto_included
	#endinput
#endif
#define EX_isto_included
#include "libraries/lib_item.inc"

stock item::store_OnPlayerInit(playerid)
{
	db_query_once(SQL_DB,"DELETE pnwl_user_item_storage WHERE anzahl<=0 OR item_id<=0");	
	format(query,sizeof(query),"SELECT * FROM pnwl_user_item_storage WHERE userid='%d'",PlayerInfo[playerid][PLAYER_ID]);
	SQL_RES = db_query(SQL_DB,query);
	new count = 0,unique = 0;
	if(db_num_rows(SQL_RES) >= 1)
	{
		new item_cache[2];
		do
		{
			db_get_field_assoc(SQL_RES,"item_id",string,sizeof(string));
			item_cache[0] = strval(string);
			if(item_cache[0] <= 0 || item_cache[0] > MAX_ITEMS) continue;
			db_get_field_assoc(SQL_RES,"anzahl",string,sizeof(string));
			item_cache[1] = strval(string);
			unique++;
			count += item_cache[1];
		}
		while(db_next_row(SQL_RES));
	}
	db_free_result(SQL_RES);
	SetPVarInt(playerid,"itemstore_unique",unique);
	if(count >= 1)
	{
		format(string,sizeof(string),"INFO: Du hast %d Items (%d Unique) im Lager",count,unique);
		SendClientMessage(playerid,GRUEN,string);
	}
	return 1;
}

stock item::store_GetItemList(playerid,&start,&end,dest[][],destsize=sizeof(dest))
{
	if(start < 0)	start = 0;
	if(end < start)	end = start;
	format(query,sizeof(query),"SELECT * FROM pnwl_user_item_storage WHERE userid='%d' LIMIT %d, %d",PlayerInfo[playerid][PLAYER_ID],start,end);
	new DBResult:res = db_query(SQL_DB,query),i=0;
	if(db_num_rows(res) >= 1)
	{
		do
		{
			db_get_field_assoc(res,"item",string,sizeof(string));
			dest[i][0] = strval(string);
			db_get_field_assoc(res,"anzahl",string,sizeof(string));
			dest[i][1] = strval(string);
			if(++i >= destsize) break;
		}
		while(db_next_row(res));
	}
	db_free_result(res);	
	return 1;
}

stock item::store_CountItems(playerid,itemid)
{
	format(query,sizeof(query),"SELECT * FROM pnwl_user_item_storage WHERE userid='%d' AND itemid='%d'",PlayerInfo[playerid][PLAYER_ID],itemid);
	new DBResult:res	= db_query(SQL_DB,query),
		anzahl			= 0;
	if(db_num_rows(res))
	{
		db_get_field_assoc(res,"anzahl",string,sizeof(string));
		anzahl = strval(string);
	}
	db_free_result(res);
	return anzahl;
}

forward bool:item::store_ChangeItemCount(playerid,itemid,anzahl,bool:_add=true);
stock bool:item::store_ChangeItemCount(playerid,itemid,anzahl,bool:_add=true)
{
	new oldcount	= item::store_CountItems(playerid,itemid),
		newcount	= _add?oldcount+anzahl:anzahl;	
	if(oldcount == 0 && newcount <= 0) return true;
	if(newcount >= 1)
	{
		if(oldcount == 0)
		{//Insert
			format(query,sizeof(query),"INSERT INTO pnwl_user_item_storage (userid,itemid,anzahl)",PlayerInfo[playerid][PLAYER_ID],itemid,newcount);
			db_query_once(SQL_DB,query);
			SetPVarInt(playerid,"itemstore_unique",GetPVarInt(playerid,"itemstore_unique")+1);
		}
		else
		{//Update
			format(query,sizeof(query),"UPDATE pnwl_user_item_storage SET anzahl=%d WHERE userid='%d' AND itemid='%d'",newcount,PlayerInfo[playerid][PLAYER_ID],itemid);
			db_query_once(SQL_DB,query);
		}
	}
	else if(newcount <= 0)
	{
		if(oldcount >= 1)
		{//Delete
			format(query,sizeof(query),"DELETE * FROM pnwl_user_item_storage WHERE userid='%d' AND itemid='%d'",PlayerInfo[playerid][PLAYER_ID],itemid);
			db_query_once(SQL_DB,query);
			SetPVarInt(playerid,"itemstore_unique",GetPVarInt(playerid,"itemstore_unique")-1);
		}
	}
	return true;
}

forward bool:item::store_IsAtItemstorage(playerid);
stock bool:item::store_IsAtItemstorage(playerid)
{
	new hid = PlayerEvent[playerid][InteractHouse];
	if(hid != -1 && HouseInfo[hid][hUpgrade][5] >= 1) return true;
	/*{
		switch(HouseInfo[hid][hInterior])
		{
			case 2:		if(IsPlayerInRangeOfPoint(playerid,3.0,801,-2727.6999,100.9000)) return true;
			default:	if(IsPlayerInRangeOfPoint(playerid,3.0,801,-2727.6999,100.9000)) return true;
		}
	}*/
	return false;
}








