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

local mp = require 'mp'

local current_shared_title = ""
local is_hovering = false
local is_numpad_active = false
local is_pip_msg_active = false
local is_playlist_visible = false
local is_script_ready = false
local is_stats_visible = false

local function update_window_title()
    if is_script_ready and current_shared_title ~= "" then
        mp.set_property_native("title", current_shared_title)
    else
        mp.set_property_native("title", " ")
    end
end

local function display_clean_title()
    if not is_script_ready or is_numpad_active or is_playlist_visible or is_stats_visible or not mp.get_property_bool("fullscreen") then
        return
    end

    local time = mp.get_property_number("time-pos")
    if not time or time <= 0 then return end

    if current_shared_title ~= "" then
        mp.osd_message(current_shared_title, 5)
    end
end

local function toggle_stats_state()
    is_stats_visible = not is_stats_visible
    if is_stats_visible then
        mp.osd_message("", 0)
        mp.set_property("osd-level", "1")
    elseif is_hovering and mp.get_property_bool("fullscreen") then
        mp.set_property("osd-level", "0")
    end
end

local function check_mouse_movement()
    if is_pip_msg_active then
        mp.commandv("script-message", "osc-visibility", "never", "no_osd")
        return
    end

    local mouse_x, mouse_y = mp.get_mouse_pos()
    local osd_w, osd_h = mp.get_osd_size()
    if not mouse_x or not mouse_y or not osd_h or not osd_w then return end

    local in_zone = mp.get_property_bool("fullscreen") and (mouse_x > (osd_w * 0.88)) and (mouse_y < (osd_h * 0.05))

    if in_zone then
        if not is_hovering then
            is_hovering = true
            mp.commandv("script-message", "osc-visibility", "always", "no_osd")
            mp.set_property("osd-level", "0")
        end
        display_clean_title()
    else
        if is_hovering or is_numpad_active or is_playlist_visible then
            is_hovering = false
            if is_numpad_active or is_playlist_visible then
                mp.commandv("script-message", "osc-visibility", "never", "no_osd")
                mp.set_property("osd-level", "0")
            else
                mp.commandv("script-message", "osc-visibility", "auto", "no_osd")
                mp.set_property("osd-level", "1")
            end
            if not is_stats_visible then mp.osd_message("", 0) end
        end
    end
end

mp.register_event("start-file", function()
    current_shared_title = ""
    is_script_ready = false
    mp.osd_message("", 0)
    mp.set_property_native("title", " ")
end)

mp.register_event("file-loaded", function()
    current_shared_title = ""
    is_script_ready = false
    is_stats_visible = false
    update_window_title()
    mp.commandv("script-message", "request-tux-title")
end)

mp.register_event("end-file", function(event)
    if event.reason == "error" then
        local media_name = (current_shared_title ~= "" and current_shared_title)
                           or mp.get_property("filename")
                           or "Sans titre"

        if #media_name > 50 then
            local truncated_name = media_name:sub(1, 50)
            local last_space = truncated_name:match("^(.*)%s.-$")
            media_name = (last_space or truncated_name) .. "..."
        end

        local osd_text = string.format("{\\an5}{\\c&H0098FF&}Échec : {\\c}%s", media_name)

        mp.osd_message("", 0)
        mp.set_osd_ass(0, 0, osd_text)
        mp.add_timeout(5, function() mp.set_osd_ass(0, 0, "") end)
    end
end)

mp.register_script_message("numpad-visible", function(state)
    is_numpad_active = (state == "yes")
    if is_numpad_active then
        mp.commandv("script-message", "osc-visibility", "never", "no_osd")
        mp.set_property("osd-level", "0")
    else
        mp.commandv("script-message", "osc-visibility", "auto", "no_osd")
        mp.osd_message("", 0)
        mp.set_property("osd-level", "1")
    end
end)

mp.register_script_message("pip-msg-active", function(state)
    is_pip_msg_active = (state == "yes")
end)

mp.register_script_message("playlist-visible", function(state)
    is_playlist_visible = (state == "yes")
    if is_playlist_visible then
        mp.commandv("script-message", "osc-visibility", "never", "no_osd")
        mp.osd_message("", 0)
        mp.set_property("osd-level", "1")
    elseif is_hovering and mp.get_property_bool("fullscreen") then
        mp.set_property("osd-level", "0")
    end
end)

mp.register_script_message("update-tux-title", function(title)
    current_shared_title = title or ""
    if is_script_ready then update_window_title() end
end)

mp.observe_property("fullscreen", "bool", function(_, val)
    if not val and is_hovering then
        is_hovering = false
        mp.osd_message("", 0)
        mp.set_property("osd-level", "1")
    end
end)

mp.observe_property("playlist-pos", "number", function()
    is_script_ready = false
    mp.set_property_native("title", " ")
end)

mp.observe_property("time-pos", "number", function(_, time)
    if time and time > 0 and not is_script_ready then
        is_script_ready = true
        display_clean_title()
        update_window_title()
    end
end)

mp.add_forced_key_binding("i", "stats-toggle-sync", function()
    toggle_stats_state()
    mp.commandv("script-binding", "stats/display-stats-toggle")
end)

mp.add_periodic_timer(0.1, check_mouse_movement)
