/*!
\brief Check if the player is skilled enough and have requested items
\return bool can or can't 
\param pc the player
\param inMenu if write a sysmessage on error
\todo Add message if haven't enough item..
*/
bool cMakeItem::checkReq( P_CHAR pc, bool inMenu, cRawItem* def )
{

    if( pc->IsGM() ) 
		return true;

    if( (skillToCheck!=INVALID) && (pc->skill[skillToCheck]<minskill) ) {
        if( !inMenu ) 
			pc->sysmsg(TRANSLATE("You're not enough skilled"));
        return false;
    }

	for( int i=0; i<2; ++i ) {
        cRawItem& raw = reqitems[i];
		if( raw.id!=0 ) {
			bool have = ( def!=NULL )? (def[i].number>=raw.number) : ( pc->CountItems( raw.id, raw.color)>= raw.number );
			if( !have ) {
				if( !inMenu )
					pc->sysmsg(TRANSLATE("You've not enough resources"));
				return false;
			}
        }
    }


    return true;
}
void cMakeMenu::loadFromScript( P_CHAR pc )
{
	VALIDATEPC(pc);

	NXWCLIENT ps = pc->getClient();
	if( ps==NULL ) return;

    cScpIterator* iter = Scripts::Create->getNewIterator("SECTION MAKEMENU %i", section);
    if (iter==NULL) return;

	std::vector<std::string> names; //name
	std::vector<std::string> models; //models
    
	this->mat[0].number = ( mat[0].id!=0 )? pc->CountItems( mat[0].id, mat[0].color ) : 0;
	this->mat[1].number = ( mat[1].id!=0 )? pc->CountItems( mat[1].id, mat[1].color ) : 0;

	//da passare a checkReq PDPD
	

	cMakeItem* imk = NULL;
    pc->making=skill;

	oldmenu->style=MENUTYPE_ICONLIST;

/* makemenu menu
SECTION MAKEMENU 1
{
    Blacksmithing
    1416 Armor
    RESOURCE 10
    SKILL 500
    MAKEMENU 3
    13BA Weapons
    RESOURCE 3
    SKILL 100
    MAKEMENU 4
    1BC3 Shields
    RESOURCE 8
    SKILL 250
    MAKEMENU 2
    09ed Misc
    RESOURCE 6
    SKILL 150
    MAKEMENU 105
}
*/
/* makemenu item
SECTION MAKEMENU 2002
{
    Golden Ring Mail
    13F2 Gloves
    RESOURCE 10
    SKILL 338
    ADDITEM $item_golden_ringmail_gloves
    13EF Sleeves
    RESOURCE 14
    SKILL 385
    ADDITEM $item_golden_ringmail_sleeves
    13F1 Leggings
    RESOURCE 16
    SKILL 408
    ADDITEM $item_golden_ringmail_leggings
    13ED Tunic
    RESOURCE 18
    SKILL 432
    ADDITEM $item_golden_ringmail_tunic
}
*/

enum MakeParamType {
MAKE_NEED_NAME = 0, //Blacksmithing
MAKE_NEED_INFO, //1416 Armor
MAKE_NEED_RESOURCE, //RESOURCE 10
MAKE_NEED_SKILL, //SKILL 500
MAKE_NEED_MENUORITEM, //MAKEMENU 3  or ADDITEM $item_golden_ringmail_tunic
};

	MakeParamType type = MAKE_NEED_NAME;
	bool error=false;
	bool canSee = true;
	int item = INVALID;

    int loopexit=0;
    do
    {
		std::string lha;
		std::string rha;
        
	    iter->parseLine(lha, rha);

		if( lha[0]=='{' )
			continue;

		if( lha[0]=='}' )
			break;

		switch( type ) {

			case MAKE_NEED_NAME: //Blacksmithing
				string2wstring( lha, oldmenu->title );
				type = MAKE_NEED_INFO;
				break;

			case MAKE_NEED_INFO: { //1416 Armor
				item++;
				names.push_back( rha );
				models.push_back( lha );
				type = MAKE_NEED_RESOURCE;
				break;
			}
			case MAKE_NEED_RESOURCE: //RESOURCE 10
		        if( lha!="RESOURCE" ) {
					if ( item > INVALID ) //Luxor
						LogWarning("create.xss, MAKEMENU %i: Expected 'RESOURCE <num>' after '%s'!", section, names[item].c_str() );
					error=true;
				}
				else {
					int needs = str2num(rha);

					imk = new cMakeItem();
					
					imk->skillToCheck=this->skill;
					imk->reqitems[0].id=mat[0].id;
					imk->reqitems[0].color=mat[0].color;
					imk->reqitems[0].number=needs;
					imk->reqitems[1].id=mat[1].id;
					imk->reqitems[1].color=mat[1].color;
					imk->reqitems[1].number=needs;

					type=MAKE_NEED_SKILL;
				}
				break;
			case MAKE_NEED_SKILL:  //SKILL 500
				if( lha!="SKILL" )
				{
					LogWarning("create.xss, MAKEMENU %i: Expected 'SKILL <num>' after RESOURCE !", section );
					error=true;
		        }
				else {

					imk->minskill=str2num(rha);
				    imk->maxskill=imk->minskill*SrvParms->skilllevel; // by Magius(CHE)
					if( imk->maxskill<200 )
						imk->maxskill=200;

					if( !imk->checkReq( pc, true, &this->mat[0] ) )
					{
						safedelete( imk );
						item--;
						names.pop_back();
						models.pop_back();
						canSee = false;
					}
					else {
						makeItems.push_back( imk );
						
						std::wstring w;
						char b[TEMP_STR_SIZE];
						if( mat[0].id!=0 ) {
							sprintf( b, "%s %s - [%d/%d.%d]", models[item].c_str(), names[item].c_str(), imk->reqitems[0].number, imk->minskill/10, imk->minskill%10 );
						}
						else {
							sprintf( b, "%s %s", models[item].c_str(), names[item].c_str() );
						}
						string2wstring( std::string( b ), w );
						oldmenu->addMenuItem( 0, item, w );
						type=MAKE_NEED_MENUORITEM;
						canSee = true;
					}
					type=MAKE_NEED_MENUORITEM;
				}
				break;
			case MAKE_NEED_MENUORITEM: //MAKEMENU 3  or ADDITEM $item_golden_ringmail_tunic
				if( ( lha=="MAKEMENU" ) || ( lha=="ADDITEM" ) ) {
					if( canSee )
						imk->command = new cScriptCommand( lha, rha );
					type = MAKE_NEED_INFO;
				}
				else {
					LogWarning("create.xss, MAKEMENU %i: Expected'MAKEMENU or ADDITEM after 'SKILL %i'!", section, imk->minskill );
					error=true;
				}
				break;
		}
	}
	while( !error  && (++loopexit < MAXLOOPS) );


    if( item<=0 ) {
        ps->sysmsg( TRANSLATE("You aren't skilled enough to make anything with what you have.") );
    }

	safedelete(iter);
	if( error ) {
		ConOut( "[ERROR] on cration of makemenu %d\n", section );
        return;
	}

}