
#include "H\General.H"
#include "H\Disco.H"
#include "H\Credito.H"
#include "H\Calc.H"
#include "H\Teclado.H"
#include "H\Efectivo.H"
#include "H\Gastos.H"
#include "H\Inst.H"
#include "H\Prog.H"
#include "H\Pend.H"
#include "H\Bancos.H"

UInt8	  		cgp[10];	// Colores
Char            opg;		// Operación general

// Menu
#define MAcerca		200
#define MEditCred	100
#define MEditGast	101
#define MEditInst	102
#define MEditIngr	103

void IniciaCreditos(FormType *f) { opg=1; IniciaCR(cgp,1,f,NULL); }
void IniciaEfectivo(FormType *f) { opg=2; IniciaEF(cgp,1,f); }
void IniciaProgramados(Char t) 	 { opg=5; IniciaPR(t,cgp); }
void IniciaBancos() 			 { opg=6; IniciaBA(cgp,1,NULL); }

void CargaResumen(double *v) {
Int16     i;
INF *     ApI;
       for(i=0;i<7;i++) v[i]=0;	
       ApI = SacarApRM();
       for (i=0;i<SacarTotReg();i++) {
       	  if(!LeerReg(ApI,i)) return;
       	  if(ApI->TReg == 1) v[2]+=((CRE*)ApI)->sc;
       	  if(ApI->TReg == 2) v[1]+=((EFE*)ApI)->sa;
       	  if(ApI->TReg == 6) { v[4]+=((PROG*)ApI)->sfi-((PROG*)ApI)->sfe;
       	  	                   v[5]+=((PROG*)ApI)->svi-((PROG*)ApI)->sve; }
       	  if(ApI->TReg == 7) if(((REGBA*)ApI)->TipoCDI != 3 || 
       	           (((REGBA*)ApI)->Vista || EsteMes(&((REGBA*)ApI)->fv)))
       	                  v[0]+=PasaNaF(&((REGBA*)ApI)->sa);
       }
       v[3]=v[0]+v[1]+v[2];
       v[6]=v[3]+v[4]+v[5];
}

