#include "goto.h"
const EVENT_HANDLER_ENTRY_T goto_state_handlers[]  =
{
	{ EV_DATA,                               	 GEditData  },
	{ EV_CANCEL,                 				     GBack },
	{ EV_DONE,                       		       GEditOk  },
    { STATE_HANDLERS_END,           		      	  NULL },
};
UINT32 GotoStateEnter( EVENT_STACK_T *ev_st,  void *app,  ENTER_STATE_TYPE_T type )
{
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	UINT16 Percent;
	char		str[10];
	WCHAR		unicode_str[10];
	UINT8 size,g_type=33;

	dbg("GotoStateEnter: Enter", NULL);


    if(type!=ENTER_STATE_ENTER) 
		return RESULT_OK;
		
		if(papp->set)
		{
			if(papp->setmode==MAIN_SET_Y)
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],set_y);
			Percent=SET_Y;
			size=3;
			}
			else if(papp->setmode==MAIN_SET_T)
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],set_t);
			Percent=SET_T;
			size=10;
			}else if(papp->setmode==MAIN_SET_H)
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],set_h);
			Percent=SET_H;
			size=3;
			}else if(papp->setmode==MAIN_SET_X)
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],set_x);
			Percent=SET_X;
			size=3;
			}else if(papp->setmode==MAIN_SET_B)
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],set_b);
			if(SET_B)
			Percent=1;
			else
			Percent=0;
			size=1;
			}
			else if(papp->setmode==MAIN_TYPE_OFF)
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],set_type_o);
			size=16;
			}
			else
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],set_c);
			Percent=SET_C_I[papp->setmode-MAIN_END];
			dbg("SET_C_I=%d,papp->setmode=%d,MAIN_SET_C=%d",SET_C_I[papp->setmode-MAIN_SET_C],papp->setmode,MAIN_SET_C);
			size=3;
			}
		}
		else if(papp->setmode==MAIN_NEW)
			{
			SetStringRes(&Resources[RES_LIST_CAPTION],new_);
			size=46;
			g_type=0x0C;
			}
		else
		{
		SetStringRes(&Resources[RES_LIST_CAPTION], goto_);
		Percent=papp->startoff*10/(papp->file_size/10);
		size=4;
		}
		if(papp->setmode==MAIN_NEW)
		{
		u_strcpy(unicode_str,L".txt");
		}
		else
		{
			if(papp->setmode==MAIN_TYPE_OFF)
			sprintf( str, "%p", map_id);
			else 
			sprintf( str, "%d", Percent );
			u_atou(str, unicode_str);
		}

		dbg("GotoStateEnter: Percent=%d", Percent);
		dbg("GotoStateEnter: str=%s", str);


        dialog = UIS_CreateCharacterEditor( &papp->apt.port,
                                            unicode_str,
                                            g_type,
                                            size, 
                                            FALSE,
                                            NULL,            
                                            Resources[RES_LIST_CAPTION]);


        if(dialog == 0) return RESULT_FAIL;
dbg("editStateEnter: Enter2", NULL);
        papp->apt.dialog = dialog;
