#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>

#include <egb.h>
#include <guidbg.h>

#include <file_dlg.h>

#include "eintm.h"

#include "ysmacro.h"
#include "ysgui.h"

#include "ysrename.h"

char *guiEgbPtr ;   /* EGB のワークアドレス */

int Ys11Id = -1 ;
int pdDlgId = -1 ;
int pdMenuId = -1 ;
int pdTwId = -1 ;
int pdTwLst = -1 ;
int pdTw[2] = -1 ;
int pdFileId = -1 ;
int pdFileLst = -1 ;
int pdFile[5] = -1 ;
int pdEditId = -1 ;
int pdEditLst = -1 ;
int pdEdit[2] = -1 ;
int pdExitBtnId = -1 ;
int pdSrcId = -1 ;
int pdDstId = -1 ;
int pdSrcScr = -1 ;
int pdDstScr = -1 ;
int execBtnId = -1 ;
int curDirId = -1 ;
int extDlg = -1 ;
int extInputId = -1 ;


static void SetShellFunc();


int main()
{
	extern int APL_init() ;
	int aplId;
	MMICTRL ct;

	ct.page0=SCREEN16|SCREENIGNORE; /* Page0の画面モード */
	ct.page1=SCREENUNUSED;          /* Page1の画面モード */
	ct.writePage=0;                 /* 書き込みページは0  */
	ct.displayPage=SCREENAVAILABLE; /* 表示ページは,これしかない */
	ct.priority=0;                  /* 優先表示ページもPage0 */
	ct.mode=SCREENAVAILABLE;        /* ビット/ピクセルのことらしい */
	ct.width=SCREENEXPAND;          /* VRAM 1line分のバイト数だそうだ. */

	ct.size=0;                      /* LIBがメモリを勝手に初期化するらしい */
	ct.ptr=NULL;

	ct.asize=0;                     /* アプリ管轄のメモリなんて使うと面倒だ */
	ct.aptr=NULL;

	ct.fr.lupx=0;                   /* 画面枠は自動再設定だけど一応 */
	ct.fr.lupy=0;
	ct.fr.rdwx=639;
	ct.fr.rdwy=479;

	ct.move.lupx=-16384;            /* ウィンドウ移動枠だ */
	ct.move.lupy=     0;
	ct.move.rdwx= 16383;
	ct.move.rdwy= 16383;

	ct.white=15;
	ct.black=8;
	ct.gray=7;
	ct.xor=7;

	if (MMI_Open( &ct ) == NOERR)
	{
		aplId=MMI_CallMessage(MMI_GetApliId(),GM_QUERYID,QM_KIND,1);
		if(aplId>0)
		{
			MMI_CallMessage(MMI_GetApliId(),GM_SWITCH,TRUE,aplId);
		}
		else if(APL_init()==NOERR)
		{
			FDG_SaveCurDir();
			FDG_InitFileDlg();

			FlInitialize();
			NfInitialize();
		
			MMI_ExecSystem();

			FDG_FreeFileDlg() ;
			FDG_RecovCurDir() ;
		}
	}

	/*	終了処理	*/
	MMI_Close() ;

	return 0;
}


static FRAME ResumeFrame;
static char ResumeId[]="%%% YSRENAME %%%";
static char ResumeTitle[]="YSRENAME V0.2";