// *****************************************************************
//
// 									Principal
//
// *****************************************************************
// Pintar la forma -------------------------------------------------
void PonVal(double v, Char cg, Char *st, Int8 *cc) {
	AstrX(v,st,true,true);
	*cc=-1; if (cg>2) *cc=((v<0)?cgp[5]:cgp[3]);
}
void PintaFP(FormType *f, Boolean ini) {
UInt16         x,y;
Char 		   st[150];
double         v[8];
Char           cg;
Int8           cc,w;
	/*
Coord          cx,cy;
	WinGetWindowExtent(&cx,&cy); 
	y=cy-30; PintaB(255,0,y,160,30,3,0,0,0,false,cgp);
    x=3; y+=3; for (i=0;i<6;i++,x+=26) PintaB(i,x,y,24,24,4,2,0,1,true,cgp);
    */
    FrmDrawForm(f); 
    CargaResumen(v); cg=PrefGen(1); x=4; y=16;
	PintaB(255,0,y,160,FntCharHeight(),7,0,0,0,false,cgp);
	SacaTexto(st,1000,2,50); y+=PonTexto(st,80,y,B,C,0,false,cgp[(cg>2)?0:2],cgp[7],&w);
    SacaTexto(st,1000,3,50); PonTexto(st,x,y,N,I,0,false,-1,-1,&w);
    PonVal(v[0],cg,st,&cc); y+=PonTexto(st,158,y,N,D,0,false,cc,-1,&w);
    SacaTexto(st,1000,4,50); PonTexto(st,x,y,N,I,0,false,-1,-1,&w);
    PonVal(v[1],cg,st,&cc); y+=PonTexto(st,158,y,N,D,0,false,cc,-1,&w);
    SacaTexto(st,1000,5,50); PonTexto(st,x,y,N,I,0,false,-1,-1,&w);
    PonVal(v[2],cg,st,&cc); y+=PonTexto(st,158,y,N,D,0,false,cc,-1,&w)+2;
    PintaB(255,0,y,160,FntCharHeight(),6,0,0,6,false,cgp);
    SacaTexto(st,1000,6,50); PonTexto(st,x,y,B,I,0,false,-1,cgp[6],&w);
    if (cg<=2) WinDrawLine(158-70,y-1,156,y-1);
    PonVal(v[3],cg,st,&cc); y+=PonTexto(st,158,y,B,D,0,false,cc,cgp[6],&w)+3;
	PintaB(255,0,y,160,FntCharHeight(),7,0,0,0,false,cgp);
    SacaTexto(st,1000,7,50); y+=PonTexto(st,80,y,B,C,0,false,cgp[(cg>2)?0:2],cgp[7],&w);
    SacaTexto(st,1000,8,50); PonTexto(st,x,y,N,I,0,false,-1,-1,&w);
    PonVal(v[4],cg,st,&cc); y+=PonTexto(st,158,y,N,D,0,false,cc,-1,&w);
    SacaTexto(st,1000,9,50); PonTexto(st,x,y,N,I,0,false,-1,-1,&w);
    PonVal(v[5],cg,st,&cc); y+=PonTexto(st,158,y,N,D,0,false,cc,-1,&w)+5;
    PintaB(255,0,y,160,FntCharHeight()+3,(cg>2)?8:2,9,0,1,true,cgp); 
    y+=1; x=4; 
    SacaTexto(st,1000,72,50); PonTexto(st,x,y,B,I,0,false,-1,cgp[(cg>2)?8:2],&w);
    PonVal(v[6],cg,st,&cc); PonTexto(st,158,y,B,D,0,false,cc,cgp[(cg>2)?8:2],&w);
}
// Menu ------------------------------------------------------------
Boolean M_Principal(UInt16 command) {
	switch (command) {
		case MAcerca	 : FrmPopupForm(FAcerca); return true;
		case MEditGast   : opg=IniciaGastos(0,1,FPrincipal,NULL,false,cgp,0); return true;
		case MEditIngr   : opg=IniciaGastos(1,1,FPrincipal,NULL,false,cgp,0); return true;
    	case MEditInst   : opg=IniciaInst(1,FPrincipal,NULL,false,cgp); return true;		
//		case MCambiaClave: FrmPopupForm(FCambClave); return true;
	}
	return false;
}
// Determinar si está sobre un campo -------------------------------
Char EnEvP(Int16 xp, Int16 yp) {
Int16 y,h;
    h=FntCharHeight();
    y=16+h; 	if (yp>=y && yp<y+h) return 1;	// Bancos
    y+=h; 		if (yp>=y && yp<y+h) return 2;	// Efectivo
    y+=h; 		if (yp>=y && yp<y+h) return 3;	// Crédito
    y+=h+2; 	if (yp>=y && yp<y+h) return 4;	// Saldo
    y+=(2*h)+3;	if (yp>=y && yp<y+h) return 5;	// Fijo
    y+=h;		if (yp>=y && yp<y+h) return 6;	// Variable
    return 0;
}
// Principal -------------------------------------------------------
static Boolean Principal(EventType* pEvent) {
	switch (pEvent->eType) {
		case menuEvent:
			return M_Principal(pEvent->data.menu.itemID);
		case frmUpdateEvent:
			switch(pEvent->data.frmUpdate.updateCode) {
				case 98:
				case 99: opg=0; break;
				default: return false;
			}
			PintaFP(FrmGetActiveForm(),false); 
			return true;
		case frmOpenEvent:
		 	opg=0; PintaFP(FrmGetActiveForm(),true);
			return true;
		case frmCloseEvent:
		    switch(opg) {
	    	case 1: FinalizaCR(true); break;
	    	case 2: FinalizaEF(); break;
	    	case 3: FinalizaGA(); break;
	    	case 4: FinalizaIN(); break;
	    	case 5: FinalizaPR(); break;
	    	case 6: FinalizaBA(true); break;	    	
		    }
		    FrmDeleteForm(FrmGetActiveForm());
		    return true;
        case penDownEvent:
            switch(EnEvP(pEvent->screenX,pEvent->screenY)) {
            	case 1: IniciaBancos(); return true;            	            	
            	case 2: IniciaEfectivo(FrmGetActiveForm()); return true;
           		case 3: IniciaCreditos(FrmGetActiveForm()); return true;
           		case 5: IniciaProgramados(1); return true;
           		case 6: IniciaProgramados(2); return true;
            }
		    return false;
		default: break;
	}
	return false;
}




