/////////////////////////////////////////////////////////////////////////////
// Power_intro.c
// Key of VSU gererator 
/////////////////////////////////////////////////////////////////////////////
//
//signed int Power_intro = 0;

//	Set up gauge header
GAUGE_CALLBACK			Power_intro_cb;
char Power_intro_gauge_name[]	= GAUGE_NAME;
extern PELEMENT_HEADER	Power_intro_list;
extern MOUSERECT      	Power_intro_mouse_rect[];

GAUGE_HEADER_FS700(GAUGE_W, Power_intro_gauge_name, &Power_intro_list, \
				   0, Power_intro_cb, 0, 0, 0);

/////////////////////////////////////////////////////////////////////////////
/*---------------------------------------------------------------------------
------------ Make ICONS macroses     ----------------------------------------
---------------------------------------------------------------------------*/

MAKE_ICON
(		
 Power_intro_icon,
 NULL,
 NULL,
 NULL,
 IMAGE_USE_ERASE | IMAGE_USE_TRANSPARENCY,
 0,
 0,0,

 MODULE_VAR_NONE, NULL,

 ICON_SWITCH_TYPE_SET_CUR_ICON,
 2,
 0,
 0
 )

 PELEMENT_HEADER		Power_intro_icon_list[] =
{
	&Power_intro_icon.header,
	NULL
};
/////////////////////////////////////////////////////////////////////////////
MAKE_STATIC
(
 Power_intro_background,
 NULL,
 &Power_intro_icon_list,
 NULL,
 IMAGE_USE_TRANSPARENCY,
 0,
 0,0
 )

 PELEMENT_HEADER		Power_intro_list= &Power_intro_background.header;
//---------------------------------------------------------------------------
//-----------------------   Call back guage   -------------------------------
//---------------------------------------------------------------------------


