#include <stdio.h>
#include <stdlib.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <string.h>
#include "sftlib.h"

extern int	DB_root ;
extern int	DB_sub[2] ;
extern int	DB_Message[1] ;
extern int	DB_list ;
extern int	DB_list_sc ;
extern int	DB_list_scFunc() ;
extern int	DB_dscrbFunc() ;
extern int	DB_data ;
extern int	DB_data_sub[11] ;
extern int	DB_data_mes[11] ;
extern int	DB_data_btn[1] ;
extern int	DB_data_btnM[1] ;
extern int	DB_data_SdateM[2] ;
extern int	DB_data_UdateM[6] ;
extern int	DB_data_Sdate[2] ;
extern int	DB_data_Udate[6] ;

#define LSTCOLUM 101        /* 1行の桁数 */

int comp1( const int *i, const int *j)
{
	int ret;
	switch( DB_sortmode ){
	case 0:
		ret=strncmp( DB_dataary[*i].code, DB_dataary[*j].code,CODE_NUM);
		break;
	case 1:
		ret=strncmp( DB_dataary[*i].lstbf, DB_dataary[*j].lstbf,TLSTBUF_LEN);
		break;
	case 2:
		ret=(DB_dataary[*i].date3>DB_dataary[*j].date3)?1:-1;
		if(DB_dataary[*i].date3==DB_dataary[*j].date3) ret=0;
		break;
	case 3:
		ret=(DB_dataary[*i].date4>DB_dataary[*j].date4)?1:-1;
		if(DB_dataary[*i].date4==DB_dataary[*j].date4) ret=0;
		break;
	case 4:
		ret=(DB_dataary[*i].date5>DB_dataary[*j].date5)?1:-1;
		if(DB_dataary[*i].date5==DB_dataary[*j].date5) ret=0;
		break;
	case 5:
		ret=(DB_dataary[*i].date6>DB_dataary[*j].date6)?1:-1;
		if(DB_dataary[*i].date6==DB_dataary[*j].date6) ret=0;
		break;
	case 6:
		ret=(DB_dataary[*i].date7>DB_dataary[*j].date7)?1:-1;
		if(DB_dataary[*i].date7==DB_dataary[*j].date7) ret=0;
		break;
	case 7:
		ret=(DB_dataary[*i].date8>DB_dataary[*j].date8)?1:-1;
		if(DB_dataary[*i].date8==DB_dataary[*j].date8) ret=0;
		break;
	default:
		ret=0;
		break;
	}
	if(ret==0) {
		if( DB_pmodec1==0 )
			ret=strncmp( DB_dataary[*i].lstbf, DB_dataary[*j].lstbf, 
				TLSTBUF_LEN);
		else
			ret=strncmp( DB_dataary[*i].code, DB_dataary[*j].code, 								CODE_NUM+1+TLSTBUF_LEN);
	}
	
	return (ret*DB_sortrev);
}


PROTO int DB_MakeList(void)
{
	/* リストメニュー作成 */
	int i, n, m;
	int w1,w2;    /* ワーク変数 */
	extern DB_list, DB_list_sc;
	index *ind;
	int *sortwork;
	n=9;  /* リストの表示行数 */

	for(DB_datalnm=0, i=0; i< DB_datanum; i++ ) {
		if( check(&DB_dataary[i]) )	{
			DB_dataary[DB_datalnm].dataid=i;
			DB_datalnm++;
		}
	}
	/* sorting */
	if( DB_datalnm>1 ) {
		if( ( sortwork=(int*)AMI_malloc(sizeof(int)*DB_datalnm) )!=NULL ) {
			for(i=0; i< DB_datalnm; i++) 
				sortwork[i]=DB_dataary[i].dataid;
			qsort( sortwork, DB_datalnm, sizeof(int), comp1 );
			for(i=0; i< DB_datalnm; i++) 
				DB_dataary[i].dataid=sortwork[i];
			AMI_free( sortwork );
		}
	}

	if( DB_lstmode<=0 ) DB_lstmode=1;
	DB_lstline=(DB_datalnm+DB_lstmode-1)/DB_lstmode;
	MMI_SendMessage( DB_list, MM_SETMAXROWCOLUM, 2, DB_lstline, DB_lstmode);
	w1=(LSTCOLUM)/DB_lstmode;
	w2=DB_lstmode-1;    /* カラム数-1 */
	for(i=0; i< w2; i++ ){
		MMI_SendMessage( DB_list, MM_SETCOLUMLEN, 2, i, w1);
	}
	MMI_SendMessage( DB_list, MM_SETCOLUMLEN, 2, w2, LSTCOLUM-w1*w2);
	MMI_SendMessage( DB_list, MM_SETDSPROWCOLUM, 2, 0, 0);

	w1=w2=0;
	for(i=0; i< DB_datalnm; i++ ) {
		m=DB_dataary[i].dataid; /* わけわからんぞ */
		ind=&DB_dataary[m];     /* わけわからんぞ */
		if( DB_pmodec1==0) {
			MMI_SendMessage( DB_list, MM_SETROWCOLUM, 3, w1, w2,
			(char*) ind->lstbf);
		}else {
			MMI_SendMessage( DB_list, MM_SETROWCOLUM, 3, w1, w2,
				ind->code);
		}
		w1++; if( w1 >= DB_lstline ) {w1=0; w2++;}
	}

	{
		static char title[LSTCOLUM];
		static char *DB_cndmodeM[3]={
			"検索条件は無効です",			
			"検索条件は有効です",
			"検索条件のモードがよくわかりません"
		};
		int cndmode;
		if( DB_cndmode>=2 ) cndmode=2; else cndmode=DB_cndmode;
		sprintf(title, "検索一覧表    %8d 件あります｡ 1つ選んでね(^_^) "
						"               %30s",
			DB_datalnm, DB_cndmodeM[cndmode]);
		MMI_SendMessage( DB_Message[0],MM_SETMSG, 1, title );
	}

	MMI_SendMessage( DB_list_sc, MM_SETSCROLL, 5,
		n-1, n-1,(DB_lstline<n)?(n-1):(DB_lstline-1), n, n-1);
	return 0;
}



