local xfmc_plugin = findPluginBySignature("klm-va.com")
local xfmc_present = false
local has_any_fmc  = false -- currently only supports X-FMC

defineProperty("replay_mode", globalPropertyi("sim/operation/prefs/replay_mode"))
defineProperty("flight_time", globalPropertyf("sim/network/misc/network_time_sec"))
defineProperty("mastervol", globalPropertyi("cl300/master_vol"))

click5 = loadSample('sounds/click5.wav')

if xfmc_plugin > -1 and isPluginEnabled(xfmc_plugin) then
	xfmc_present = true
	has_any_fmc  = true
	
	-- xfmc input proxy datarefs
	createProp("xfmc/Keypath_entry", "int", 0); -- keystrokes must be written in here
	createProp("xfmc/Keypath_sync",  "int", 0); -- this will contain both a counter and the keystroke for SmartCopilot sync

	defineProperty("Keypath_entry",       globalPropertyi("xfmc/Keypath_entry"))
	defineProperty("Keypath_sync",        globalPropertyi("xfmc/Keypath_sync"))
	defineProperty("Keypath",             globalPropertyi("xfmc/Keypath"))
	
	function createXfmcCommand(name, keyval)
		registerCommandHandler(createCommand("xfmc/key_" .. name, "XFMC key proxy " .. name), 1,
				function(phase) if phase == 0 then set(Keypath_entry, keyval) end return 1 end)
	end
	
	local keys = {  LSK1   =  0, LSK2   =  1, LSK3   =  2, LSK4   =  3, LSK5   =  4, LSK6   =  5, 
					RSK1   =  6, RSK2   =  7, RSK3   =  8, RSK4   =  9, RSK5   = 10, RSK6   = 11, 
					INIT   = 12, RTE    = 13, DEPARR = 14, AP     = 15, VNAV   = 16, LEGS   = 18,
					HOLD   = 19, PERF   = 20, PROG   = 21, EXEC   = 22, MENU   = 23, TUNE   = 24,
					PREV   = 25, NEXT   = 26, A      = 27, B      = 28, C      = 29, D      = 30,
					E      = 31, F      = 32, G      = 33, H      = 34, I      = 35, J      = 36,
					K      = 37, L      = 38, M      = 39, N      = 40, O      = 41, P      = 42,
					Q      = 43, R      = 44, S      = 45, T      = 46, U      = 47, V      = 48,
					W      = 49, X      = 50, Y      = 51, Z      = 52, DEL    = 54, SLASH  = 55,
					CLR    = 56, ["1"]  = 57, ["2"]  = 58, ["3"]  = 59, ["4"]  = 60, ["5"]  = 61,
					["6"]  = 62, ["7"]  = 63, ["8"]  = 64, ["9"]  = 65, DOT    = 66, ["0"]  = 67,
					PLUS   = 68}
	for id,val in pairs(keys) do
		createXfmcCommand(id, val)
	end
end

local flight_time_last = get(flight_time)

local last_sync = 0
local last_cnt  = 0
local sync      = 0
local key       = 0
local counter   = 0

function update()
	if has_any_fmc then
		if get(flight_time)-flight_time_last == 0 and get(replay_mode) == 0 then
			stopsound = 0
		else
			stopsound = 1
		end
	end
	
	if xfmc_present then 
		-- Set XFMC key:
		sync = get(Keypath_sync)
		if sync ~= last_sync then
			-- process synced keys first:
			counter = math.floor(sync / 256)
			if counter == 0 then last_cnt = last_cnt - 256 end
			if last_cnt ~= counter + 1 then
				logError("ERROR: XFMC sync missing " .. (counter - last_cnt + 1) .. " keystrokes!")
			end
			set(Keypath, sync % 256)
			
			last_cnt = counter
			last_sync = sync
		else
			-- process local key 
			key = get(Keypath_entry)
			if key ~= 255 then
				-- new keystroke
				set(Keypath_entry, 255)
				set(Keypath, key)
				if last_cnt == 255 then last_cnt = 0 else last_cnt = last_cnt + 1 end
				last_sync = last_cnt * 256 + key
				set(Keypath_sync, last_sync)
				-- sound
				setSampleGain(click5, 1300*get(mastervol)*stopsound)
				playSample(click5, 0)
			end
		end
	end
	flight_time_last = get(flight_time)
end

