-- Load in OBS via Tools > Scripts, then select the VibeTube browser source.
local obs = obslua
local source_name = ""
local hotkeys = {}
local motions = {
    {"clapping", "Clapping"},
    {"house-dancing", "House Dance"},
    {"quick-formal-bow", "Formal Bow"},
    {"victory-idle", "Victory"},
    {"waving-gesture", "Short Wave"},
    {"waving", "Wave"},
    {"whatever-gesture", "Whatever"},
    {"typing", "Typing"},
    {"praying", "Praying"},
    {"stop", "Return to Idle"},
}

local expressions = {
    {"neutral", "Neutral"},
    {"smile", "Smile"},
    {"surprise", "Surprise"},
    {"focus", "Focused"},
    {"awkward", "Awkward"},
    {"angry", "Angry"},
}

local function send_action(event, id)
    if source_name == "" then return end
    local source = obs.obs_get_source_by_name(source_name)
    if source == nil then return end
    if obs.obs_source_get_id(source) == "browser_source" then
        local data = obs.calldata_create()
        obs.calldata_set_string(data, "eventName", event)
        obs.calldata_set_string(data, "jsonString", '{"id":"' .. id .. '"}')
        local handler = obs.obs_source_get_proc_handler(source)
        obs.proc_handler_call(handler, "javascript_event", data)
        obs.calldata_destroy(data)
    end
    obs.obs_source_release(source)
end

function script_description()
    return "Send motion and expression hotkeys to VibeTube. Select the Browser source below, then assign the VibeTube actions under Settings > Hotkeys."
end

function script_properties()
    local props = obs.obs_properties_create()
    local field = obs.obs_properties_add_list(props, "source_name", "VibeTube source",
        obs.OBS_COMBO_TYPE_LIST, obs.OBS_COMBO_FORMAT_STRING)
    obs.obs_property_list_add_string(field, "Select a browser source", "")
    local names = {}
    local sources = obs.obs_enum_sources()
    if sources ~= nil then
        for _, source in ipairs(sources) do
            if obs.obs_source_get_id(source) == "browser_source" then
                table.insert(names, obs.obs_source_get_name(source))
            end
        end
        obs.source_list_release(sources)
    end
    table.sort(names)
    for _, name in ipairs(names) do
        obs.obs_property_list_add_string(field, name, name)
    end
    return props
end

function script_update(settings)
    source_name = obs.obs_data_get_string(settings, "source_name")
end

local function register_actions(settings, actions, prefix, label_prefix, event)
    for _, action in ipairs(actions) do
        local id, label = action[1], action[2]
        local name = prefix .. id
        local hotkey = obs.obs_hotkey_register_frontend(name, label_prefix .. label, function(pressed)
            if pressed then send_action(event, id) end
        end)
        hotkeys[name] = hotkey
        local bindings = obs.obs_data_get_array(settings, name)
        obs.obs_hotkey_load(hotkey, bindings)
        obs.obs_data_array_release(bindings)
    end
end

function script_load(settings)
    script_update(settings)
    register_actions(settings, motions, "pngtube-lite.motion.", "VibeTube: ", "pngtubeMotion")
    register_actions(settings, expressions, "vibetube.expression.", "VibeTube Expression: ", "pngtubeExpression")
end

function script_save(settings)
    for name, hotkey in pairs(hotkeys) do
        local bindings = obs.obs_hotkey_save(hotkey)
        obs.obs_data_set_array(settings, name, bindings)
        obs.obs_data_array_release(bindings)
    end
end
