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


local update_time_step = 0.01 --update will be called 50 times per second
make_default_activity(update_time_step) 
sensor_data                     = get_base_data()
local   dev                     = GetSelf()
local   aircraft                = get_aircraft_type()

--[[ --------------------------------------------------------------------
--  Mainpanel Hijacking
local   MISC_TAXI_LIGHT         = get_param_handle("MISC_TAXI_LIGHT")
-------------------------------------------------------------------- ]]
--  Update Connectors Position              Step 1/3
local   connector = {}
connector["TMB_MISC_TAXI-LIGHT"]= nil
--------------------------------------------------------------------
--  Variables Declaration
local   CPT_ILL,CPT_ILL_ROT                 
local   n_flash_stat            =   0       --  0:Off   1:Flashing
local   n_flash                 =   0
local   n_flash_s               =   1.5
local   c_flash_stat            =   0       --  0:Off   1:Flashing
local   c_flash                 =   0
local   c_flash_s               =   1
local   ANTICOL                 =   0
local   LIGHT_POS               =   0  
local   LIGHT_FORM,PROC

local   navlight_s
local   GEARLIGHT               =   0   
local   LCS                     =   get_param_handle("LICS_READY") 
local   ECS                     =   get_param_handle("ECS_READY")
local   COCKPIT_ILLUMINATION    =   get_param_handle("COCKPIT_ILLUMINATION")
local   TMB_INTERIOR_STBY_INST  =   get_param_handle("TMB_INTERIOR_STBY_INST") 
local   INTERIOR_FLT            =   get_param_handle("INTERIOR_FLT")
local   GEARLIGHT_POS           =   get_param_handle("GEARLIGHT_POS")
local   ANTICOL_POS             =   get_param_handle("ANTICOL_POS")
local   POSITION_POS            =   get_param_handle("POSITION_POS")
local   TEST                    =   get_param_handle("TEST")

--[[ -RADIO,TEST,ANTICOL,LICS_READY,ECS_READY,COCKPIT_ILLUMINATION,TMB_INTERIOR_STBY_INST,INTERIOR_FLT,GEARLIGHT_POS,ANTICOL_POS,POSITION_POS
 ]]
--------------------------------------------------------------------

local   function lics_hot_start()
    dev:performClickableAction(device_commands.LIGHT_FORM, 0, false) 
    --dev:performClickableAction(device_commands.LIGHT_POS, 0.5, false) 
    dev:performClickableAction(device_commands.LIGHT_COCKPIT, 1, false)
    dev:performClickableAction(device_commands.LIGHT_COCKPIT_ILLUMINATION, 1, false)
    CPT_ILL                     =   1
    CPT_ILL_ROT                 =   1
    ANTICOL                     =   -1
    --LIGHT_POS                   =   5
end
local   function lics_cold_start()
    dev:performClickableAction(device_commands.LIGHT_FORM, 0, false) 
    --dev:performClickableAction(device_commands.LIGHT_POS, 0, false) 
    dev:performClickableAction(device_commands.LIGHT_COCKPIT, -1, false)
    dev:performClickableAction(device_commands.LIGHT_COCKPIT_ILLUMINATION, 0.1, false)
    CPT_ILL                     =   -1
    CPT_ILL_ROT                 =   0
    ANTICOL                     =   -1
    --LIGHT_POS                   =   0
end

function post_initialize()
    --print_message_to_user("DEV.LIGHT_CONTROL_SYSTEM",0.2)
  
   
	local birth = LockOn_Options.init_conditions.birth_place
--------------------------------------------------------------------------------------------------------
--  Update Connectors Position              Step 2/3
    connector["TMB_MISC_TAXI-LIGHT"] 			= get_clickable_element_reference("TMB_MISC_TAXI-LIGHT")
--------------------------------------------------------------------------------------------------------

  
    if birth=="GROUND_HOT" or birth=="AIR_HOT" then
        
        lics_hot_start() 
    elseif birth=="GROUND_COLD" then
        lics_cold_start()
    end
    dev:listen_command(Keys.iCommandPlaneCockpitIllumination)
    dev:listen_command(Keys.iCommandPlaneHeadLightOnOff)
    dev:listen_command(Keys.iCommandPlaneAntiCollisionLights)
    dev:listen_command(Keys.iCommandPlaneLightsOnOff)