int APL_init()
{
	extern MMIINIT initDataMAINGUS;
	extern MMIINIT initDataNEWFGUS;
	extern MMIINIT initDataSTSPGUS;
	extern MMIINIT initDataEXTGUS;
	register int	ret ;

	guiEgbPtr = MMI_GetEgbPtr() ;
	EGB_tmenuPalette2(guiEgbPtr);


	if ((ret = MMI_initHyper()) < 0)return ret ;
	if ((ret = MMI_initDialogL40()) < 0)return ret ;
	if ((ret = MMI_initAlertL40()) < 0)return ret ;
	if ((ret = MMI_initWindowL40()) < 0)return ret ;
	if ((ret = MMI_initMessageL40()) < 0)return ret ;
	if ((ret = MMI_initMenuL40()) < 0)return ret ;
	if ((ret = MMI_initButtonL40()) < 0)return ret ;
	if ((ret = MMI_initDrawButtonL40()) < 0)return ret ;
	if ((ret = MMI_initIconL40()) < 0)return ret ;
	if ((ret = MMI_initToggleIconL40()) < 0)return ret ;
	if ((ret = MMI_initMenuItemL40()) < 0)return ret ;
	if ((ret = MMI_initScrollBarL40()) < 0)return ret ;
	if ((ret = MMI_initTextL40()) < 0)return ret ;
	if ((ret = MMI_initListMenuL40()) < 0)return ret ;
	if ((ret = MMI_initNumBoxL40()) < 0)return ret ;


	if ((ret = MMI_Init(&initDataMAINGUS)) < 0)
		return ret ;
	if ((ret = MMI_Init(&initDataNEWFGUS)) < 0)
		return ret ;
	if ((ret = MMI_Init(&initDataSTSPGUS)) < 0)
		return ret ;
	if ((ret = MMI_Init(&initDataEXTGUS)) < 0)
		return ret ;


	MMI_SetUpPrgMenu(pdTwId, 2);
	MMI_SetUpSDKMenu(pdTwId,-3);
	SetShellFunc();


	/* EIN(TM) SetUp */
	char work[RSMWORKSIZE];	// リジュームマネージャのワークエリア
	char buf[1024];
	char *ptr;
	int size,x,y;
	HYPER hyp;

	EIN_rsmInit( work, ResumeId );
	EIN_rsmBufSet( work, buf, 1023 );
	size = EIN_rsmLoad( work );

	if ( size > 0 ){
		if ( ((ptr = strstr(buf,"\nICON: ")) != NULL ) &&
			 (sscanf(ptr,"\nICON: %d %d\n", &x, &y) > 1 ) ){
			MMI_SendMessage( Ys11Id, MM_GETHYPER, 1, &hyp );
			ResumeFrame.lupx = x;
			ResumeFrame.lupy = y;
			ResumeFrame.rdwx = x+(hyp.fr.rdwx-hyp.fr.lupx);
			ResumeFrame.rdwy = y+(hyp.fr.rdwy-hyp.fr.lupy);

			if ( x<0 ){
				x=0;
			}
			if ( y<0 ){
				y=0;
			}
			MMI_SendMessage( Ys11Id, MM_MOVE, 1, &ResumeFrame ) ;
		}
	}


	MMI_SendMessage(MMI_GetBaseObj(), MM_SHOW, 0) ;

	return NOERR ;
}


void SaveResumeFile()
{
	char	work[RSMWORKSIZE];
	char	buf[1024];
	char	aplpath[128];
	HYPER	hyp;
	register int ret;
	
	MMI_SendMessage( Ys11Id, MM_GETHYPER, 1, &hyp );
	
	if ( (ResumeFrame.lupx  != hyp.fr.lupx) ||
	     (ResumeFrame.lupy  != hyp.fr.lupy) ){

		EIN_rsmInit( work, ResumeId);
		EIN_rsmBufSet( work, buf, 1023 );
		MMI_CallMessage( MMI_GetApliId(), GM_QUERYID, QM_PATH, (int)aplpath );
		EIN_rsmBufPrintf( work, "APLPATH: %s", aplpath );
		EIN_rsmBufPrintf( work, "TITLE:   %s", ResumeTitle);
		EIN_rsmBufCat( work, "COMMENT: ファイルリネームツールです.");
		EIN_rsmBufPrintf( work, "ICON: %d %d", hyp.fr.lupx, hyp.fr.lupy );
		EIN_rsmBufTail( work );
		ret = EIN_rsmSave( work );

		ResumeFrame.lupx = hyp.fr.lupx;
		ResumeFrame.lupy = hyp.fr.lupy;
		ResumeFrame.rdwx = hyp.fr.rdwx;
		ResumeFrame.rdwy = hyp.fr.rdwy;
	}
}



static int ShellCallFunc(int apId,int messId,int info,int data)
{
	switch(messId)
	{
	case GM_WAKE:
	case GM_SHOW:
		return NOERR;
	case GM_QUIT:
		if(YsYesNoQuestion(Ys11Id,"終了しますか?","終了","まさか")==YSYES)
		{
			SaveResumeFile();
			YsAbortGuiLoop();
			return NOERR;
		}
		return ILLEGAL_FUNCTION;
	default:
		return ILLEGAL_FUNCTION;
	}
}

static void SetShellFunc()
{
	int bas,app;

	bas=MMI_GetBaseObj();
	app=MMI_GetApliId();
	MMI_SendMessage(bas,MM_SETEXEC,1,ShellCallFunc);
	MMI_CallMessage(app,GM_TITLE,(int)"YsRename",0);
}