PROTO int check( index *ind )
{
	/*検索対象でなければ0を返す*/
	ulbit_t bit;


	if( ind->system[0]!='S' ) return 0;
	if( DB_cndmode==0) return 1;

	bit=ind->sort;
	if( (bit) && (!(bit & DB_search.sortbit)) ) return 0;

	bit=ind->media;
	if( (bit) && (!(bit & DB_search.mediabit)) ) return 0;

	bit=DB_ctob(ind->name1h);
	if( (bit) && (!(bit & DB_search.name1bit)) ) return 0;

	bit=DB_ctob(ind->name2h);
	if( (bit) && (!(bit & DB_search.name2bit)) ) return 0;

	bit=DB_ctob(ind->makerh);
	if( (bit) && (!(bit & DB_search.makerbit)) ) return 0;

	bit=DB_ctob(ind->abouth);
	if( (bit) && (!(bit & DB_search.aboutbit)) ) return 0;

	bit=DB_ctob(ind->infomh);
	if( (bit) && (!(bit & DB_search.infombit)) ) return 0;

	if( ind->teika < DB_search.teikad || ind->teika > DB_search.teikau )
			return 0;
	if( ind->ram   < DB_search.ramd   || ind->ram   > DB_search.ramu )
			return 0;
	if( ind->hd    < DB_search.hdd    || ind->hd    > DB_search.hdu )
			return 0;
	return 1;
}

PROTO int DB_showData( void )
{
	LISTITEML40   *listItem;
	int r,c,lr,  lc, number; 
	int ret;

	MMI_SendMessage( DB_list, MM_GETLMENUPTR, 5, &listItem, &r, &c, &lr,&lc);
	if( lr>=0 ) {
		number=DB_dataary[lr+lc*DB_lstline].dataid;
	} else number=-1;
	ret=DB_showDataNum( number );
	return ret;
}

