size={900,90} 

local digital = loadFont('digital.ttf')
local clBlack = {0,0,0,1}

t = globalProperty("sim/network/misc/network_time_sec")

vvi = globalProperty("sim/cockpit2/autopilot/vvi_dial_fpm")
hdg_m = globalProperty("sim/cockpit2/autopilot/heading_mode")
nav_s = globalProperty("sim/cockpit2/autopilot/nav_status")
hsi_src = globalProperty("sim/cockpit2/radios/actuators/HSI_source_select_pilot")
apr_s = globalProperty("sim/cockpit2/autopilot/approach_status")
alt_s = globalProperty("sim/cockpit2/autopilot/altitude_hold_status")
gs_s = globalProperty("sim/cockpit2/autopilot/glideslope_status")
rev_s = globalProperty("sim/cockpit2/autopilot/backcourse_status")
vvi_status = globalProperty("sim/cockpit2/autopilot/vvi_status")
vvi_step = globalProperty("sim/aircraft/autopilot/vvi_step_ft")

alt_sel = globalProperty("sim/cockpit/autopilot/current_altitude")

alt_pilot = globalProperty("sim/cockpit2/gauges/indicators/altitude_ft_pilot")
vvi_pilot = globalProperty("sim/cockpit2/gauges/indicators/vvi_fpm_pilot")

battery_on_0 = globalProperty("sim/cockpit2/electrical/battery_on[0]")
battery_on_1 = globalProperty("sim/cockpit2/electrical/battery_on[1]")
autopilot = globalProperty("sim/cockpit/autopilot/autopilot_mode")

apf_servo_rudd = globalProperty("sim/operation/failures/rel_servo_rudd")
apf_computer = globalProperty("sim/operation/failures/rel_otto")
apf_auto_runaway = globalProperty("sim/operation/failures/rel_auto_runaway")
apf_servos = globalProperty("sim/operation/failures/rel_auto_servos")
apf_ailerons = globalProperty("sim/operation/failures/rel_servo_ailn")
apf_elevators = globalProperty("sim/operation/failures/rel_servo_elev")

--local i_hdg = loadImage("display.png", 0, 0, 150, 60)
local i_hdg = loadImage("i_hdg.png")
--local i_rdy = loadImage("display.png", 400, 0, 50, 75)
local i_rdy = loadImage("i_rdy.png")
--local i_nav = loadImage("display.png", 0, 100, 140, 60)
local i_nav = loadImage("i_nav.png")
--local i_cws = loadImage("display.png", 400, 200, 50, 75)
local i_cws = loadImage("i_cws.png")
--local i_apr = loadImage("display.png", 0, 200, 140, 60)
local i_apr = loadImage("i_apr.png")
--local i_fail = loadImage("display.png", 400, 100, 50, 75)
local i_fail = loadImage("i_fail.png")
--local i_gpss = loadImage("display.png", 400, 300, 50, 75)
local i_gpss = loadImage("i_gpss.png")
--local i_rev = loadImage("display.png", 0, 300, 140, 60)
local i_rev = loadImage("i_rev.png")
--local i_trim = loadImage("display.png", 0, 400, 165, 60)
local i_trim = loadImage("i_trim.png")
--local i_trup = loadImage("display.png", 200, 400, 50, 25)
local i_trup = loadImage("i_trup.png")
--local i_trdn = loadImage("display.png", 200, 450, 50, 25)
local i_trdn = loadImage("i_trdn.png")
--local i_alt = loadImage("display.png", 200, 0, 120, 60)
local i_alt = loadImage("i_alt.png")
--local i_gs = loadImage("display.png", 200, 200, 100, 60)
local i_gs = loadImage("i_gs.png")
--local i_vs = loadImage("display.png", 200, 100, 100, 60)
local i_vs = loadImage("i_vs.png")

local vvi_max = 1590
local vvi_ms = 0
set(vvi_step, 98.4) -- 0.5 m/s approx.

local started = 0
local starting_since = 0

local intercepting_nav_cmd = 0

local cws_active = false

-- shall be true if any servo or computer fails
local INOPERATIVE_NOW = 6
local autopilot_failed = false
local ap_failure_points = {apf_servo_rudd, apf_computer, apf_auto_runaway, apf_servos, apf_ailerons, apf_elevators}
local apf_now_pooling = 1
local warning_played = false

