-- (C) audiobirdxp / o. schmidt 2017 / 2018

--  ============================================== SETTING values ========================================================== 

is_success = 0
--  ============================================== FINDING datarefs ========================================================== 

-- general


is_command_cabindoor_closed = find_dataref("laminar/b738/fmodpack/cabindoor_closed")
is_command_cargo_loading = find_dataref("laminar/b738/fmodpack/fmod_play_cargo")

--  ============================================== FINDING commands ========================================================== 

fwd_L_toggle = find_command("laminar/B738/door/fwd_L_toggle")
fwd_R_toggle = find_command("laminar/B738/door/fwd_R_toggle")
aft_L_toggle = find_command("laminar/B738/door/aft_L_toggle")
aft_R_toggle = find_command("laminar/B738/door/aft_R_toggle")
aft_cargo_toggle = find_command("laminar/B738/door/aft_cargo_toggle")
fwd_cargo_toggle = find_command("laminar/B738/door/fwd_cargo_toggle")


-- door ratios --
door_ratio_fwd_L = find_dataref("737u/doors/L1")
door_ratio_fwd_R = find_dataref("737u/doors/R1")
door_ratio_aft_L = find_dataref("737u/doors/L2")
door_ratio_aft_R = find_dataref("737u/doors/R2")
door_ratio_fwd_cargo = find_dataref("737u/doors/Fwd_Cargo")
door_ratio_aft_cargo = find_dataref("737u/doors/aft_Cargo")



-------------   ULTIMATE ONLY!!! -------------------------


--  ============================================== CREATING datarefs ========================================================== --

-- Zibo and Ultimate

is_success = create_dataref("axp/success", "number")
is_cargo_sounds = create_dataref("axp/cargo_sounds_on", "number")

--  ============================================== CREATING functions ========================================================== --

function success()

    is_success = 1

end

function AXP_autoclose_all_doors_CMDhandler(phase, duration)
-- check for open doors and close them --
-- necessary if user fails to follow procedures  --

    if phase == 0 then

            if door_ratio_fwd_L == 1 then fwd_L_toggle:once() end
            if door_ratio_fwd_R == 1 then fwd_R_toggle:once() end
            if door_ratio_aft_L == 1  then aft_L_toggle:once() end
            if door_ratio_aft_R == 1  then aft_R_toggle:once() end
            if door_ratio_fwd_cargo == 1 then fwd_cargo_toggle:once() end
            if door_ratio_aft_cargo == 1  then aft_cargo_toggle:once() end
    end

end

function AXP_handle_cargo_sounds()

    if is_command_cabindoor_closed == 1 then
        is_cargo_sounds = 0
    elseif is_command_cabindoor_closed == 0 then
        if is_command_cargo_loading == 1 then 
            is_cargo_sounds = 1 
            if door_ratio_fwd_cargo == 0 then fwd_cargo_toggle:once() end
            if door_ratio_aft_cargo == 0 then aft_cargo_toggle:once() end
        elseif is_command_cargo_loading == 0 then 
            is_cargo_sounds = 0 
        end
    end
end



--  ============================================== CREATING commands ========================================================== --

-- Zibo and Ultimate

AXP_CMD_autoclose_all_doors = create_command("axp/commands/autoclose_all_doors", "This will close all open doors of the aircraft", AXP_autoclose_all_doors_CMDhandler)


--  ============================================== CREATING functions ========================================================== --

function ultimate_secure_doors()

    if is_command_cabindoor_closed == 1 then

        AXP_CMD_autoclose_all_doors:once()
    

    end
    

end




-- =================================================================== RUNNING FUNCTIONS  ===============================================================================


function flight_start()

    is_cargo_sounds = 0
    is_success = 0


end

function after_physics() 

    -- ultimate_secure_doors()
    AXP_handle_cargo_sounds()
    success()

end