dbg("editStateEnter: Enter1", NULL);
        return RESULT_OK;
}
UINT32 GBack( EVENT_STACK_T *ev_st,  void *app )
{	
	APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	UINT32                	    status;

	if(papp->set)
	{
	papp->d_true=TRUE;
	if(papp->setmode>=MAIN_END)
	papp->setmode=MAIN_SET_C;
	else if(papp->setmode==MAIN_TYPE_OFF)
	papp->setmode=0;
	status = APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
	}
	else if(papp->setmode==MAIN_NEW)
	{
	papp->mode=MODE_NULL;
	status = APP_UtilChangeState(HE_STATE_FB, ev_st, app );
	}
	else
	{
	papp->mode=MODE_NULL;
	status = APP_UtilChangeState(HE_STATE_READ, ev_st, app );
	}
	return status;
}
UINT32 GEditOk( EVENT_STACK_T *ev_st,  void *app )
{
     UINT32                  status = 0;
     ADD_EVENT_DATA_T        *ev_data;
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	dbg("EditOk: Enter", NULL);
     AFW_AddEvEvD(ev_st, EV_REQUEST_DATA, ev_data);
	 UIS_HandleEvent( papp->apt.dialog,  ev_st );

     return status;
}
UINT32 GEditData( EVENT_STACK_T *ev_st,  void *app )
{
	APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
    UINT32       status = 0;
    EVENT_T      *event = AFW_GetEv(ev_st);
	char         buf[10];
	UINT16 i;
	udbg("event->attachment=%s",event->attachment);
	u_utoa( event->attachment, buf);
	dbg("buf=%s",buf);
	i=strtoul( buf, NULL, 10 );
	dbg("i=%d",i);
    //value = strtoul( buf, NULL, 16 );
		if(papp->set)
		{
			papp->d_true=TRUE;
			if(papp->setmode==MAIN_SET_Y)
			{
			SET_Y=i;
			status=APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
			}else if(papp->setmode==MAIN_SET_H)
			{
			SET_H=i;
			status=APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
			}else if(papp->setmode==MAIN_SET_X)
			{
			if(i>16)
			SET_X=16;
			else
			SET_X=i;
			status=APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
			}
			else if(papp->setmode==MAIN_SET_T)
			{
			SET_T=i;
			//papp->R_TIME=TRUE;
			status=APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
			}else if(papp->setmode==MAIN_SET_B)
			{
			if(i==0)
			SET_B=FALSE;
			else
			SET_B=TRUE;
			//papp->R_TIME=TRUE;
			status=APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
			}
			else if(papp->setmode==MAIN_TYPE_OFF)
			{
			map_id=strtoul( buf, NULL, 16 );
			papp->setmode=0;
			status=APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
			}
			else
			{
			if(i>255)
			SET_C_I[papp->setmode-MAIN_END]=255;
			else
			SET_C_I[papp->setmode-MAIN_END]=i;
			papp->setmode=MAIN_SET_C;
			status=APP_UtilChangeState(HE_STATE_MAIN, ev_st, app );
			}
			saveset(app);
		}else if(papp->setmode==MAIN_NEW)
			{			
			u_strcat(papp->filedir,event->attachment);
			status=APP_UtilChangeState(HE_STATE_EDIT, ev_st, app );
			}
		else
	{
	if(i>100)
	i=100;
	papp->offset=papp->file_size/100*i;
	dbg("papp->offset=%d",papp->offset);
	papp->tem_j=0;
	papp->mode=MODE_EDIT;
	status=APP_UtilChangeState(HE_STATE_READ, ev_st, app );
	}
	return status;
}
UINT32 saveset(void * app)
{
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	WCHAR uri[FILEURI_MAX_LEN+1];
	FILE_HANDLE_T f;
	size_t readen;
	u_strcpy(uri,file_dir);
	u_strcat(uri,L"edit.set");
	f = DL_FsOpenFile( uri,FILE_WRITE_MODE,0);
		if (f == FILE_HANDLE_INVALID) 
		{
		DL_FsCloseFile(f);
		return RESULT_OK;
		}
	DL_FsWriteFile( &SET_Y, 1, 1, f, &readen );
	DL_FsWriteFile( &SET_T, 1, 2, f, &readen );
	DL_FsWriteFile( &SET_C_I, 1, 6, f, &readen );
	DL_FsWriteFile( &map_id, 1, 4, f, &readen );
	DL_FsWriteFile( &map_uir, 1, 100, f, &readen );
	DL_FsWriteFile( &papp->type, 1, 1, f, &readen );
	DL_FsWriteFile( &pic_uir, 1, 100, f, &readen );
	DL_FsWriteFile( &r_pic, 1, 1, f, &readen );
	DL_FsWriteFile( &SET_H, 1, 1, f, &readen );
	DL_FsWriteFile( &SET_X, 1, 1, f, &readen );
	DL_FsWriteFile( &SET_B, 1, 1, f, &readen );
	DL_FsWriteFile( &bmap_uir, 1, 100, f, &readen );
	DL_FsWriteFile( &atype, 1, 1, f, &readen );
	//DL_FsWriteFile( &papp->MAIN_TYPE, 1, 1, f, &readen );
	DL_FsCloseFile(f);
	return RESULT_OK;
}