PROTO int DB_showDataNum( int num )
{
	static char s_name1[ TNAME1_LEN+1 ];
	static char s_name2[ TNAME2_LEN+1 ];
	static char s_maker[ TMAKER_LEN+1 ];
	static char s_about[ TABOUT_LEN+1 ];
	static char s_infom[ TINFOM_LEN+1 ];
	static char s_bunrui[ BUNRUI_LEN+1 ];
	static char s_media[ MEDIA_NUM*(MEDIA_LEN+2)+1 ];
	static char s_code[ CODE_NUM+1 ];
	static char s_teika[ 16 ];
	static char s_ram[ 32 ];
	static char s_hd[ 32 ];
	static char s_btnmsg[ TDOCFL_LEN+8 ];
	static char s_Sdatemsg[2][28];
	static char s_Udatemsg[6][28];
	int mos;
	int i, j;
	index *ind;
	FILE *fp;
	char buf[512];

	DB_strccpy( s_name1, "", TNAME1_LEN );
	DB_strccpy( s_name2, "", TNAME2_LEN );
	DB_strccpy( s_maker, "", TMAKER_LEN );
	DB_strccpy( s_bunrui,"", BUNRUI_LEN );
	DB_strccpy( s_about, "", TABOUT_LEN );
	DB_strccpy( s_code , "", CODE_NUM);
	DB_strccpy( s_ram,   "", sizeof(s_ram)  -1);
	DB_strccpy( s_hd,    "", sizeof(s_hd)   -1);
	DB_strccpy( s_teika, "", sizeof(s_teika)-1);
	DB_strccpy( s_media, "", sizeof(s_media)-1);
	DB_strccpy( s_infom, "", TINFOM_LEN );
	DB_strccpy( s_btnmsg,  "", sizeof(s_btnmsg) -1);
	DB_strccpy( s_Sdatemsg[0],  "", sizeof(s_Sdatemsg[0]) -1);
	DB_strccpy( s_Sdatemsg[1],  "", sizeof(s_Sdatemsg[1]) -1);
	DB_strccpy( s_Udatemsg[0],  "", sizeof(s_Sdatemsg[0]) -1);
	DB_strccpy( s_Udatemsg[1],  "", sizeof(s_Sdatemsg[1]) -1);
	DB_strccpy( s_Udatemsg[2],  "", sizeof(s_Sdatemsg[2]) -1);
	DB_strccpy( s_Udatemsg[3],  "", sizeof(s_Sdatemsg[3]) -1);
	DB_strccpy( s_Udatemsg[4],  "", sizeof(s_Sdatemsg[4]) -1);
	DB_strccpy( s_Udatemsg[5],  "", sizeof(s_Sdatemsg[5]) -1);

	MTL_setAtrObj( DB_data_btn[0], MS_INACTIVEL40 );
	MMI_SendMessage( DB_data_btn[0], MM_ERASE, 0);
	MMI_SendMessage( DB_data_btn[0], MM_SHOW, 0);

	if ( num>=0 ) {

		ind=&DB_dataary[num];
		fp=fopen( DB_sftlibp, "rb");
		if( fp==NULL ) return -1;

		MG_PushPtr( 64 , &mos ) ;
		fseek( fp, sizeof(soft)*num, SEEK_SET);
		fread( &DB_nowdata, sizeof(soft),1,fp);
		fclose(fp);
		MG_PopPtr( mos ) ;

		DB_strccpy( s_name1,  DB_nowdata.d_name1, TNAME1_LEN);
		DB_strccpy( s_name2,  DB_nowdata.d_name2, TNAME2_LEN);
		DB_strccpy( s_maker,  DB_nowdata.d_maker, TMAKER_LEN);
		DB_strccpy( s_about,  DB_nowdata.d_about, TABOUT_LEN);
		DB_strccpy( s_infom,  DB_nowdata.d_infom, TINFOM_LEN);

		for( j=0; j< BUNRUI_NUM; j++){
			if( ind->sort & (1<<j) ) {break;}
		}
		if( j< BUNRUI_NUM )
			sprintf( s_bunrui, "%-.*s", BUNRUI_LEN, bunrui_mes[j] );

		DB_strccpy( s_code , DB_nowdata.d_code , CODE_NUM);

		{
			buf[0]=0;
			for( j=0; j< MEDIA_NUM; j++){
				if( ind->media & (1<<j) ) 
					{
					strcat( buf, "[");
					strcat( buf, media_mes[j]);
					strcat( buf, "]");
					} 
			}
			DB_strccpy( s_media, buf, sizeof(s_media)-1);
		}

		sprintf( buf, "%d 円", ind->teika);
		sprintf( s_teika, "%-14s", buf);

		sprintf( buf,"%dK(%dM) bytes 以上",
				ind->ram,(ind->ram+(1024-1))/(1024) );
		sprintf( s_ram, "%-24s", buf );

		sprintf( buf, "%dK(%dM) bytes 以上",
				ind->hd,(ind->hd+(1024-1))/(1024) );
		sprintf( s_hd,  "%-24s", buf );

		DB_cuttlsp(DB_nowdata.d_docfile, TDOCFL_LEN);
		if( DB_nowdata.d_docfile[0]!='\0' ) {
			sprintf( buf, "%-s", DB_nowdata.d_docfile );
			sprintf( DB_docp, "%s%s", DB_docdir, buf );
			MTL_resetAtrObj( DB_data_btn[0],(~MS_INACTIVEL40) );
			sprintf( s_btnmsg, "%s呼出し", buf);
		} else {
			MTL_setAtrObj( DB_data_btn[0], MS_INACTIVEL40 );
		}
		strcpy( s_Sdatemsg[0], DB_ctime( &DB_nowdata.d_date1) );
		strcpy( s_Sdatemsg[1], DB_ctime( &DB_nowdata.d_date2) );
		strcpy( s_Udatemsg[0], DB_ctime( &DB_nowdata.d_date3) );
		strcpy( s_Udatemsg[1], DB_ctime( &DB_nowdata.d_date4) );
		strcpy( s_Udatemsg[2], DB_ctime( &DB_nowdata.d_date5) );
		strcpy( s_Udatemsg[3], DB_ctime( &DB_nowdata.d_date6) );
		strcpy( s_Udatemsg[4], DB_ctime( &DB_nowdata.d_date7) );
		strcpy( s_Udatemsg[5], DB_ctime( &DB_nowdata.d_date8) );
	}

	MMI_SendMessage( DB_data_btn[0], MM_ERASE, 0);
	MMI_SendMessage( DB_data_btn[0], MM_SHOW, 0);
	MMI_SendMessage( DB_data_btnM[0], MM_SETMSG, 1, s_btnmsg);
	MMI_SendMessage( DB_data_btnM[0], MM_ERASE, 0);
	MMI_SendMessage( DB_data_btnM[0], MM_SHOW, 0);

	MMI_SendMessage( DB_data_mes[0],MM_SETMSG, 1, s_name1);
	MMI_SendMessage( DB_data_mes[1],MM_SETMSG, 1, s_name2);
	MMI_SendMessage( DB_data_mes[2],MM_SETMSG, 1, s_maker);
	MMI_SendMessage( DB_data_mes[3],MM_SETMSG, 1, s_bunrui);
	MMI_SendMessage( DB_data_mes[4],MM_SETMSG, 1, s_about);
	MMI_SendMessage( DB_data_mes[5],MM_SETMSG, 1, s_code);
	MMI_SendMessage( DB_data_mes[6],MM_SETMSG, 1, s_ram);
	MMI_SendMessage( DB_data_mes[7],MM_SETMSG, 1, s_hd);
	MMI_SendMessage( DB_data_mes[8],MM_SETMSG, 1, s_teika);
	MMI_SendMessage( DB_data_mes[9],MM_SETMSG, 1, s_media);
	MMI_SendMessage(DB_data_mes[10],MM_SETMSG, 1, s_infom);

	MMI_SendMessage(DB_data_Sdate[0],MM_SETMSG, 1, s_Sdatemsg[0]);
	MMI_SendMessage(DB_data_Sdate[1],MM_SETMSG, 1, s_Sdatemsg[1]);
	MMI_SendMessage(DB_data_Udate[0],MM_SETMSG, 1, s_Udatemsg[0]);
	MMI_SendMessage(DB_data_Udate[1],MM_SETMSG, 1, s_Udatemsg[1]);
	MMI_SendMessage(DB_data_Udate[2],MM_SETMSG, 1, s_Udatemsg[2]);
	MMI_SendMessage(DB_data_Udate[3],MM_SETMSG, 1, s_Udatemsg[3]);
	MMI_SendMessage(DB_data_Udate[4],MM_SETMSG, 1, s_Udatemsg[4]);
	MMI_SendMessage(DB_data_Udate[5],MM_SETMSG, 1, s_Udatemsg[5]);

	for(i=0; i<11; i++) {
			MMI_SendMessage( DB_data_mes[i], MM_ERASE, 0);
			MMI_SendMessage( DB_data_mes[i], MM_SHOW, 0);
	}
	for(i=0; i<2; i++) {
			MMI_SendMessage( DB_data_Sdate[i], MM_ERASE, 0);
			MMI_SendMessage( DB_data_Sdate[i], MM_SHOW, 0);
	}
	for(i=0; i<6; i++) {
			MMI_SendMessage( DB_data_Udate[i], MM_ERASE, 0);
			MMI_SendMessage( DB_data_Udate[i], MM_SHOW, 0);
	}
	return num;
}



PROTO int DB_getlstptr( int *x)
{
	int prt, min, max, len, page;
	MMI_SendMessage( DB_list_sc, MM_GETSCROLL, 5, &prt, &min, 
		&max, &len, &page);
	*x=prt;
	return 0;
}

PROTO int DB_setlstptr( int x)
{
	int prt, min, max, len, page;
	MMI_SendMessage( DB_list_sc, MM_GETSCROLL, 5, &prt, &min, 
		&max, &len, &page);
	if( x< min) x=min;
	if( x> max) x=max;
	MMI_SendMessage( DB_list_sc, MM_SETSCROLL, 5, x, min, 
		max, len, page);
	MMI_SendMessage( DB_list, MM_SETDSPROWCOLUM, 2, x-min, 0);
	MMI_SendMessage( DB_list, MM_SHOW, 0);
	return 0;
}

