
#if defined _PARSER_included
  #endinput
#endif
#define _PARSER_included

#include <all.inc>

#include <../common/bn_defs.inc>
//#include "top.inc"


enum{
	ORDER_LEFT = 0 ,
	ORDER_RIGHT,
	ORDER_CENTER, // AUTO ODD/EVEN
	ORDER_CENTER_LEFTEVEN,
	ORDER_CENTER_LEFTODD,
};
enum{
	MENU_TYPE_LABEL,
	MENU_TYPE_ICON,
};
enum{
	ITEM_DATA_XML,
	ITEM_DATA_DB,
};

//グローバルな現在参照グループとアイテム
public xmltag:stdmenu_curr_group = xmltag:0;
//new curr_item = xmltag:0;

stock bool: set_current_menu_group( const id[] )
{
	//printf("set_current_menu_group %s!!!!!!!!!!!!\n", id);
	stdmenu_curr_group = domFindTag( domRoot(), "MENUGROUP", id ) ;
	if(stdmenu_curr_group) return true ;
	return false ;
}
stock bool:set_menu_return( nextgroup[], retgroup[], retitem[] )
{
	new xmltag:next_group = domFindTag( domRoot(), "MENUGROUP", nextgroup ) ;
	if( !next_group ) return false ;
	domSetData( next_group, "retgroup", retgroup ) ;
	domSetData( next_group, "retitem", retitem ) ;
	//printf("set_menu_return %s, %s !!!!!!!!!!!!!!\n", retgroup, retitem);
	return true;
}
stock bool:get_menu_return( retgroup[], retitem[], 
					   _gpsize_=sizeof retgroup , _itsize_=sizeof retitem )
{
	if( !stdmenu_curr_group ) return false ;
	if( !domGetData( stdmenu_curr_group, "retgroup", retgroup, _gpsize_ ) ) return false ;
	if( !domGetData( stdmenu_curr_group, "retitem", retitem, _itsize_ ) ) return false;
	//printf("get_menu_return %s, %s !!!!!!!!!!!!!!\n", retgroup, retitem);
	return true;
}
stock bool:get_menu_title( title[], _bufsize_ = sizeof title )
{
	if( !stdmenu_curr_group ) return false ;
	new xmltag:title_item = domFirstChild(stdmenu_curr_group,"TITLE") ;
	if( !title_item ) return false;
	if( !domGetData( title_item, "label", title, _bufsize_ ) ) return false ;
	//printf("get_menu_title %s!!!!!!!!!!!!!!\n", title);
	return true;
}
stock get_menu_type( type[], _bufsize_ = sizeof type )
{
	if( !stdmenu_curr_group ) return (-1) ;
	if( !domGetData( stdmenu_curr_group, "menutype", type, _bufsize_ ) ) return (-1) ;
	//printf("get_menu_type %s!!!!!!!!!!!!!!\n", type);
	if( !strcmp( type, "label" ) ) return MENU_TYPE_LABEL ;
	else if( !strcmp( type, "icon" ) ) return MENU_TYPE_ICON ;
	return (-1);
}
stock get_menu_order( order[], _bufsize_ = sizeof order )
{
	if( !stdmenu_curr_group ) return (-1) ;
	if( !domGetData( stdmenu_curr_group, "order", order, _bufsize_ ) ) return (-1) ;
	//printf("get_menu_order %s!!!!!!!!!!!!!!\n", order);
	if( !strcmp( order, "left" ) ) return ORDER_LEFT ;
	else if( !strcmp( order, "right" ) ) return ORDER_RIGHT;
	else if( !strcmp( order, "center" ) ) return ORDER_CENTER ;
	return (-1);
}
stock get_menu_attr( const attr[], value[], _bufsize_ = sizeof value )
{
	//printf("get_menu_order %s!!!!!!!!!!!!!!\n", order);
	if( !stdmenu_curr_group ) return (-1) ;
	return domGetData( stdmenu_curr_group, attr, value, _bufsize_ );
}
stock bool:set_menu_psp_info( const id[], const url[] )
{
	if( !stdmenu_curr_group ) return false ;
	new xmltag:psptag = domFindTag( stdmenu_curr_group, "ITEM", id ) ;
	if( !psptag ) return false ;
	domSetData( psptag, "link", "href" ) ; //href 決め打ち！！
	domSetData( psptag, "href", url ) ;
	//printf("get_menu_type %s!!!!!!!!!!!!!!\n", type);
	return true;
}
stock get_menu_item_num()
{
    new count = 0 ;
	if( !stdmenu_curr_group ) return (-1) ;
    for( new xmltag:child = domFirstChild(stdmenu_curr_group,"ITEM") ; child ; child=domNext(child,"ITEM") )
    {
		count ++;
    }
	//printf("get_menu_item_num %d!!!!!!!!!!!!\n", count);
    return count;
}
stock get_menu_item_number( const id[] )
{
    new count = 0 ;
	if( !stdmenu_curr_group ) return (-1) ;
    for( new xmltag:child = domFirstChild(stdmenu_curr_group,"ITEM") ; child ; child=domNext(child,"ITEM") )
    {
        new tmp[256 char];
        domGetData( child, "id", tmp );
        if( !strcmp( tmp, id ) )
        {
			//printf("get_menu_item_number %d!!!!!!!!!!!!\n", count);
			return count;
        }

		count ++;
    }
    return (-1);
}
stock bool: get_menu_item_attr_withnum( num, const attr[], value[], _bufsize_ = sizeof value )
{
    new count = 0;
	//printf("get_menu_item_attr_withnum %d, %s!!!!!!!!!!!!!!\n", num, attr);
	if( !stdmenu_curr_group || num < 0 ) return false ;
	new xmltag:child=xmltag:0;
    for( child=domFirstChild(stdmenu_curr_group,"ITEM") ;
		 child && count<num ; child=domNext(child,"ITEM") )
    {
		count ++;
    }
	if( !child ) return false ;
	return domGetData( child, attr, value, _bufsize_ ) ;
}
stock bool: get_menu_item_attr_withid( const id[], const attr[], value[], _bufsize_ = sizeof value  )
{
	//printf("get_menu_item_attr_withid %s, %s!!!!!!!!!!!!!\n", id, attr);
	if( !stdmenu_curr_group ) return false ;
	new xmltag:item=domFindTag( stdmenu_curr_group, "ITEM", id ) ;
	if( !item ) return false ;
	return domGetData( item, attr, value, _bufsize_ ) ;
}

