dofile(LockOn_Options.script_path.."devices.lua")
dofile(LockOn_Options.script_path.."command_defs.lua")

local electric_system = GetSelf()
local sensor_data = get_base_data()

local update_time_step = 0.05

make_default_activity(update_time_step)

-- ***********************
local lWINGS_POS_ARG  = get_param_handle("WINGSPOS_ARG")
-- local SWEEPLEV  = get_param_handle("SWEEPLEV")

electric_system:listen_command(Keys.PlanePackWing)
electric_system:listen_command(Keys.PlanePackDownWing)

SYSTEM = {}
SYSTEM['SWEEP-STAT'] = 3001

wings_val = 1
wings_cmd = 0
wings_step = 0.005

wings_cur_angle = 4
wings_prev_angle = 0
wings_direction = 1

wings_positions = {0, 44, 61, 100}
wings_pos_val = 2

function post_initialize()
	wings_val =  1
	-- SWEEPLEV:set(100)
end	
	
function SetCommand(command,value)
	debug("command="..command)	
	
	if command == SYSTEM['SWEEP-STAT'] then
		wings_pos_val =  tonumber(string.format("%.3f", value) * 10 )  
		debug("wings_pos_val="..wings_pos_val)
		if wings_pos_val > wings_prev_angle then
			wings_prev_angle = wings_pos_val
			wings_direction = -1
			wings_cmd = 1
			wings_cur_angle = wings_cur_angle + (1 * wings_direction)
		else
			wings_prev_angle = wings_pos_val
			wings_direction = 1
			wings_cmd = 1
			wings_cur_angle = wings_cur_angle + (1 * wings_direction)	
		end
	end
	
	if command == Keys.PlanePackDownWing and wings_cur_angle > 1 and wings_cmd == 0 then   
			wings_direction = -1
			wings_cmd = 1
			wings_cur_angle = wings_cur_angle + (1 * wings_direction)
	elseif command == Keys.PlanePackWing and wings_cur_angle < 4 and wings_cmd == 0 then   
			wings_direction = 1
			wings_cmd = 1
			wings_cur_angle = wings_cur_angle + (1 * wings_direction)		
	  end

end

function update()
 
	local flaps_pos = sensor_data.getFlapsPos()
	flaps_pos = 0
	if wings_cmd == 1 and flaps_pos == 0   then
		wings_val = wings_val + (wings_step * wings_direction)
		if wings_val < 0 or math.floor(wings_val * 100) == wings_positions[wings_cur_angle] then
			wings_cmd = 0
		end
		debug('wings_val='..wings_val.." / wings_cur_angl="..wings_cur_angle)
	end
	
	set_aircraft_draw_argument_value(7, wings_val)
	lWINGS_POS_ARG:set(wings_val)
	
end

need_to_be_closed = false