void FSAPI	Power_intro_cb (PGAUGEHDR pgauge, int service_id, UINT32 extra_data)
{
	papu_status = (PBOOL)apu_status.var_ptr;

	switch(service_id)
	{
	case    PANEL_SERVICE_PRE_INSTALL:
		register_var_by_name(&POWER_SUPLY_STATUS_var, TYPE_UINT32, "POWER_SUPLY_STATUS");
		break;

	case    PANEL_SERVICE_PRE_INITIALIZE:
		initialize_var_by_name(&apu_status, "APU_STATUS");
		break;

	case    PANEL_SERVICE_PRE_UPDATE:
		// Power supply status
		if ( AKK_1 == 1 && AKK_2 == 1 && AKK_3 == 1 && AKK_4 == 1 &&
			 GVSU == 0 && VU1 == 0 && VU2 == 0 && G1 != 1 && G2 != 1)
			POWER_SUPLY_STATUS_var = AKKs; // 1

		else if ( AKK_1 == 1 && AKK_2 == 1 && AKK_3 == 1 && AKK_4 == 1 &&
			 GVSU == 1 )
			POWER_SUPLY_STATUS_var = AKKs_APU; // 2

		else if ( AKK_1 == 1 && AKK_2 == 1 && AKK_3 == 1 && AKK_4 == 1 &&
			 GVSU == 0 && (VU1 == 1 || VU2 == 1) && G1 != 1 && G2 != 1)
			POWER_SUPLY_STATUS_var = AKKs_VU1_2; // 3

		else if ( AKK_1 == 1 && AKK_2 == 1 && AKK_3 == 1 && AKK_4 == 1 &&
				  VU1 == 0 && VU2 == 0 && (G1 == 1 || G2 == 1) )
			POWER_SUPLY_STATUS_var = AKKs_G1_2; // 4

		else if ( (AKK_1 == 0 || AKK_2 == 0 || AKK_3 == 0 || AKK_4 == 0) &&
			      VU1 == 0 && VU2 == 0 && (G1 == 1 || G2 == 1) )
			POWER_SUPLY_STATUS_var = G1_2; // 5

		else if ( (AKK_1 == 0 || AKK_2 == 0 || AKK_3 == 0 || AKK_4 == 0) &&
			      (VU1 == 1 || VU2 == 1) && (G1 == 1 || G2 == 1) )
			POWER_SUPLY_STATUS_var = G1_2_VU1_2; // = 6

		else if ( AKK_1 == 1 && AKK_2 == 1 && AKK_3 == 1 && AKK_4 == 1 &&
				 GVSU == 0 && (VU1 == 1 || VU2 == 1) && (G1 == 1 || G2 == 1) )
			POWER_SUPLY_STATUS_var = AKKs_G1_2_VU1_2; // = 7
		else 
			POWER_SUPLY_STATUS_var = NO_POWER; // = 0


		// Init of AKK_1,2,3,4
		lookup_var(&MASTER_BATTERY_var);
		lookup_var(&TICK18_var);
		if ( AKK_1 == -1 && MASTER_BATTERY_var.var_value.n == 1){
			trigger_key_event (KEY_TOGGLE_MASTER_BATTERY,0); // Off master battery, init state of system
			time_counter = TICK18_var.var_value.n;
		}
		else if ( (AKK_1 == 1 || AKK_2 == 1 || AKK_3 == 1 || AKK_4 == 1) && MASTER_BATTERY_var.var_value.n == 0){
				trigger_key_event (KEY_TOGGLE_MASTER_BATTERY,0); // On master battery
		}
		else if ( AKK_1 == 0 && AKK_2 == 0 && AKK_3 == 0 && AKK_4 == 0 && MASTER_BATTERY_var.var_value.n == 1){
			trigger_key_event (KEY_TOGGLE_MASTER_BATTERY,0); // Off master battery
		}
		// If more then 30 min are used only AKKs wo VU1/2 close it
		if ( ( time_counter - TICK18_var.var_value.n ) > 18*60*30 ){ 
			AKK_1 = 0;
			AKK_2 = 0;
			AKK_3 = 0;
			AKK_4 = 0;
		}

		// On/Off all power suply key lights
		if (AKK_1 == 1 && AKK_3 == 1) L_from_AKK = 1; else  L_from_AKK = 0;
		if (AKK_2 == 1 && AKK_4 == 1) R_from_AKK = 1; else  R_from_AKK = 0;
		if (AKK_1 == 1 && AKK_2 == 1 && AKK_3 == 1 && AKK_4 == 1 && G1 == -1){
			G1 = 0;
			G2 = 0;
			VU1 = 0;
			VU2 = 0;
			PPO_R = 0;
			PPO_L = 0;
			GP2 = 0;
			GP1 = 0;
		}
		else if (AKK_1 != 1 && AKK_2 != 1 && AKK_3 != 1 && AKK_4 != 1){
			G1 = -1;
			G2 = -1;
			VU1 = -1;
			VU2 = -1;
			PPO_R = -1;
			PPO_L = -1;
			GP2 = -1;
			GP1 = -1;
		}

		// VU1/2
		if ( VU1 == 1 && VU2 == 1 && (AKK_1 != 0 && AKK_2 != 0 && AKK_3 != 0 && AKK_4 != 0)){
			time_counter = TICK18_var.var_value.n;
		}
		// GVSU 
		if ( G1 == 1 && G2 == 1){
			GVSU = 0;
			RAP = 0;
		}

		break;

	case    PANEL_SERVICE_PRE_DRAW:
		break;

	case    PANEL_SERVICE_PRE_KILL:
		//unregister_var_by_name("VU_AKK_RAP_STATUS_FOR_LS");
		//unregister_var_by_name ("VU_AKK_RAP_STATUS");
		unregister_var_by_name ("POWER_SUPLY_STATUS");
		break;
	}

}
#undef GAUGE_NAME
#undef GAUGEHDR_VAR_NAME
#undef GAUGE_W