// *****************************************************************
//
// 						Inicio de la aplicación
//
// *****************************************************************
static Boolean AppHandleEvent(EventType* pEvent) {
UInt16 		formId;
FormType* 	pForm;
Boolean		handled = false;
	if (pEvent->eType == frmLoadEvent) {
		formId = pEvent->data.frmLoad.formID;
		pForm = FrmInitForm(formId);
		FrmSetActiveForm(pForm);
		switch (formId) {
			// General
			case FPrincipal 	: FrmSetEventHandler(pForm, Principal); break;
			case FAdver			:
			case FAcerca		: 
			case FAyuda		    : 
			case FError			: FrmSetEventHandler(pForm, AyudaError); break;
			case FPeriodo       : FrmSetEventHandler(pForm, RPeriodo); break;
			case FPeriodoC      : FrmSetEventHandler(pForm, RPeriodo); break;
			// Créditos
			case FResumenCR 	: FrmSetEventHandler(pForm, ResumenCR); break;
			case FSaldoCorteCR 	: FrmSetEventHandler(pForm, SaldoCorteCR); break;
			case FDetallesCR	: FrmSetEventHandler(pForm, DetallesCR); break;
			case FVerDatosCR	: FrmSetEventHandler(pForm, VerDatosCR); break;
			case FDetGenCR  	: FrmSetEventHandler(pForm, DetGenCR); break;
			case FEditarCR_TC	: FrmSetEventHandler(pForm, EditarCR_TC); break;
			case FEditarCR_TX	: FrmSetEventHandler(pForm, EditarCR_TX); break;
			case FSmaTC         : FrmSetEventHandler(pForm, SmaTC); break;
			case FPagAdiTX		: FrmSetEventHandler(pForm, PagAdiTX); break;
			case FEdtPagAdiTX	: FrmSetEventHandler(pForm, EdtPagAdiTX); break;
			case FRegComCR   	: FrmSetEventHandler(pForm, RegMovCR); break;
			case FRegDepCR   	: FrmSetEventHandler(pForm, RegMovCR); break;
			case FRegRetCR   	: FrmSetEventHandler(pForm, RegMovCR); break;
			case FRegCarCR  	: FrmSetEventHandler(pForm, RegMovCR); break;
			case FRegAboCR  	: FrmSetEventHandler(pForm, RegMovCR); break;
			case FRegPagosCR    : FrmSetEventHandler(pForm, RegPagosCR); break;
			case FPagosVencidos : FrmSetEventHandler(pForm, PagosVencidosTX); break;
			case FVerDocTX 		: FrmSetEventHandler(pForm, VerDocTX); break;
			case FVerPagTX 		: FrmSetEventHandler(pForm, VerDocTX); break;
			case FPagDocTX 		: FrmSetEventHandler(pForm, PagDocTX); break;
			case FPagTX 		: FrmSetEventHandler(pForm, PagDocTX); break;
			// Efectivo
			case FResumenEF 	: FrmSetEventHandler(pForm, ResumenEF); break;
			case FEditarEF  	: FrmSetEventHandler(pForm, EditarEF); break;
			case FEditarEF_1	: FrmSetEventHandler(pForm, EditarEF); break;
			case FEditarEF_2	: FrmSetEventHandler(pForm, EditarEF); break;
			case FEditarEF_3	: FrmSetEventHandler(pForm, EditarEF); break;
			case FEditarEF_4	: FrmSetEventHandler(pForm, EditarEF); break;
			// Gastos
			case FGastos    	: FrmSetEventHandler(pForm, Gastos); break;
			case FEdtGastos     : FrmSetEventHandler(pForm, EdtGastos); break;
			case FGastosM    	: FrmSetEventHandler(pForm, Gastos); break;
			// Instituciones
			case FInst      	: FrmSetEventHandler(pForm, Inst); break;
			case FEdtInst       : FrmSetEventHandler(pForm, EdtInst); break;
			case FInstM     	: FrmSetEventHandler(pForm, Inst); break;
			// Programados
			case FResumenPR		: FrmSetEventHandler(pForm, ResumenPR); break;
			case FConfirmarPR	: FrmSetEventHandler(pForm, ConfirmarPR); break;
			// Seleccionar cuenta
			case FSelecCuenta   : FrmSetEventHandler(pForm, SelecCuenta); break;
			// Pendientes
			case FPendientes	: FrmSetEventHandler(pForm, RPendientes); break;
			case FConfPend		: FrmSetEventHandler(pForm, RConfPend); break;
			// Calculadora y teclado
			case FCalcula   	: FrmSetEventHandler(pForm, Calcula); break;
			case FTecladoX		: FrmSetEventHandler(pForm, TecladoX); break;
			default				: break;
			// Bancos
			case FResumenBA		: FrmSetEventHandler(pForm, ResumenBA); break;			
    		case FCuentaBA		: FrmSetEventHandler(pForm, CuentaBA); break;
    		case FListaMovBA    : FrmSetEventHandler(pForm, ListaMovBA); break;    		
    		case FDatosBA       : FrmSetEventHandler(pForm, DatosBA); break;    		    		
    		case FDetalleBA     : FrmSetEventHandler(pForm, DetalleBA); break;    		    		    		
    		case FDepositoBA    : FrmSetEventHandler(pForm, DepositoBA); break;    		    		    		
    		case FCarCreBA      : FrmSetEventHandler(pForm, CarCreBA); break;    		    		    		    		
    		case FRetiroBA      : FrmSetEventHandler(pForm, RetiroBA); break;    		    		    		    		
		}
		handled = true;
	}
	return handled;
}
static Err AppStart(void) { 
RGBColorType  ct;
	
	InicioBD();
	if (SacarTotReg()==0) { 
		IniciaRegEfe(); IniciaRegGA(); IniciaRegIN(); 
		IniciaRegPR();
	}
	FntSetFont(stdFont);
    IniciaColores(cgp);
    ActualizaSaldosTC();
    ActualizaSaldosEF();
    ActualizaSaldosBA();
    switch(PrefGen(1)) {
	case  1: break;
	case  2: break;
   	default: ct.r=220; ct.g=230; ct.b=255; UIColorSetTableEntry(UIMenuFill,&ct);
    		 ct.r=128; ct.g=  0; ct.b=  0; UIColorSetTableEntry(UIAlertFrame,&ct); 
    		 ct.r=255; ct.g=255; ct.b=200; UIColorSetTableEntry(UIAlertFill,&ct);
			 ct.r=255; ct.g=255; ct.b=100; UIColorSetTableEntry(UIFieldTextHighlightBackground,&ct);
             ct.r=  0; ct.g=  0; ct.b=  0; UIColorSetTableEntry(UIMenuFrame,&ct);
                                           UIColorSetTableEntry(UIMenuForeground,&ct);
                                           UIColorSetTableEntry(UIFieldText,&ct);
                                           UIColorSetTableEntry(UIObjectForeground,&ct);
    		 							   UIColorSetTableEntry(UIObjectFrame,&ct);
    		 ct.r=  0; ct.g=  0; ct.b=128; UIColorSetTableEntry(UIMenuSelectedFill,&ct);
    		 							   UIColorSetTableEntry(UIFormFrame,&ct);
    		 							   UIColorSetTableEntry(UIDialogFrame,&ct);
    		 							   UIColorSetTableEntry(UIFieldTextHighlightForeground,&ct);
    		 							   UIColorSetTableEntry(UIObjectSelectedFill,&ct);
    		 ct.r=255; ct.g=255; ct.b=255; UIColorSetTableEntry(UIMenuSelectedForeground,&ct);
    		 							   UIColorSetTableEntry(UIFormFill,&ct);
   										   UIColorSetTableEntry(UIDialogFill,&ct);
   										   UIColorSetTableEntry(UIFieldBackground,&ct);
   										   UIColorSetTableEntry(UIObjectSelectedForeground,&ct);
   										   UIColorSetTableEntry(UIObjectFill,&ct);
    		 ct.r=128; ct.g=128; ct.b=128; UIColorSetTableEntry(UIFieldTextLines,&ct);
    		 break;
    }
    opg=0; FrmGotoForm(FPrincipal); 
	return errNone; 
}
static void AppStop(void) { FinBD(); FrmCloseAllForms(); }

static void AppEventLoop(void) {
Err			error;
EventType	event;
	do { EvtGetEvent(&event, evtWaitForever);
 		 if (SysHandleEvent(&event)) continue;
		 if (MenuHandleEvent(0, &event, &error)) continue;
		 if (AppHandleEvent(&event)) continue;
		 FrmDispatchEvent(&event);
	} while (event.eType != appStopEvent);
}
UInt32 PilotMain(UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags) {
Err error = errNone;
	switch (cmd) {
		case sysAppLaunchCmdNormalLaunch:
			if ((error=AppStart())==0) { AppEventLoop(); AppStop(); }
			break;
		default: break;
	}
	return error;
}
