-- created by Bertrand Legruley
-- lestrucsetastucesdetux@free.fr
-- lestrucsetastucesdetux.pages-perso.free.fr

local mp = require 'mp'

local function execute_action(direction)
    local duration = mp.get_property_number("duration", 0)
    local playlist_count = mp.get_property_number("playlist-count", 0)
    local seekable = mp.get_property_native("seekable")

    if (duration <= 0) or (seekable == false) then
        if playlist_count > 1 then
            if direction == "next" then
                mp.command("playlist-next")
            else
                mp.command("playlist-prev")
            end
        else
            mp.commandv("seek", (direction == "next" and 10 or -10))
        end
    else
        mp.commandv("seek", (direction == "next" and 10 or -10))
    end
end

local function handle_back()
    execute_action("prev")
end

local function handle_forward()
    execute_action("next")
end

mp.add_forced_key_binding("LEFT", "smart_left", handle_back)
mp.add_forced_key_binding("MBTN_BACK", "smart_mback", handle_back)
mp.add_forced_key_binding("MBTN_FORWARD", "smart_mforward", handle_forward)
mp.add_forced_key_binding("RIGHT", "smart_right", handle_forward)
