aileron_trim_pos = globalProperty("sim/cockpit2/controls/aileron_trim")
battery_on_0 = globalProperty("sim/cockpit2/electrical/battery_on[0]")
battery_on_1 = globalProperty("sim/cockpit2/electrical/battery_on[1]")
engine_fires_0 = globalProperty("sim/cockpit2/annunciators/engine_fires[0]")
engine_fires_1 = globalProperty("sim/cockpit2/annunciators/engine_fires[1]")
oil_pressure_low_0 = globalProperty("sim/cockpit2/annunciators/oil_pressure_low[0]")
oil_pressure_low_1 = globalProperty("sim/cockpit2/annunciators/oil_pressure_low[1]")
fuel_pressure_low_0 = globalProperty("sim/cockpit2/annunciators/fuel_pressure_low[0]")
fuel_pressure_low_1 = globalProperty("sim/cockpit2/annunciators/fuel_pressure_low[1]")
fuel_low_0 = globalProperty("sim/flightmodel/weight/m_fuel[0]")
fuel_low_1 = globalProperty("sim/flightmodel/weight/m_fuel[1]")
fadec_on_0 = globalProperty("sim/cockpit/engine/fadec_on[0]")
fadec_on_1 = globalProperty("sim/cockpit/engine/fadec_on[1]")
chip_detected_0 = globalProperty("sim/cockpit2/annunciators/chip_detected[0]")
chip_detected_1 = globalProperty("sim/cockpit2/annunciators/chip_detected[1]")
isolvalve_0 = globalProperty("sim/cockpit2/engine/indicators/torque_n_mtr[0]")
isolvalve_1 = globalProperty("sim/cockpit2/engine/indicators/torque_n_mtr[1]")
beta_0 = globalProperty("sim/cockpit2/engine/actuators/prop_rotation_speed_rad_sec[0]")
beta_1 = globalProperty("sim/cockpit2/engine/actuators/prop_rotation_speed_rad_sec[1]")
auto_feather_arm_0 = globalProperty("sim/cockpit2/annunciators/auto_feather_arm[0]")
auto_feather_arm_1 = globalProperty("sim/cockpit2/annunciators/auto_feather_arm[1]")
propheatfail_0 = globalProperty("sim/operation/failures/rel_ice_prop_heat")
propheatfail_1 = globalProperty("sim/operation/failures/rel_ice_prop_heat2")
ice_vane_extend_0 = globalProperty("sim/cockpit/warnings/annunciators/ice_vane_extend[0]")
ice_vane_extend_1 = globalProperty("sim/cockpit/warnings/annunciators/ice_vane_extend[1]")
crossfeed_on = globalProperty("sim/cockpit2/annunciators/crossfeed_on")
speedbrake = globalProperty("sim/cockpit2/annunciators/speedbrake")
fuel_transfer_0 = globalProperty("sim/cockpit2/annunciators/fuel_transfer[0]")
fuel_transfer_1 = globalProperty("sim/cockpit2/annunciators/fuel_transfer[1]")
stall_warning = globalProperty("sim/cockpit2/annunciators/stall_warning")
taxi_light_on = globalProperty("sim/cockpit/electrical/taxi_light_on")
starter1 = globalProperty("ksgy/L410/starter1")
starter2 = globalProperty("ksgy/L410/starter2")
onground = globalProperty("sim/flightmodel/failures/onground_any")
icing = globalProperty("sim/cockpit2/annunciators/ice")
inv_0 = globalProperty("sim/cockpit2/electrical/inverter_on[0]")
inv_1 = globalProperty("sim/cockpit2/electrical/inverter_on[1]")
gen_0 = globalProperty("sim/cockpit2/electrical/generator_on[0]")
gen_1 = globalProperty("sim/cockpit2/electrical/generator_on[1]")
extpwr = globalProperty("sim/cockpit2/annunciators/external_power_on")