end
function SetCommand(command,value)
     --print_message_to_user(tostring(command).." = "..tostring(value)) 
    if                  command     ==  device_commands.LIGHT_LDG_TAXI                              or
                        command     ==  Keys.iCommandPlaneHeadLightOnOff                            then
                        GEARLIGHT   =   GEARLIGHT + 1
        if              GEARLIGHT   >   1                                                           then
                        GEARLIGHT   =   -1
        end

    end 
    if                  ECS:get()   ==  1                                                           then
        if              command     ==  Keys.iCommandPlaneCockpitIllumination                       or
                        command     ==  device_commands.LIGHT_COCKPIT                               then
                        CPT_ILL         =   CPT_ILL +  2
            if          CPT_ILL         >   1                                                       then
                        CPT_ILL         =   -1
            end 
            if          CPT_ILL         ==  1                                                       then
                
                        CPT_ILL_ROT     =   1
            end   
        end
        if              CPT_ILL     ==  1                                                           then
            if          command     ==  device_commands.LIGHT_COCKPIT_ILLUMINATION                  then
                if      value       >   0                                                           then
                        CPT_ILL_ROT =   CPT_ILL_ROT + 0.05
                elseif  value       <   0                                                           then
                        CPT_ILL_ROT =   CPT_ILL_ROT - 0.05
                    
                end
                if      CPT_ILL_ROT <   0.1                                                         then
                        CPT_ILL_ROT =   0.1
                elseif  CPT_ILL_ROT >   1                                                           then
                        CPT_ILL_ROT =   1
                end
            end
        else            CPT_ILL_ROT =   0
        end
          
        if              command     ==  device_commands.LIGHT_ANTICOL                               or
                        command     ==  Keys.iCommandPlaneAntiCollisionLights                       then
                        ANTICOL     =  ANTICOL + 2
            if          ANTICOL     >   1                                                           then
                        ANTICOL     =  -1
            end
            if
                        ANTICOL     == 1                                                            then
                        c_flash_stat=   1
            elseif      ANTICOL     == -1                                                           then
                        c_flash_stat=   0
            end
        end
        if              command     ==  device_commands.LIGHT_POS                                   then
            if          value       ==  1                                                           then
                        LIGHT_POS   =   LIGHT_POS + 0.1
        elseif          value       ==  -1                                                          then
                        LIGHT_POS   =   LIGHT_POS - 0.1
        end
                
            if          LIGHT_POS   ==  0.6                                                         then
                        LIGHT_POS   =   0.7
            elseif      LIGHT_POS   <   0                                                           then
                        LIGHT_POS   =   0
            end
        elseif          command     ==  Keys.iCommandPlaneLightsOnOff                               and
                        LIGHT_POS   ==   0                                                          then
                        LIGHT_POS   =   0.5
        elseif          command     ==  Keys.iCommandPlaneLightsOnOff                               and
                        LIGHT_POS   ==  0.5                                                         then
                        LIGHT_POS   =   0.7
            if          ANTICOL     ==  -1                                                          then
                        dispatch_action(nil,Keys.iCommandPlaneAntiCollisionLights)
            end
        elseif          command     ==  Keys.iCommandPlaneLightsOnOff                               and
                        LIGHT_POS   ==  0.7                                                         and
                        ANTICOL     ==  1                                                           then
                        dispatch_action(nil,Keys.iCommandPlaneAntiCollisionLights)
                        LIGHT_POS   =   0
        end
        
            if          LIGHT_POS   >=  0                                                           and
                        LIGHT_POS   <=  0.6                                                         then
                        n_flash_stat=   0
                                        set_aircraft_draw_argument_value(190,LIGHT_POS)   --navl
                                        set_aircraft_draw_argument_value(191,LIGHT_POS)   --navr 
                                        set_aircraft_draw_argument_value(192,LIGHT_POS)   --strob
            elseif      LIGHT_POS   ==  0.7                                                         then
                        n_flash_stat=   1 
            elseif      LIGHT_POS   >   0.7                                                         then
                        LIGHT_POS   =   0
            end                               
        
        if              n_flash_stat==   1                                                          then
                        n_flash     =   n_flash_s
        elseif          n_flash_stat==  0                                                           then
                        n_flash     =   0
        end
        if              c_flash_stat==  1                                                           then
                        c_flash     =   c_flash_s
        elseif          c_flash_stat==  0                                                           then
                        c_flash     =   0
        end
        if              command     ==  device_commands.LIGHT_FORM                                   then
                        LIGHT_FORM   =   value
                                        
        end
    end
end


 
    