function checkFailuresAp()
	if get(ap_failure_points[apf_now_pooling]) == INOPERATIVE_NOW then
		autopilot_failed = true
		if not warning_played then
			playSample(loadSample("autopilot_fail.wav"), 0)
			warning_played = true
		end
		apf_now_pooling = 1
	else
		apf_now_pooling = apf_now_pooling + 1
	end
	if apf_now_pooling > 6 then
		autopilot_failed = false
		warning_played = false
		apf_now_pooling = 1
	end
end

function setCurrentVVI()
	set(vvi, math.floor((get(vvi_pilot) + 25) / 100) * 100.0)
end

function altHoldCmdCallback(phase)
	if phase == 2 then
		set(vvi, 0)
	end
	vvi_ms = 0
	return 1
end

function vsCallback(phase)
	if phase == 2 then
		setCurrentVVI()
	end
	return 1
end

function navCmdCallback(phase)
	--logInfo(phase)
	--logInfo(get(nav_s))
	--logInfo(intercepting_nav_cmd)
	if phase == 0 and get(nav_s) > 0 then
		intercepting_nav_cmd = 1
		if get(hsi_src) == 0 then
			set(hsi_src, 2)
		else
			set(hsi_src, 0)
		end
		return 0
	elseif intercepting_nav_cmd == 1 then
		if phase == 2 then
			intercepting_nav_cmd = 2
		end		
		return 0
	end
	--logInfo("-->")
	return 1
end

function vviFpmDnCallback(phase)
	if phase == 0 and get(alt_s) == 2 then
		local selected_alt = get(alt_sel)
		if get(alt_pilot) - selected_alt < 340 then
			set(alt_sel, selected_alt - 20 )
		else
			set(alt_sel, -360 )
		end
	end
	return 1
end

function vviFpmUpCallback(phase)
	if phase == 0 and get(alt_s) == 2 then
		local selected_alt = get(alt_sel)
		if selected_alt - get(alt_pilot) < 340 then
			set(alt_sel, selected_alt + 20 )
		else
			set(alt_sel, 360 )
		end
	end
	return 1
end

function cwsCallback(phase)
	if phase == 0 then
		cws_active = true
	elseif phase == 2 then
		if(get(alt_s) == 2) then
			set(alt_sel, get(alt_pilot))
		elseif get(vvi_status) > 0 then
			setCurrentVVI()
		end
		cws_active = false
	end
	return 1
end

function update()

	checkFailuresAp()
	if started == 0 then
		if starting_since == 0 then
			starting_since = get(t)
			return
		end
		if get(t) - starting_since > 5 then
			set(vvi, 0)
			set(alt_sel, 2000)
			set(vvi_step, 98.4) -- 0.5 m/s approx.
			set(hdg_m, 0)
			set(nav_s, 0)
			set(alt_s, 0)
			set(vvi_status, 0)
			started = 1
			starting_since = 0
		else
			return
		end
	end
	local _vvi = get(vvi)
	if _vvi < -vvi_max then
		set(vvi, -vvi_max)
	end
	if _vvi > vvi_max then
		set(vvi, vvi_max)
	end
	vvi_ms = get(vvi) * 0.00508
	
	if intercepting_nav_cmd == 2 then
		intercepting_nav_cmd = 0
		if get(nav_s) == 0 then
			commandOnce(findCommand("sim/autopilot/NAV"))
		end
	end
	
end

