/////////////////////////////////////////////////////////////////////////////
// Fuel_sys_init.c
// 
/////////////////////////////////////////////////////////////////////////////

//	Set up gauge header
GAUGE_CALLBACK	              Fuel_sys_init_cb;
char Fuel_sys_init_gauge_name[]	= GAUGE_NAME;
extern PELEMENT_HEADER	      Fuel_sys_init_list;
extern MOUSERECT              Fuel_sys_init_mouse_rect[];

GAUGE_HEADER_FS700(GAUGE_W, Fuel_sys_init_gauge_name, &Fuel_sys_init_list, \
						0, Fuel_sys_init_cb, 0, 0, 0);
/*---------------------------------------------------------------------------
------------ Make ICONS macroses     ----------------------------------------
---------------------------------------------------------------------------*/

MAKE_ICON
(		
	Fuel_sys_init_icon,
	NULL,
	NULL,
	NULL,
	IMAGE_USE_ERASE | IMAGE_USE_BRIGHT | IMAGE_USE_TRANSPARENCY,
	0,
	0,0,

	MODULE_VAR_NONE, NULL,

	ICON_SWITCH_TYPE_SET_CUR_ICON,
	0,
	0,
	0
)

PELEMENT_HEADER		Fuel_sys_init_icon_list[] =
{
	&Fuel_sys_init_icon.header,
	NULL
};
/////////////////////////////////////////////////////////////////////////////
MAKE_STATIC
(
	Fuel_sys_init_background,
	NULL,
	&Fuel_sys_init_icon_list,
	NULL,
	IMAGE_USE_TRANSPARENCY,
	0,
	0,0
)

PELEMENT_HEADER		Fuel_sys_init_list= &Fuel_sys_init_background.header;

//---------------------------------------------------------------------------
//-----------------------   Call back guage   -------------------------------
//---------------------------------------------------------------------------
void FSAPI	Fuel_sys_init_cb (PGAUGEHDR pgauge, int service_id, UINT32 extra_data)
{
	PPOWER_SUPLY_STATUS_var = (PINT)POWER_SUPLY_STATUS_var.var_ptr;  // If power suply on?
	switch(service_id)
	{
	case    PANEL_SERVICE_PRE_INSTALL:
		break;

	case    PANEL_SERVICE_PRE_INITIALIZE:
		initialize_var_by_name(&POWER_SUPLY_STATUS_var, "POWER_SUPLY_STATUS");
		break;

	case    PANEL_SERVICE_PRE_UPDATE:

		// Check if power supported by power suply system
		if ( PPOWER_SUPLY_STATUS_var != NULL && (*PPOWER_SUPLY_STATUS_var == 2 ||  
			*PPOWER_SUPLY_STATUS_var == 4 || *PPOWER_SUPLY_STATUS_var == 5 ||
			*PPOWER_SUPLY_STATUS_var == 6 || *PPOWER_SUPLY_STATUS_var == 7) )// defined in PS gauge
			power_good = TRUE;
		else power_good = FALSE;

		if ( power_good == FALSE ){ Pump_1_L =-1; Pump_2_L = -1; Pump_1_R = -1; Pump_2_R = -1;}


		lookup_var( &FUEL_TANK_SELECTOR_var);
		lookup_var( &FUEL_TANK_SELECTOR_2_var);
		//---- Auto consume update
		if (T4_man_pump_1 == 0 || T4_man_pump_2 == 0 || T2L_man_pump == 0 || T2R_man_pump == 0)
			Auto_fuel_consume = -1;

		//---- Pump left 1/2 close
		if (Pump_1_L == -1 && Pump_2_L == -1 && FUEL_TANK_SELECTOR_var.var_value.e != FUEL_TANK_SELECTOR_OFF){ 
			trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_OFF);
		}
		//---- Pump left 1/2 open
		if (Pump_1_L == 0 || Pump_2_L == 0 && FUEL_TANK_SELECTOR_var.var_value.e != FUEL_TANK_SELECTOR_RIGHT_AUX){ 
			
			if (Auto_fuel_consume == 0) { // Auto consume on
				if ( Tank_1_L >= 2500 && Auto_fuel_consume == 0) { // more then 2500 kg
					trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_LEFT_AUX);
				}
				else if ( Tank_1_L < 2500 && Tank_1_L >= 10 && Tank_4 >= 10 && Auto_fuel_consume == 0 ){ // Less then 2500 kg , fed from Tank4
					trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_CENTER2);
				}
				else if ( Tank_1_L < 2500 && Tank_1_L >= 10 && Tank_4 < 10 && Auto_fuel_consume == 0){ // Less then 2500 kg , feed from Tank1
					trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_LEFT_AUX);
				}
				else if ( Tank_1_L < 10 && Auto_fuel_consume == 0){ // feed from Tank2
						trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_LEFT_TIP);
				}
			}

			else if (Auto_fuel_consume == -1){ // Auto consume off
				if (T4_man_pump_1 == 0){
					trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_CENTER2);  // Feed from Tank 4
				}
				if (T2L_man_pump == 0){
					trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_LEFT_TIP); // Feed from Tank 2 L
				}
				if (T1L_man_pump == 0){
					trigger_key_event (KEY_FUEL_SELECTOR_SET, FUEL_TANK_SELECTOR_LEFT_AUX); // Feed from Tank 1 L
				}
			}
		}

		//---- Pump right 1/2 close
		if (Pump_1_R == -1 && Pump_2_R == -1){// && FUEL_TANK_SELECTOR_2_var.var_value.e != FUEL_TANK_SELECTOR_OFF){ 
			trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_OFF);
		}
		//---- Pump right 1/2 open
		if (Pump_1_R == 0 || Pump_2_R == 0){// && FUEL_TANK_SELECTOR_2_var.var_value.e != FUEL_TANK_SELECTOR_RIGHT_AUX){ 
			
			if (Auto_fuel_consume == 0) { // Auto consume on
				if ( Tank_1_R >= 2500 && Auto_fuel_consume == 0) { // more then 2500 kg
					trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_RIGHT_AUX);
				}
				else if ( Tank_1_R < 2500 && Tank_1_R >= 10 && Tank_4 >= 10 && Auto_fuel_consume == 0){ // Less then 2500 kg , fed from Tank4
					trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_CENTER2);
				}
				else if ( Tank_1_R < 2500 && Tank_1_R >= 10 && Tank_4 < 10 && Auto_fuel_consume == 0){ // Less then 2500 kg , feed from Tank1
					trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_RIGHT_AUX);
				}
				else if ( Tank_1_R < 10 && Auto_fuel_consume == 0){ // feed from Tank2
						trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_RIGHT_TIP);
				}
			}

			else if (Auto_fuel_consume == -1){ // Auto consume off
				if (T4_man_pump_2 == 0){
					trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_CENTER2);
				}
				if (T2R_man_pump == 0){
					trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_RIGHT_TIP); // Feed from Tank 2 R
				}
				if (T1R_man_pump == 0){
					trigger_key_event (KEY_FUEL_SELECTOR_2_SET, FUEL_TANK_SELECTOR_RIGHT_AUX); // Feed from Tank 1 R
				}
			}
		}

		break;

	case    PANEL_SERVICE_PRE_DRAW:
		break;

	case    PANEL_SERVICE_PRE_KILL:
		break;
	}

}
#undef GAUGE_NAME
#undef GAUGEHDR_VAR_NAME
#undef GAUGE_W