function update()
--[[ --------------------------------------------------------------------
--  Sync landing and taxi lights with lever via Mainpanel
    if          get_aircraft_draw_argument_value(208)   ==  1   then
                MISC_TAXI_LIGHT:set(-1)

    elseif      get_aircraft_draw_argument_value(209)   ==  1   then
                MISC_TAXI_LIGHT:set(1)
    elseif      get_aircraft_draw_argument_value(208)   ==  0   and     
                get_aircraft_draw_argument_value(209)   ==  0   then    
                MISC_TAXI_LIGHT:set(0)
    end
-------------------------------------------------------------------- ]]
--  Update Connectors Position              Step 3/3
    if  connector["TMB_MISC_TAXI-LIGHT"]	then
		connector["TMB_MISC_TAXI-LIGHT"]:update()
	end
--------------------------------------------------------------------
--  Navigation lights logic
    if                  n_flash     >   0                                                               then
                        n_flash     =   n_flash - update_time_step
        if              n_flash     <=  0                                                               then
                        n_flash     =   n_flash_s
        end
        if              n_flash     >=  0.5                                                            and
                        n_flash     >=  1.00                                                           then
                        set_aircraft_draw_argument_value(190,1)   --navl
                        set_aircraft_draw_argument_value(191,1)   --navr 
                        set_aircraft_draw_argument_value(192,1)   --strob
        else    
                        set_aircraft_draw_argument_value(190,0)   --navl
                        set_aircraft_draw_argument_value(191,0)   --navr 
                        set_aircraft_draw_argument_value(192,0)   --strob
        end
    end
    if                  LIGHT_POS   ==  0                                                               then
                        set_aircraft_draw_argument_value(190,0)   --navl
                        set_aircraft_draw_argument_value(191,0)   --navr 
                        set_aircraft_draw_argument_value(192,0)   --strob

    end
--------------------------------------------------------------------
--  Collision lights flash logic
    if                  c_flash     >   0                                                               then
                        c_flash     =   c_flash - update_time_step
        if              c_flash     <=  0                                                               then
                        c_flash     =   c_flash_s
        end
        if              c_flash     >=  0.5                                                             and
                        c_flash     <=  0.6                                                             then 
                        set_aircraft_draw_argument_value(199,1)   --coll
        else    
                        set_aircraft_draw_argument_value(199,0)   --coll
        end
    end
--------------------------------------------------------------------
--  Sync Formation lights with rotary
                        set_aircraft_draw_argument_value(88,LIGHT_FORM)
--------------------------------------------------------------------
--  Gearlight Stuff
    if                  GEARLIGHT       ==  0                                                           and
                        ECS:get()       ==  1                                                           then
                        set_aircraft_draw_argument_value(208,0)   
                        set_aircraft_draw_argument_value(209,0)   
    end
    if                  GEARLIGHT       ==  1                                                           and
                        ECS:get()       ==  1                                                           then
                        set_aircraft_draw_argument_value(208,0)   
                        set_aircraft_draw_argument_value(209,1)
    end
    if                  GEARLIGHT       ==  -1                                                           and
                        ECS:get()       ==  1                                                           then
                        set_aircraft_draw_argument_value(208,1)   
                        set_aircraft_draw_argument_value(209,0)
    end
    if                  ECS:get()       ==  0                                                           then
                        set_aircraft_draw_argument_value(190,0)   --navl
                        set_aircraft_draw_argument_value(191,0)   --navr 
                        set_aircraft_draw_argument_value(192,0)   --strob
                        set_aircraft_draw_argument_value(208,0)   
                        set_aircraft_draw_argument_value(209,0)
    end
    if                  ECS:get()       ==  1                                                           then
    TMB_INTERIOR_STBY_INST:set(CPT_ILL)
    INTERIOR_FLT:set(CPT_ILL_ROT)
    COCKPIT_ILLUMINATION:set(CPT_ILL_ROT)
    GEARLIGHT_POS:set(GEARLIGHT)
    ANTICOL_POS:set(ANTICOL)
    POSITION_POS:set(LIGHT_POS)
    else
    TMB_INTERIOR_STBY_INST:set(-1)
    INTERIOR_FLT:set(0)
    COCKPIT_ILLUMINATION:set(0)
    GEARLIGHT_POS:set(0)
    ANTICOL_POS:set(-1)
    POSITION_POS:set(0)

    end
    if          ANTICOL         ==  1     and
                LIGHT_POS       >   0     then
                LCS:set(1)

    else        LCS:set(0)
    end 

TEST:set(get_cockpit_draw_argument_value(181))
end

need_to_be_closed = false -- close lua state after initialization


