
#ifndef UNITITEMS_H
#define UNITITEMS_H
#include "ItemSlot.h"
#define maxslots 10
#include "ItemList.h"

class UnitItems
{


private:


public:
string unitItems;
UnitItems();
ItemSlot items[maxslots];
int usedslots;
int slotsperpage;
bool additem(int,int);
bool useitem(int);
string toString();
void fromString();
int Get_Value(ItemList* );

};

UnitItems::UnitItems()
{
unitItems= "none";
usedslots=0;
slotsperpage=5;
}



bool UnitItems::additem(int id,int itemsnum)
{
if(usedslots>0)
	{
		for(int i = 0; i<usedslots; i++)
		{
			if(items[i].itemid==id)
			{
				items[i].nuberofitems+=itemsnum;
				return true;
			}
		}
		
	}
	if(usedslots!=maxslots)
		{
				usedslots++;
				items[usedslots].itemid=id;
				items[usedslots].nuberofitems=itemsnum;
				items[usedslots].something_in_slot=true;
				return true;
		}
return false;
}


	bool UnitItems::useitem(int slot)
	{
		if(items[slot].nuberofitems >0)
		{
			items[slot].nuberofitems--;
			if(items[slot].nuberofitems ==0)
			{
				items[slot].something_in_slot=false;
			
			

				ItemSlot temp = items[slot];
				items[slot] = items[usedslots];
				items[usedslots] = temp;
			
				usedslots--;
				
			}
			return true;
		}

	return false;
	}






void UnitItems::fromString()
{
	
if(unitItems != "none")
{
//textout(screen,font,statholder.c_str(),1,400,makecol(0,255,255));
//Sleep(3770);
	int j= 0;
	int i = 0;
//maybey i acn make the conver for sring to char* right in here
	string temps[41]; //arry to hold data
	
	while (unitItems[i]!= '`') //stay in untl you fing the ` at the end
	{
		if (unitItems[i] != ',')//if found , dont add it to the data and move on to next char
		{
			temps[j] += unitItems[i]; //add char to temp spot
			i++;
		}
		else
		{
//textout(screen,font,temps[1].c_str(),1,200,makecol(0,0,255));
			i++;
			j++;

		}

	}
	usedslots=StringToNumber(temps[0]);
	 j=1;
	for(i=0;i< usedslots;i++)
	{
		items[i].itemid=StringToNumber(temps[i+j]);
		items[i].nuberofitems=StringToNumber(temps[i+j+1]);
		items[i].something_in_slot=true;
		j++;
	}


}
//Sleep(5000);
//socketid = StringToNumber(temps[17]);// scoketId
}


string UnitItems::toString()
{

	unitItems=NumberToString(usedslots);
	for(int i=0;i< usedslots;i++)
	{
		unitItems +=  "," +NumberToString(items[i].itemid)+ "," +NumberToString(items[i].nuberofitems);
	}
	unitItems +="`";
	//textout(screen,font,unitItems.c_str(),100,50*id,makecol(255,255,255));
//Sleep(10000);

	return unitItems;
		

}



int UnitItems::Get_Value(ItemList *Itemsinlist)
{
int total=0;

	for(int i=0;i< usedslots;i++)
	{
		total +=  (Itemsinlist->get_value(items[i].itemid) * items[i].nuberofitems);
	}
	return total;


}















#endif