-- Create a table to store previous positions of switches
local previous_positions = {}

-- Function to toggle switch
function toggle_switch(position, command_base, min, max)
    local switch_value = _G[position]
    local previous_position = previous_positions[position] or min

    local command_suffix
    if switch_value == min then
        command_suffix = "_up"
    elseif switch_value == max then
        command_suffix = "_dn"
    elseif switch_value < previous_position then
        command_suffix = "_dn"
    elseif switch_value > previous_position then
        command_suffix = "_up"
    end

    command_once(command_base .. command_suffix)
    previous_positions[position] = switch_value
end

-- Function to create command
function create_switch_command(command, description, position, command_base,
                               min, max)
    dataref(position, position, "readonly")
    create_command(command, description,
                   "toggle_switch('" .. position .. "', '" .. command_base ..
                       "', " .. min .. "," .. max .. ")", "", "")
end

if PLANE_ICAO == "B738" then
    -- Lights test (bright_test)
    create_switch_command("PassengerFlying/zibo/switch_bright_test",
                        "Toggle lights switch between TEST, BRT and DIM",
                        "laminar/B738/toggle_switch/bright_test",
                        "laminar/B738/toggle_switch/bright_test", -1, 1)

    -- Transponder Knob (transponder_mode)
    create_switch_command("PassengerFlying/zibo/switch_transponder_mode",
                        "Toggle transponder modes",
                        "laminar/B738/knob/transponder_pos",
                        "laminar/B738/knob/transponder_mode", 1, 5)
end
