--======================================================================================================================
--  GENERAL SECTION:  Put high level descriptions here of what this script does and any information that can help you
--  understand what the code does and how/why it was written the way it was.    
--======================================================================================================================
--                                              CONSTANTS DECLARATIONS
--
--  Declare any constants required by the module.  use all CAPITAL LETTERS for constant names.
------------------------------------------------------------------------------------------------------------------------

local   SOME_CONSTANT                   =   99.999

--  Below is a 'special function' added to XLua by Laminar, that allows the Lua scripts to be reloaded  whenever the
--  flight is changed, usually by selecting a new start location.  Original implementations of XLua did not have this
--  Adding this function anywhere in the scripts tells X-Plane to reload the scripts on a flight change.  Should be
--  included with all XLua scripts nowadays.  Only need to be in one module.  We just stuck it here.

XLuaReloadOnFlightChange()


--======================================================================================================================
--                                              MODULE LOCAL VARIABLES
--
--  Declare any variables here that need to be used in multiple functions within this module.
--  Best practice is to prefix "local module variables" with 'l_'   The 'l' stands for 'local'
------------------------------------------------------------------------------------------------------------------------

 local  l_example_variable      =   10.0    --  some variable needed in multiple functions



--======================================================================================================================
--                                          FETCH X-PLANE DATAREFS that your code needs.

--  Best practice is to prefix X-Plane dataref names with 'xdr_' ...an acronym for Xplane Data Ref
------------------------------------------------------------------------------------------------------------------------
xdr_external_view                   =   find_dataref("sim/graphics/view/view_is_external")
xdr_throttle_ratio                  =   find_dataref("sim/cockpit2/engine/actuators/throttle_ratio")
xdr_prop_mode                       =   find_dataref("sim/cockpit2/engine/actuators/prop_mode")



--======================================================================================================================
--                                  CUSTOM DATAREF WRITE CALLBACK DEFINITIONS

--  Whenever a value is written to your custom dataref, then that dataref's 'write callback function (WCB)' is called. 
--  All custom datarefs require a 'write callback' function to be defined when the custom dataref is declared in the
--  next section; however, a custom dataref's WCB may not need to do anything. For example, a custom dataref used to 
--  animate a two-position switch that doesn't do anything needs to take no action when the dataref value is written 
--  to.  Another example is code that runs continuously in a flight loop function and "polls" the dataref value and 
--  takes action there instead of from inside the dataref's WCB.  Because all custom datarefs require a WCB to be 
--  specified, we provide a 'proxy' or 'placeholder' write callback that does not do anything, specifically to be used 
--  with custom datarefs that do not need write callback functionality. That is the function "empty_dr_wrb()" below

--  NOTE:  Dataref Write Callback functions MUST be declared before their function name is used as part of the custom
--  dataref declarations below.

------------------------------------------------------------------------------------------------------------------------
--  Empty write callback, for CDRs that require no action upon data writes to the dataref
function    empty_dr_wrb()
    -- for custom datarefs that need to take no action upon dataref write
end


--  Example Write Callback for "cdr_collective_ratio".  Whenever the collective on the ALIA_250 aircraft is moved in
--  sim, then this callback is called and we set X-Plane's throttle ratio based on the collective position dataref
--  value.  We just so happen to specify our collective animation to be 0 to 1, the same as X-Plane's throttle ratio
--  so that we simply have to set X-Plane's throttle ratio to be equal to the collective ratio.  We needed a script
--  so that we can set four throttle ratios (the four lifter props) by only manipulating one custom dataref (the 
--  collective position ratio).  This function, being a WCB is only run whenever the collective dataref is actively 
--  being written to by one of 3 ways:... the F1/F2 keys, the cockpit manipulator or a hardware input).  When the 
--  collective is not moving at all, this function is not being called.  This is why we call it a "write callback".
--  X-Plane only "calls us back" to run this function whenever the dataref is being written to.

function    EXAMPLE_collective_mnp_handler()
    xdr_throttle_ratio[0]   =   cdr_collective_ratio
    xdr_throttle_ratio[1]   =   cdr_collective_ratio
    xdr_throttle_ratio[2]   =   cdr_collective_ratio    
    xdr_throttle_ratio[3]   =   cdr_collective_ratio
end

--  CREATE / DEFINE ANY NEW CUSTOM DATAREF WRITE CALLBACKS HERE:




--===================================================================================================================== 
--                                          CREATE / DECLARE CUSTOM DATAREFs

--  We create / declare our custom datarefs next.  See manual section "Dataref Work > Custom Datarefs"
--  Best practice is to prefix custom dataref names with 'cdr_'....and acronym for Custom Data Ref.

--  NOTE: Some examples of custom dataref creation/declaration are shown below.  No WCBs functionalities are required
--  for most of these example datarefs because those dataref values only represent states...i.e "this knob is here" or 
--  "this annunciator is on" and only used for animation. The cdr_collective_ratio dataref; however, does need to take 
--  action whenever the dataref is written to, it needs to set specific X-Plane dataref values. See description above.
------------------------------------------------------------------------------------------------------------------------

--  EXAMPLE:  G1000 knob animations--
cdr_g1000n1_nvol_knob_anim          =   create_dataref("beta/alia_250/g1000n1/nvol_knob_anim",      "number",   empty_dr_wrb)
cdr_g1000n1_cvol_knob_anim          =   create_dataref("beta/alia_250/g1000n1/cvol_knob_anim",      "number",   empty_dr_wrb)