function draw()
	
	drawAll(components)
		--drawRectangle(0,0,14,60,0,1,0,1)
		 --hdg
	if get(avionics_on) == 1 and (get(battery_on_0) == 1 or get(battery_on_1) == 1)  then
		--drawTexture(get(irdy), 160, 0, 51, 75)
		--drawTexture(get(ihdg), 0, 0, 185, 75)
		if autopilot_failed then
			drawTexture(i_fail, 600, 0, 50, 75)
		elseif  get(hdg_m) == 0 and get(nav_s) == 0 and get(alt_s) == 0 and get(vvi_status) == 0 and intercepting_nav_cmd == 0 then
			drawTexture(i_rdy, 160, 0, 50, 75)
		end

		if started == 0 then
			drawTexture(i_hdg, 0, 0, 140, 60)
			drawTexture(i_nav, 230, 0, 140, 60)
			drawTexture(i_cws, 380, 0, 50, 75)
			drawTexture(i_apr, 440, 0, 140, 60)
			drawTexture(i_fail, 580, 0, 50, 75)
			drawTexture(i_gpss, 635, 0, 50, 75)
			drawTexture(i_rev, 700, 0, 140, 60)
			drawTexture(i_trim, 850, 0, 165, 60)
			drawTexture(i_trup, 1020, 35, 50, 25)
			drawTexture(i_trdn, 1020, 0, 50, 25)
			--drawTexturePart(dsply, 1000, 0, 155, 75, 0, 400, 155, 75) --alt
			drawTexture(i_alt, 1080, 0, 120, 60)
			--gs_on = "GS"
			--drawTexturePart(dsply, 1150, 0, 120, 75, 200, 300, 120, 75) --gs
			drawTexture(i_gs, 1240, 0, 100, 60)
			drawTexture(i_vs, 1360, 0, 100, 60)
			plusminus = "+ ."
			vvi_on = "VS"
			vvi_val = "88"
			-- drawBitmapText(digital, 1480,0, plusminus, 0,0,0, 1)
			-- --drawBitmapText(digital, 1540,4, vvi_val, 0,0,0, 1)
			drawText(digital, 1486+100,0, plusminus, 90, true, false, TEXT_ALIGN_CENTER, clBlack)
			drawText(digital, 1542+100,4, string.sub(vvi_val,1,1), 90, true, false, TEXT_ALIGN_CENTER, clBlack)
			drawText(digital, 1590+100,4, string.sub(vvi_val,2), 90, true, false, TEXT_ALIGN_CENTER, clBlack)
		elseif (get(autopilot) == 2 or get(autopilot) == 1) then
			if(get(hdg_m) == 1) then
				drawTexture(i_hdg, 0, 0, 140, 60) --hdg
			end
			
			if(get(nav_s) == 1 or get(nav_s) == 2) or intercepting_nav_cmd > 0 then
				drawTexture(i_nav, 230, 0, 140, 60) --nav
				if get(hsi_src) == 2 then
					drawTexture(i_gpss, 635, 0, 50, 75) --gpss
				end
			end

			if(get(apr_s) == 1 or get(apr_s) == 2) then
				drawTexture(i_apr, 440, 0, 140, 60) --apr
			end
			
			if(get(rev_s) == 1 or get(rev_s) == 2) then
				drawTexture(i_rev, 700, 0, 140, 60) --rev
			end

			if(get(alt_s) == 2) then
				drawTexture(i_alt, 1080, 0, 120, 60) --alt
			end
			
			if(get(gs_s) == 1 or get(gs_s) == 2) then
				drawTexture(i_gs, 1240, 0, 100, 60) --gs
			end		

			if(get(vvi_status) == 2) then
				--vvi_val = string.format("%.2d", get(vvi)/100)
				--Display vertical speed in m/s
				vvi_val = string.sub(string.format("%02d", math.abs(vvi_ms) * 10),-2)
				
				if (get(vvi) > 0) then
					plusminus = "+ ."
				else
					plusminus = "- ."
				end
				drawTexture(i_vs, 1360, 0, 100, 60) --vs
				drawText(digital, 1480+100,0, plusminus, 90, true, false, TEXT_ALIGN_CENTER, clBlack)
				--drawBitmapText(digital, 1540,4, vvi_val, 0,0,0, 1)
				drawText(digital, 1536+100,4, string.sub(vvi_val,1,1), 90, true, false, TEXT_ALIGN_CENTER, clBlack)
				drawText(digital, 1584+100,4, string.sub(vvi_val,2), 90, true, false, TEXT_ALIGN_CENTER, clBlack)
				
			else
				plusminus = ""
				vvi_val = ""
			end
			if cws_active then
				drawTexture(i_cws, 380, 0, 50, 75)
			end
		end
	else
		started = 0
	end
end

registerCommandHandler(findCommand("sim/autopilot/altitude_hold"), 1, altHoldCmdCallback)
registerCommandHandler(findCommand("sim/autopilot/NAV"), 1, navCmdCallback)
registerCommandHandler(findCommand("sim/autopilot/vertical_speed"), 1, vsCallback)
registerCommandHandler(findCommand("sim/autopilot/vertical_speed_down"), 1, vviFpmDnCallback)
registerCommandHandler(findCommand("sim/autopilot/vertical_speed_up"), 1, vviFpmUpCallback)
registerCommandHandler(findCommand("sim/autopilot/control_wheel_steer"), 1, cwsCallback)