trq_max_left = globalProperty("sim/aircraft/controls/acf_trq_max_eng[0]")
trq_max_right = globalProperty("sim/aircraft/controls/acf_trq_max_eng[1]")
thr_ratio_0 = globalProperty("sim/cockpit2/engine/actuators/throttle_ratio[0]")
thr_ratio_1 = globalProperty("sim/cockpit2/engine/actuators/throttle_ratio[1]")
flap_18 = globalProperty("sim/cockpit2/controls/flap_ratio")
flap_18d = globalProperty("sim/flightmodel2/controls/flap_handle_deploy_ratio")
gear_deploy_ratio_0 = globalProperty("sim/flightmodel2/gear/deploy_ratio[0]")
gear_deploy_ratio_1 = globalProperty("sim/flightmodel2/gear/deploy_ratio[1]")
gear_deploy_ratio_2 = globalProperty("sim/flightmodel2/gear/deploy_ratio[2]")
sun_pitch_degrees = globalProperty("sim/graphics/scenery/sun_pitch_degrees")

ann_battery = createGlobalPropertyf("ksgy/L410/ann/battery")
ann_eng_fire_0 = createGlobalPropertyf("ksgy/L410/ann/eng_fire_0")
ann_eng_fire_1 = createGlobalPropertyf("ksgy/L410/ann/eng_fire_1")
ann_oil_pressure_low_0 = createGlobalPropertyf("ksgy/L410/ann/oil_pressure_low_0")
ann_oil_pressure_low_1 = createGlobalPropertyf("ksgy/L410/ann/oil_pressure_low_1")
ann_fuel_pressure_low_0 = createGlobalPropertyf("ksgy/L410/ann/fuel_pressure_low_0")
ann_fuel_pressure_low_1 = createGlobalPropertyf("ksgy/L410/ann/fuel_pressure_low_1")
ann_fuel_low_0 = createGlobalPropertyf("ksgy/L410/ann/fuel_low_0")
ann_fuel_low_1 = createGlobalPropertyf("ksgy/L410/ann/fuel_low_1")
ann_fadec_on_0 = createGlobalPropertyf("ksgy/L410/ann/fadec_on_0")
ann_fadec_on_1 = createGlobalPropertyf("ksgy/L410/ann/fadec_on_1")
ann_chipdetect_on_0 = createGlobalPropertyf("ksgy/L410/ann/chipdetect_on_0")
ann_chipdetect_on_1 = createGlobalPropertyf("ksgy/L410/ann/chipdetect_on_1")
ann_isolvalve_0 = createGlobalPropertyf("ksgy/L410/ann/isolvalve_0")
ann_isolvalve_1 = createGlobalPropertyf("ksgy/L410/ann/isolvalve_1")
ann_beta_0 = createGlobalPropertyf("ksgy/L410/ann/beta_0")
ann_beta_1 = createGlobalPropertyf("ksgy/L410/ann/beta_1")
ann_auto_feather_arm_0 = createGlobalPropertyf("ksgy/L410/ann/auto_feather_arm_0")
ann_auto_feather_arm_1 = createGlobalPropertyf("ksgy/L410/ann/auto_feather_arm_1")
ann_propheatfail_0 = createGlobalPropertyf("ksgy/L410/ann/propheatfail_0")
ann_propheatfail_1 = createGlobalPropertyf("ksgy/L410/ann/propheatfail_1")
ann_ice_vane_extend_0 = createGlobalPropertyf("ksgy/L410/ann/ice_vane_extend_0")
ann_ice_vane_extend_1 = createGlobalPropertyf("ksgy/L410/ann/ice_vane_extend_1")
ann_crossfeed_on = createGlobalPropertyf("ksgy/L410/ann/crossfeed_on")
ann_speedbrake = createGlobalPropertyf("ksgy/L410/ann/speedbrake")
aileron_trim = createGlobalPropertyf("ksgy/L410/lights/aileron_trim")
ann_fuel_transfer_0 = createGlobalPropertyf("ksgy/L410/ann/fuel_transfer_0")
ann_fuel_transfer_1 = createGlobalPropertyf("ksgy/L410/ann/fuel_transfer_1")
ann_stall_warning = createGlobalPropertyf("ksgy/L410/ann/stall_warning")
ann_taxi_light_on = createGlobalPropertyf("ksgy/L410/ann/taxi_light_on")
ann_starter = createGlobalPropertyf("ksgy/L410/ann/starter")
ann_flap_18 = createGlobalPropertyf("ksgy/L410/ann/flap_18")
ann_icing = createGlobalPropertyf("ksgy/L410/ann/icing")
ann_inv_0 = createGlobalPropertyf("ksgy/L410/ann/inv_0")
ann_inv_1 = createGlobalPropertyf("ksgy/L410/ann/inv_1")
ann_gen_0 = createGlobalPropertyf("ksgy/L410/ann/gen_0")
ann_gen_1 = createGlobalPropertyf("ksgy/L410/ann/gen_1")
ann_extpwr = createGlobalPropertyf("ksgy/L410/ann/extpwr")
ann_gear_green_left = createGlobalPropertyf("ksgy/L410/ann/gear_green_left")
ann_gear_green_nose = createGlobalPropertyf("ksgy/L410/ann/gear_green_nose")
ann_gear_green_right = createGlobalPropertyf("ksgy/L410/ann/gear_green_right")
ann_gear_red_left = createGlobalPropertyf("ksgy/L410/ann/gear_red_left")
ann_gear_red_nose = createGlobalPropertyf("ksgy/L410/ann/gear_red_nose")
ann_gear_red_right = createGlobalPropertyf("ksgy/L410/ann/gear_red_right")
instruments_light = createGlobalPropertyf("ksgy/L410/instruments/light")
instruments_light_knob = createGlobalPropertyf("ksgy/L410/instruments/light_knob")
m_flaps0 = createGlobalPropertyf("ksgy/L410/ann/flaps0")
m_flaps18 = createGlobalPropertyf("ksgy/L410/ann/flaps18")
m_flaps40 = createGlobalPropertyf("ksgy/L410/ann/flaps40")