--  EXAMPLE:  Misc Button Annunciator lights in ALIA (i.e. Garmin 340 audio panel buttons)
cdr_beacon_button_annun             =   create_dataref("beta/alia_250/beacon_annun",                "number",   empty_dr_wrb)
cdr_strobe_button_annun             =   create_dataref("beta/alia_250/strobe_annun",                "number",   empty_dr_wrb)

cdr_collective_ratio                =   create_dataref("beta/alia_250/collective_ratio",            "number",   EXAMPLE_collective_mnp_handler)


--  CREATE / DECLARE NEW CUSTOM DATAREFS HERE:



--======================================================================================================================
--                                              COMMAND HANDLER FUNCTIONS
--
--  Commands are discrete events with a beginning and an end, (i.e. start/stop).  In between the start/stop events is
--  called the 'hold' event.  All X-Plane commands have these three events: a start, a hold and a stop event.  For each 
--  of these 3 events we can define functions to run when each event occurs.  The start event occurs first, when the 
--  command is executed.  This typically happens when a button or manipulator is pressed/clicked.  We sometimes refer 
--  to this event as "mouse down" or "key down".   The command start function runs only one time each time the command 
--  is executed.  The command hold function runs immediately after the start function is done and runs continuously 
--  until the command stop event occurs.  For a keypress, the command stop happens on "button up" and obviously, for
--  mouse clicks on "mouse up".  
------------------------------------------------------------------------------------------------------------------------

function    before_proxy()
    --  empty function
end

function    after_proxy()
    --  empty function
end


--  NAV volume Knob
------------------------------------------------------------------------------------------------------------------------
function    g1000n1_nvolup_handler(phase, duration)
    if phase    ==  0
    then
        cdr_g1000n1_nvol_knob_anim  =   ((cdr_g1000n1_nvol_knob_anim    +   1) % 45)
    end
end

function    g1000n1_nvoldn_handler(phase, duration)
    if  phase   ==  0   then
        cdr_g1000n1_nvol_knob_anim  =   ((cdr_g1000n1_nvol_knob_anim    -   1) % 45)
    end
end


--  'W' key default view command handler
------------------------------------------------------------------------------------------------------------------------
function    default_view_w_key_handler(phase, duration)
    if  phase == 0  then

    --  Pilot Head View setting
        xdr_pilot_head_x    =   DEFAULT_VIEW_LATERAL        --  set pilot head x to the constant shown
        xdr_pilot_head_y    =   DEFAULT_VIEW_VERTICAL       --  set pilot head ...y
        xdr_pilot_head_z    =   DEFAULT_VIEW_LONGITUDINAL   --  set pilot head ...z
    end
end

--======================================================================================================================
--                          COMMAND  DECLARATIONS (replace or wrap)  Handler callbacks defined above
--====================================================================================================================== 


xcmd_default_view           =   replace_command("sim/view/default_view",        default_view_w_key_handler)
xcmd_throttle_up_a_bit      =   replace_command("sim/engines/throttle_up",      lift_rotors_throttle_up_handler)        
xcmd_throttle_down_a_bit    =   replace_command("sim/engines/throttle_down",    lift_rotors_throttle_down_handler)


--  G1000 KNOB ANIMATIONS.   Augment (wrap) command callbacks with animation code to animate the G1000 knobs
------------------------------------------------------------------------------------------------------------------------
xcmd_g1000n1_nvol_up        =   wrap_command("sim/GPS/g1000n1_nvol_up",         before_proxy,       g1000n1_nvolup_handler)
xcmd_g1000n1_nvol_dn        =   wrap_command("sim/GPS/g1000n1_nvol_dn",         before_proxy,       g1000n1_nvoldn_handler)

xcmd_g1000n1_cvol_up        =   wrap_command("sim/GPS/g1000n1_cvol_up",         before_proxy,       g1000n1_cvolup_handler)
xcmd_g1000n1_cvol_dn        =   wrap_command("sim/GPS/g1000n1_cvol_dn",         before_proxy,       g1000n1_cvoldn_handler)


--======================================================================================================================
--                                              GENERIC FLIGHT LOOP CALLBACK (FLCB)
--======================================================================================================================
function    generic_FLCB()

    xdr_com1_power  =   math.min(xdr_bus_volts[0],  1)
    xdr_com2_power  =   math.min(xdr_bus_volts[0],  1)

--  Pusher collective wheel animation.  Piggybacking off of this flight loop function for convenience.

    if  xdr_prop_mode[4]        ==  3   then
        cdr_pusher_wheel_anim       =   xdr_throttle_ratio[4] * -1
    else
        cdr_pusher_wheel_anim       =   xdr_throttle_ratio[4]       
    end
end

--  Run the flight loop at 24 FPS  No need to run it faster....no frame rate sensitive functionality here.
run_at_interval(generic_FLCB, 0.04167)

--======================================================================================================================
--                                      INITIALIZATION STUFFS
--======================================================================================================================

function    initialization()
    xdr_ibr[0]              =   1   --  default the instrument brightness ratio[0] to 1.  Otherwise it defaults to 
                                    --  0.75 and the G1000 screens look washed out.  This may change as XP12 evolves.

    --  executes the 'w' key on initialization, to set the default view to the CONSTANT values set at the top of the file.
    xcmd_default_view:once()
end

run_after_time(initialization,  0.2)