
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.02 --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()
--------------------------------------------------------------------
--  Variables Declaration

local   birth                   =   LockOn_Options.init_conditions.birth_place
local   SYSTEM_STAT
local   OXYGEN                  =   0
local   P_CABIN                 =   0
local   OXY_ESCAPE              =   0
local   OXY_ESCAPE_S            =   0.1
local   OXY_PRESURISE           =   0
--[[ local   SYSTEM_STAT,SYSTEM_TARGET,REFUEL_STAT,REFUEL_INIT 
local   FUEL_TANK,FUEL_CENTER,FUEL_WINGS,FUEL_DUMP,FUEL_EXTTR,FUEL_EMERTR,FUEL_REFUEL ]]
local   OCS                     =   get_param_handle("OCS_READY") 
local   ECS                     =   get_param_handle("ECS_READY") 
local   CANOPY_POS              =   get_param_handle("BASE_SENSOR_CANOPY_POS")
local   LIQUID_OXY              =   get_param_handle("LIQUID_OXY")                  -- -1: 0        0:2.5       1:5
local   PSI_OXY                 =   get_param_handle("PSI_OXY")                     -- -1: 0        -0.92:Green Start -0.52:Green Stop 0:125       1:450
local   PSI_CABIN               =   get_param_handle("PSI_CABIN")                   -- -1: 0        0:25        1:50
--------------------------------------------------------------------
local   function    oxygen_hot_start()
    dev:performClickableAction(device_commands.OXYGEN_SUPPLY, 1, false)  
    dev:performClickableAction(device_commands.OXYGEN_FLOX_ADJ, -1 , false)
    dev:performClickableAction(device_commands.OXYGEN_FLOW, 0 , false)
    OXYGEN                      =   0.90
    P_CABIN                     =   0    

end    
local   function    oxygen_cold_start()
    dev:performClickableAction(device_commands.OXYGEN_SUPPLY, -1, false)  
    dev:performClickableAction(device_commands.OXYGEN_FLOX_ADJ, -1 , false)
    dev:performClickableAction(device_commands.OXYGEN_FLOW, 0 , false)
    OXYGEN                      =   1
    P_CABIN                     =   -1
end

function post_initialize()
    print_message_to_user("DEV.OXYGEN_CONTROL_SYSTEM",0.2)  
    sndhost_cockpit                 =   create_sound_host("COCKPIT", "2D",0,0,0) 
    OXYGEN_FLOW_TEST 	            =   sndhost_cockpit:create_sound("Aircrafts/OXYGEN_FLOW_TEST") 
    OXYGEN_SUPPLY_ON                =   sndhost_cockpit:create_sound("Aircrafts/OXYGEN_SUPPLY_ON") 

    if              birth           ==      "GROUND_HOT"                or 
                    birth           ==      "AIR_HOT"                   then
                    oxygen_hot_start()
    elseif          birth           ==      "GROUND_COLD"               then
                    oxygen_cold_start()
    end
end
local function oxygen_supply_on()   
    if              OXYGEN             >   0                             then
                    OXY_PRESURISE      =   1
                    OXYGEN_SUPPLY_ON:play_once()
                    print_message_to_user("oxygen_supply_on")
    end
end
local function oxygen_supply_off()
                    OXY_ESCAPE     =   0
                    OXYGEN_SUPPLY_ON:stop()
                    print_message_to_user("oxygen_supply_off")
end
local function oxygen_supply_fail()
                    OXY_ESCAPE     =   1
                    OXYGEN_SUPPLY_ON:play_once()
                    print_message_to_user("oxygen_supply_fail")
end
local function oxygen_test()
    OXYGEN_FLOW_TEST:play_once()
    print_message_to_user("oxygen_test")
end 
function SetCommand(command,value)
    print_message_to_user(tostring(command).." = "..tostring(value))
--    if              ECS:get()       ==  1                                   then 
        if          CANOPY_POS:get()==  0                                   then
            if      command         ==  device_commands.OXYGEN_SUPPLY       then
                if  value           ==  0 or 1                              then
                    oxygen_supply_on()
                end
                if  value           == -1                                   then
                    oxygen_supply_off()
                end

            end
        elseif      CANOPY_POS:get()      ~=  0                             then
            if      command         ==  device_commands.OXYGEN_SUPPLY       then

                if  value           ==  0                               then
                     oxygen_supply_fail()
                end
                if  value           == -1                                   then
                    oxygen_supply_off()
                end
            end
        end
        if          command         ==  device_commands.OXYGEN_FLOW             and
                    value           ==  -1                                      then
                    oxygen_test()
                    --OXYGEN_FLOW_TEST:play_once()
                    SYSTEM_STAT     =   1             
        end
       
    end
        
                                           

--end


function update()  
--------------------------------------------------------------------
--  Oxygen PSI reduction logic
    if                  OXY_ESCAPE      >   0                               then
                        OXY_ESCAPE      =   OXY_ESCAPE - update_time_step
        if              OXY_ESCAPE      <=  0                               then
                        OXY_ESCAPE      =   OXY_ESCAPE_S
                        OXYGEN          =   OXYGEN -0.005
        end
        if              OXYGEN          <   -1                              then
                        OXYGEN          =   -1
                        OXY_ESCAPE      =   0
        end
    end
--------------------------------------------------------------------
--  Cabin Presurization logic
    if                  OXY_PRESURISE   >   0                               then
                        OXY_PRESURISE   =   OXY_PRESURISE - update_time_step
        if              OXY_PRESURISE   <=  0                               then
                        OXY_PRESURISE   =   OXY_ESCAPE_S
                        OXYGEN          =   OXYGEN  -   0.0025
                        P_CABIN         =   P_CABIN +   0.005
        end
        if              OXYGEN          <   -1                              then
                        OXYGEN          =   -1
                        OXY_PRESURISE   =   0
        end
        if              P_CABIN         >  0                             then
                        OXY_PRESURISE   =   0
                        OXY_ESCAPE      =   0
        end                                 
    end
--OXY_PRESURISE
--------------------------------------------------------------------
local test =    -0.52
                 OCS:set(SYSTEM_STAT)  
                 LIQUID_OXY:set(OXYGEN)
                 PSI_OXY:set(test)
                 PSI_CABIN:set(P_CABIN) 
end

need_to_be_closed = false -- close lua state after initialization


--[[ 
    arg 114 cabnin  PSI
    arg 363 OXYGEN     PSI
    	disable_built_in_oxygen_system	= true,

 ]]