set(ann_battery, get(battery_on_0))
set(m_flaps0, 0)
set(m_flaps18, 0)
set(m_flaps40, 0)
set(aileron_trim, 0)


function anim(dref, target)

	if get(dref) == target then
		return
	end

	if get(dref) < target then
		set(dref, get(dref) + 0.1)
	end

	if get(dref) > target then
		set(dref, get(dref) - 0.1)
	end
end

-- function getLight(knob, brightness) 
-- 	brightness = brightness or 35
-- 	local lit = knob*550/get(sun_pitch_degrees)/brightness
-- 	if lit > 0.8 then
-- 		lit = 0.8
-- 	end
-- 	return lit
-- end

function update()
	if get(battery_on_0) == 1 or get(battery_on_1) == 1 then 

		if(get(aileron_trim_pos) == 0) then
			if get(aileron_trim) ~= 1 then
				set(aileron_trim, 1)
			end
		elseif get(aileron_trim) ~= 0 then
			set(aileron_trim, 0)
		end

		if get(battery_on_0) == 1 and get(battery_on_1) == 1 then 
			anim(ann_battery, 0)
		else
			anim(ann_battery, 1)
		end
		
		anim(ann_eng_fire_0, get(engine_fires_0)) 
		anim(ann_eng_fire_1, get(engine_fires_1)) 
		anim(ann_oil_pressure_low_0, get(oil_pressure_low_0)) 
		anim(ann_oil_pressure_low_1, get(oil_pressure_low_1)) 
		anim(ann_fuel_pressure_low_0, get(fuel_pressure_low_0)) 
		anim(ann_fuel_pressure_low_1, get(fuel_pressure_low_1)) 
		if get(fuel_low_0) < 108 then 
			anim(ann_fuel_low_0, 1)
		else
			anim(ann_fuel_low_0, 0)
		end
		if get(fuel_low_1) < 108 then 
			anim(ann_fuel_low_1, 1)
		else
			anim(ann_fuel_low_1, 0)
		end
		if get(fadec_on_0) then
			anim(ann_fadec_on_0, 0) 
		else
			anim(ann_fadec_on_0, 1 )
		end
		if get(fadec_on_1) then
			anim(ann_fadec_on_1, 0) 
		else
			anim(ann_fadec_on_1, 1 )
		end
		anim(ann_chipdetect_on_0, get(chip_detected_0)) 
		anim(ann_chipdetect_on_1, get(chip_detected_1)) 
		if get(isolvalve_0) < get(trq_max_left)*0.18 then 
			anim(ann_isolvalve_0, 1)
		else
			anim(ann_isolvalve_0, 0)
		end
		if get(isolvalve_1) < get(trq_max_right)*0.18 then 
			anim(ann_isolvalve_1, 1)
		else
			anim(ann_isolvalve_1, 0)
		end
		-- TODO fuel by-pass
		if get(beta_0) < 150 then 
			anim(ann_beta_0, 1)
		else
			anim(ann_beta_0, 0)
		end
		if get(beta_1) < 150 then 
			anim(ann_beta_1, 1)
		else
			anim(ann_beta_1, 0)
		end
		if get(thr_ratio_0) > 0.88 then 
			anim(ann_auto_feather_arm_0, get(auto_feather_arm_0)) 
		else
			anim(ann_auto_feather_arm_0, 0) 
		end
		if get(thr_ratio_1) > 0.88 then 
			anim(ann_auto_feather_arm_1, get(auto_feather_arm_1)) 
		else
			anim(ann_auto_feather_arm_1, 0) 
		end
		
		-- TODO FEATHER PUMP  
		-- TODO HYDRPUL.
		anim(ann_propheatfail_0, get(propheatfail_0)) 
		anim(ann_propheatfail_1, get(propheatfail_1)) 
		anim(ann_ice_vane_extend_0, get(ice_vane_extend_0)) -- how to check?
		anim(ann_ice_vane_extend_1, get(ice_vane_extend_1)) -- how to check?
		anim(ann_crossfeed_on, get(crossfeed_on)) 
		-- TODO AUT BANK CONTROL green
		-- TODO AUT BANK CONTROL amber
		anim(ann_speedbrake, get(speedbrake)) 
		anim(ann_fuel_transfer_0, get(fuel_transfer_0)) -- how to check?
		anim(ann_fuel_transfer_1, get(fuel_transfer_1)) -- how to check?
		anim(ann_stall_warning, get(stall_warning))
		-- TODO doors
		-- TODO actuate transfer
		anim(ann_taxi_light_on, get(taxi_light_on)) 
		-- TODO EXTEND LAND GEAR (continuously sounding horn)
		if get(starter1) == 1 or get(starter2) == 1 then 
			anim(ann_starter, 1)
		else 
			anim(ann_starter, 0)
		end
		-- TODO water inj.
		-- TODO bag comp fire
		-- TODO cabin call
		-- TODO manual steering
		-- TODO pedal steering
		if (get(flap_18) == 0 or get(flap_18) == 1) and (get(onground) == 1) then 
			anim(ann_flap_18, 1)
		else
			anim(ann_flap_18, 0)
		end

		if (get(flap_18d) < 0.25) then 
			anim(m_flaps0, 1)
		else
			anim(m_flaps0, 0)
		end

		if (get(flap_18d) > 0.25 and get(flap_18d) < 0.75) then 
			anim(m_flaps18, 1)
		else
			anim(m_flaps18, 0)
		end

		if (get(flap_18d) > 0.75) then 
			anim(m_flaps40, 1)
		else
			anim(m_flaps40, 0)
		end


		anim(ann_icing, get(icing)) 
		-- TODO LH BRAKE RELEASED, RH BRAKE RELEASED 
		if get(inv_0) == 0 then
			anim(ann_inv_0, 1)
		else
			anim(ann_inv_0, 0)
		end
		if get(inv_1) == 0 then
			anim(ann_inv_1, 1)
		else
			anim(ann_inv_1, 0)
		end
		if get(gen_0) == 0 then
			anim(ann_gen_0, 1)
		else
			anim(ann_gen_0, 0)
		end
		if get(gen_1) == 0 then
			anim(ann_gen_1, 1)
		else
			anim(ann_gen_1, 0)
		end
		anim(ann_extpwr, get(extpwr))

		-- gears
		if get(gear_deploy_ratio_0) > 0 and get(gear_deploy_ratio_0) < 1 then
			anim(ann_gear_red_left, 1)
			anim(ann_gear_green_left, 0)
		else
			anim(ann_gear_red_left, 0)
			if get(gear_deploy_ratio_0) == 1 then
				anim(ann_gear_green_left, 1)
			else
				anim(ann_gear_green_left, 0)
			end
		end
		if get(gear_deploy_ratio_1) > 0 and get(gear_deploy_ratio_1) < 1 then
			anim(ann_gear_red_nose, 1)
			anim(ann_gear_green_nose, 0)
		else
			anim(ann_gear_red_nose, 0)
			if get(gear_deploy_ratio_1) == 1 then
				anim(ann_gear_green_nose, 1)
			else
				anim(ann_gear_green_nose, 0)
			end
		end
		if get(gear_deploy_ratio_2) > 0 and get(gear_deploy_ratio_2) < 1 then
			anim(ann_gear_red_right, 1)
			anim(ann_gear_green_right, 0)
		else
			anim(ann_gear_red_right, 0)
			if get(gear_deploy_ratio_2) == 1 then
				anim(ann_gear_green_right, 1)
			else
				anim(ann_gear_green_right, 0)
			end
		end
		
		if(get(battery_on_0) == 1) then
			if get(instruments_light) ~= get(instruments_light_knob) then
				set(instruments_light, get(instruments_light_knob))
			end
			-- if(get(sun_pitch_degrees) > 0) then
			-- 	set(instruments_light, getLight(get(instruments_light_knob)))
			-- else
			-- set(instruments_light, get(instruments_light_knob))
			-- end
		elseif get(instruments_light) ~= 0 then
			set(instruments_light, 0)
		end
	

	else
		anim(ann_battery, 0)
		anim(ann_eng_fire_0, 0)
		anim(ann_eng_fire_1, 0)
		anim(ann_oil_pressure_low_0, 0)
		anim(ann_oil_pressure_low_1, 0)
		anim(ann_fuel_pressure_low_0, 0)
		anim(ann_fuel_pressure_low_1, 0)
		anim(ann_fuel_low_0, 0)
		anim(ann_fuel_low_1, 0)
		anim(ann_fadec_on_0, 0)
		anim(ann_fadec_on_1, 0)
		anim(ann_chipdetect_on_0, 0)
		anim(ann_chipdetect_on_1, 0)
		anim(ann_isolvalve_0, 0)
		anim(ann_isolvalve_1, 0)
		anim(ann_beta_0, 0)
		anim(ann_beta_1, 0)
		anim(ann_auto_feather_arm_0, 0)
		anim(ann_auto_feather_arm_1, 0)
		anim(ann_propheatfail_0, 0)
		anim(ann_propheatfail_1, 0)
		anim(ann_ice_vane_extend_0, 0)
		anim(ann_ice_vane_extend_1, 0)
		anim(ann_crossfeed_on, 0)
		anim(ann_speedbrake, 0)
		anim(ann_fuel_transfer_0, 0)
		anim(ann_fuel_transfer_1, 0)
		anim(ann_stall_warning, 0)
		anim(ann_taxi_light_on, 0)
		anim(ann_starter, 0)
		anim(ann_flap_18, 0)
		anim(ann_icing, 0)
		anim(ann_inv_0, 0)
		anim(ann_inv_1, 0)
		anim(ann_gen_0, 0)
		anim(ann_gen_1, 0)
		anim(ann_extpwr, 0)
		anim(ann_gear_red_left, 0)
		anim(ann_gear_green_left, 0)
		anim(ann_gear_red_nose, 0)
		anim(ann_gear_green_nose, 0)
		anim(ann_gear_red_right, 0)
		anim(ann_gear_green_right, 0)
		anim(m_flaps0, 0)
		anim(m_flaps18, 0)
		anim(m_flaps40, 0)
		set(aileron_trim, 0)

		set(instruments_light, 0)
		set(instruments_light_knob, 0)
	end

end