#include "edit.h"
//WCHAR					*TXT;
//char					*TXT;
UINT16 ULTOU(char *g_buf,WCHAR *u_buf,UINT8 type, UINT16 len);
//UINT16 USC2toUTF8(WCHAR *u_buf, char *g_buf,UINT16 len);
UINT16 MakeNewFile(WCHAR* name,UINT16 f_type);
const EVENT_HANDLER_ENTRY_T edit_state_handlers[]  =
{
	{ EV_DATA,                               	 EditData  },
	{ EV_CANCEL,                 				     edBack },
	{ EV_DONE,                       		       EditOk  },
    { STATE_HANDLERS_END,           		      	  NULL },
};
UINT32 edBack( EVENT_STACK_T *ev_st,  void *app )
{	
	APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
	UINT32                	    status;
	if(papp->setmode==MAIN_NEW)
	{
	papp->mode=MODE_NULL;
	papp->setmode=0;
	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 editStateEnter( EVENT_STACK_T *ev_st,  void *app,  ENTER_STATE_TYPE_T type )
{
    APP_TXTEDIT_T           *papp = (APP_TXTEDIT_T*)app;
    //WCHAR                   buf[255];
    UINT8                   edit_type=1;			
	FILE_HANDLE_T fd;
	size_t read;
	WCHAR * WTXT;//=NULL;
	char * TXT=NULL;
	//int  i=papp->startoff-papp->offset;
	//UINT16 k=papp->endoff,j=0;
	dbg("editStateEnter: Enter", NULL);
	if(papp->setmode==MAIN_NEW)
	{
	WTXT=NULL;
	//papp->startoff=MakeNewFile(papp->filedir,papp->type);
	//papp->endoff=0;
	}
	else
	{
	TXT=suAllocMem((papp->endoff+2)*sizeof(char), NULL);	
	fd = DL_FsOpenFile( papp->openfile,FILE_READ_PLUS_MODE,0);
	DL_FsFSeekFile( fd , papp->startoff, 0);
	DL_FsReadFile( TXT, papp->endoff, 1, fd, &read);
	DL_FsCloseFile(fd);
	TXT[papp->endoff]=0;
	TXT[papp->endoff+1]=0;
	if(r_type==TYPE_ANSI||r_type==TYPE_BIG||r_type==TYPE_XBIG5)
	{
	WTXT=suAllocMem((papp->endoff+2)*sizeof(WCHAR), NULL);
	//if(r_type==TYPE_BIG5)
	//GBTOU(TXT,WTXT,TYPE_ANSI,papp->endoff);
	//else
	GBTOU(TXT,WTXT,r_type,papp->endoff);
	}else if(r_type==TYPE_UCL)
	{	
	//UINT16 i=0;
	//UINT16 j=0;
	WTXT=suAllocMem((papp->endoff+2)*sizeof(WCHAR), NULL);
	ULTOU(TXT,WTXT,r_type,papp->endoff);
	//while(i<papp->endoff-1)
	//{
	//WTXT[j]=(TXT[i+1]<<8)|TXT[i];
	//i+=2;
	//j++;
	//}
	//WTXT[j]=0;
	//WTXT=suAllocMem((papp->endoff+2)*sizeof(WCHAR), NULL);
	
	//ULTOU(TXT,WTXT,r_type,papp->endoff);
	}else if(r_type==TYPE_UTF8)
	{
	WTXT=suAllocMem((papp->endoff+2)*sizeof(WCHAR), NULL);
	DL_Char_convUTF8toUCS2String( (UINT8*)TXT,papp->endoff,WTXT,papp->endoff*sizeof(WCHAR) );
	//USC2toUTF8(WTXT, TXT,papp->endoff);
	}
	else 
	WTXT=(WCHAR*)TXT;
	}
    if(type!=ENTER_STATE_ENTER) 
		return RESULT_OK;


        edit_type = 1;

dbg("editStateEnter: Enter3", NULL);



        dialog = UIS_CreateCharacterEditor( &papp->apt.port,
                                            WTXT,
                                            edit_type,
                                            512, 
                                            FALSE,
                                            NULL,            
                                            LANG_EDIT );


        if(dialog == 0) return RESULT_FAIL;
dbg("editStateEnter: Enter2", NULL);
        papp->apt.dialog = dialog;
dbg("editStateEnter: Enter1", NULL);
		if(TXT)
		suFreeMem(TXT);
		if(r_type==TYPE_ANSI||r_type==TYPE_BIG||r_type==TYPE_UCL||r_type==TYPE_XBIG5)
		suFreeMem(WTXT);
        return RESULT_OK;
}
UINT32 EditData( 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);
	WCHAR 		i[]={0x31,0};
    //char         buf[255];
	BYTE				cbuf[COPY_BUF_SIZE];
	WCHAR uri[255];
	char *tem_g;
	UINT16 len=u_strlen(event->attachment)*2;
	FILE_HANDLE_T fd;
	FILE_HANDLE_T fw;
	size_t size=COPY_BUF_SIZE;
	size_t read=COPY_BUF_SIZE;
	dbg("EditData: Enter", NULL);
	if(papp->setmode==MAIN_NEW)
	{
	u_strcpy(papp->openfile,papp->filedir);
	papp->startoff=MakeNewFile(papp->filedir,papp->type);
	papp->endoff=0;
	if(papp->type==TYPE_AUTO)
	r_type=TYPE_UCS;
	else
	r_type=papp->type;
	}
	u_strcpy(uri,papp->openfile);
	u_strcat(uri,L".bak");
	while(DL_FsFFileExist(uri))
	{
	u_strcpy(uri,papp->openfile);
	u_strcat(uri,L".bak");
	u_strcat(uri,i);
	i[0]++;
	}

	DL_FsSRenameFile( papp->openfile, uri, 0 );
	fd = DL_FsOpenFile( uri,FILE_READ_PLUS_MODE,0);
	fw = DL_FsOpenFile( papp->openfile,FILE_WRITE_MODE,0);
	//if (fd == FILE_HANDLE_INVALID ||fw == FILE_HANDLE_INVALID ) 
	//{
	//DL_FsCloseFile(fd); 
	//DL_FsCloseFile(fw); 
	//return edBack( ev_st, app );
	//}
	while(size < papp->startoff) 
	{
		DL_FsReadFile( cbuf, read, 1, fd, &read);
		DL_FsWriteFile( cbuf,read, 1, fw, &read);
		size+=COPY_BUF_SIZE;
	}
	DL_FsReadFile( cbuf, papp->startoff+COPY_BUF_SIZE-size, 1, fd, &read);

	DL_FsWriteFile( cbuf,  papp->startoff+COPY_BUF_SIZE-size,1, fw, &read );
	if(r_type==TYPE_ANSI||r_type==TYPE_BIG||papp->type==TYPE_XBIG5)
	{
	tem_g=suAllocMem((len)*sizeof(char), NULL);
	UtoGB(event->attachment,tem_g,0,len);
	DL_FsWriteFile( tem_g, strlen(tem_g),1, fw, &read );
	suFreeMem(tem_g);
	}else if(r_type==TYPE_UCL)
			{	
			//UINT16 k=0;
			//UINT16 j=0;
			//char * tem_u=event->attachment;
			tem_g=suAllocMem((len)*sizeof(char), NULL);
			//UtoUL(event->attachment,tem_g,0,len);
			ULTOU(event->attachment,(WCHAR*)tem_g,0,len);
			//tem_g=suAllocMem((papp->len/2)*sizeof(WCHAR), NULL);
			//while(j<len-1)
			//{
			//tem_g[j]=tem_u[j+1];
			//tem_g[j+1]=tem_u[j];
			//j+=2;
			//}
			//tem_g[j]=0;
			DL_FsWriteFile( tem_g, len,1, fw, &read );
			suFreeMem(tem_g);
			//WTXT=suAllocMem((papp->endoff+2)*sizeof(WCHAR), NULL);
			
			//ULTOU(TXT,WTXT,r_type,papp->endoff);
			}
	else if(r_type==TYPE_UTF8)
	{
	tem_g=suAllocMem(len*sizeof(WCHAR), NULL);
	DL_Char_convUCS2toUTF8String(event->attachment,len,(UINT8*)tem_g,len*sizeof(WCHAR));
	//DL_Char_convUTF8toUCS2String( event->attachment,len,(WCHAR*)tem_g,len*sizeof(WCHAR) );
	//USC2toUTF8(event->attachment,tem_g,len);
	DL_FsWriteFile( tem_g, len,1, fw, &read );
	suFreeMem(tem_g);
	}
	else
	DL_FsWriteFile( event->attachment, len,1, fw, &read );

	DL_FsReadFile( cbuf, papp->endoff, 1, fd, &read );
	//DL_FsFSeekFile( fd , papp->startoff+papp->endoff, 0);

	size=COPY_BUF_SIZE;
	while(size == COPY_BUF_SIZE) {
	
		DL_FsReadFile( cbuf,1, size,  fd, &size );
		DL_FsWriteFile( cbuf, 1,size,  fw, &size  );
		//DL_FsReadFile( cbuf,size, 1, fd, &size );
		//DL_FsWriteFile( cbuf,size, 1, fw, &size  );
	//	dbg("size=%d",size);

	}
	DL_FsCloseFile(fd); 
	DL_FsCloseFile(fw); 
	if(papp->setmode==MAIN_NEW)
	{
	papp->setmode=0;
	papp->mode=MODE_FB;
	}	
	else
	{
	if(papp->tem_j>papp->tem_x)
	papp->tem_j=papp->tem_j-papp->tem_x;
	else
	papp->tem_j=0;
	papp->mode=MODE_EDIT;
	}
	if(!SET_B||papp->setmode==MAIN_NEW)
	DL_FsDeleteFile( uri, 0 );
	//u_strcpy(papp->openfile,uri);
    status = APP_UtilChangeState( HE_STATE_READ, ev_st, app );
    
    return status;
}
UINT32 EditOk( 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;
}
UINT16 UtoGB(WCHAR *str,char * dst,int start,UINT16 len)
{	
	FILE_HANDLE_T f;
	size_t readen;
	WCHAR buf[2];
	UINT16 j=0,i=0;
	//UINT32 len=u_strlen(str)*2;			//获取文件大小
	char buff[1024];						//读取缓存
	buff[1022]=0;
	buff[1023]=0;
	dbg("len=%d",len);
	dbg("str=%d",*(char*)str);
	dbg("str+1=%d",*((char*)str+1));
	//f = DL_FsOpenFile(L"/c/uc2gb.map", FILE_READ_MODE, 0);  //读取MAP
	for(;i<len;)		
	{
		if(*((char*)str+i)==0&&*((char*)str+i+1)<0x81)  //当小于0x81的时候 U码和GB是相同的不需要转换
		{
			//if (*((char*)str+i+1)==0x0d)				//0X000D,0X000A U码中的换行 GB为0x0d0a
			//{
			//*dst++=0x0d;
			//i+=2;										//2位
			//}else if (*((char*)str+i+1)==0x0a)
			//	{
			//	*dst++=0x0a;
			//	i+=2;									//2位
			//	}
			//else
			//{
			i++;									//跳过第一位 
			*dst++=*((char*)str+i++);				//写入到GB 偏移加1
			dbg("*str=%d",*((char*)str+i-2));
			dbg("*str+1=%d",*((char*)str+i-1));
			dbg("*dst=%d",*(dst-1));
			dbg("dst=%s",dst);
			//}
		
			//*dst++=buff[j+2];
	//if (c2==0 && (byte0 & 0x80)==0)
	//{ if (flag && byte0==10) abyte0[l1++]=13; 
		}
		else
		{
			f = DL_FsOpenFile(map_uir, FILE_READ_MODE, 0);
			DL_FsFSeekFile( f , *((char*)str+i++)*2 , 0);	//读取MAP后 偏移 地址=U码的第一位*2
			DL_FsReadFile( buf, 1, 2, f, &readen );			//读取2位数据   数据为 U码第一位在MAP的偏移
			DL_FsCloseFile(f);
			dbg("buf=%d",buf[0]);
			f = DL_FsOpenFile(map_uir, FILE_READ_MODE, 0);  //读取索引
			DL_FsFSeekFile( f , buf[0], 0);				//偏移到 U码的第一位在MAP的偏移
			DL_FsReadFile( buff, 1, 1024, f, &readen );		//读取数据768 其实可以减小的 我这就写了最大 
			DL_FsCloseFile(f);												//可以用DL_FsReadFile( buf, 1, 4, f, &readen );
															//buf[1]-buf[0]得到   
			j=1;
			while(buff[j]!=*((char*)str+i)&&j<1024)			//如果buff[j]=U码第二位
			j+=3;
			i++;
			*dst++=buff[j+1];								//GB出来了
			*dst++=buff[j+2];
		}
	}
	//DL_FsCloseFile(f);
	dbg("dst=%s",dst);
	*dst=0;
	//dbg("tem_u=%s",tem_u);
	dbg("j=%d",j);
	dbg("j=%d j+1=%d",buff[j+1],buff[j+2]);
	return 1;
}
UINT16 GBTOU(char *g_buf,WCHAR *u_buf,UINT8 type, UINT16 len)
{
	UINT16 i=0;
	UINT16 k=0;     
	//UINT16 leng=0;  
	UINT16 tmp=0; 
	char 	buf[2];
	//leng=papp->endoff;
	//for(;i <= len;)
	while(i<len)
	{
		if (g_buf[i]<0x81)
		{


			u_buf[k++] = g_buf[i++];

			if (i == len)
			u_buf[k++] = 0;
		}else
		{
		buf[0]=g_buf[i];
		buf[1]=g_buf[i+1];
		dbg("buf=%s",buf);
		tmp=GBtoU(buf,type);
		if(tmp==0)
		{
		i++;
		}
		else
		{
		u_buf[k++]=tmp;
		i+=2;
		}
		dbg("u_buf[k++]=%d",u_buf[k]);		
		}
	}
	u_buf[k]=0;	

	



	return k;

}
UINT16 ULTOU(char *g_buf,WCHAR *u_buf,UINT8 type, UINT16 len)
{
	UINT16 i=0;
	UINT16 j=0;
	while(i<len-1)
	{
	u_buf[j]=(g_buf[i+1]<<8)|g_buf[i];
	i+=2;
	j++;
	}
	u_buf[j]=0;
	return 0;
}
UINT16 MakeNewFile(WCHAR* name,UINT16 f_type)
{
	UINT8 startoff=0;
	char e_type[4];
	FILE_HANDLE_T fw;
	size_t read;
	
					switch (f_type) 
					{	
						case TYPE_BIG:
						case TYPE_ANSI:
						startoff=0;
						break;
						
						case TYPE_AUTO:
						case TYPE_UCS:	
						e_type[0]=0xfe;
						e_type[1]=0xff;
						startoff=2;
						break;
						case TYPE_UCL:
						startoff=2;
						e_type[0]=0xff;
						e_type[1]=0xfe;
						break;
  			
						case TYPE_UTF8:
						e_type[0]=0xef;
						e_type[1]=0xbb;
						e_type[2]=0xbf;
						startoff=3; 
						break;
					}
	if(startoff!=0)
	{
	fw = DL_FsOpenFile( name,FILE_WRITE_MODE,0);
	DL_FsWriteFile( e_type, startoff,1, fw, &read );
	DL_FsCloseFile(fw); 
	}
return startoff;
}
//UINT16 USC2toUTF8(WCHAR *u_buf, char *g_buf,UINT16 len)
//{
//DL_Char_convUCS2toUTF8String( u_buf,len*sizeof(WCHAR),
//									(UINT8*)g_buf,len);		
//return 1;
//}
//UINT16 USC2toUTF8(WCHAR *u_buf, char *g_buf,UINT16 len)
//{
//DL_Char_convUTF8toUCS2String( (UINT8 *) g_buf,len,u_buf,len*sizeof(WCHAR) );
/*	UINT16 i=0,j=0;
	while(i< len-1)
	{
		if (u_buf[j]<0x80) 
		g_buf[i++]= u_buf[j]>>0 & 0x7F | 0x00 ;
		else if (u_buf[j]<0x0800&&i+1< len) 
		{
		g_buf[i++]= u_buf[j]>>6 & 0x1F | 0xC0 ;
		g_buf[i++]= u_buf[j]>>0 & 0x7F | 0x00 ;
		}
		else if (u_buf[j]<0x010000&&i+2< len) 
		{
		g_buf[i++] = u_buf[j]>>12 & 0x0F | 0xE0 ;
		g_buf[i++] = u_buf[j]>>6 & 0x3F | 0x80 ;
		g_buf[i++] = u_buf[j]>>0 & 0x3F | 0x80 ;
		}else if (u_buf[j]<0x110000&&i+3< len) 
		{
		g_buf[i++] = u_buf[j]>>18 & 0x07 | 0xF0 ;
		g_buf[i++] = u_buf[j]>>12 & 0x3F | 0x80 ;
		g_buf[i++] = u_buf[j]>>6 & 0x3F | 0x80 ;
		g_buf[i++] = u_buf[j]>>0 & 0x3F | 0x80 ;
		}else
		i++;
	j++;
	}
	g_buf[len-1]=0;
	*/
//	return 1;
//}







