--[[
    The prompt to github copilot:
        write a lua script for VLC media player that can do the following:
        we are eventually gonna do this:
            ffmpeg -i input.mp4 -ss <current time of video> -c copy output.mp4
            
        when clicking a button, the script should show a dialog box that ask for:
        - current time of video (should be filled in)
        - filename (should be filled in with the path of the current video, but is "<stem>_cut.<original_extension>")
    
    Installation:
        - Save this file as `cut_video.lua` in VLC's extensions directory
        - On Windows, the extensions directory is usually located at `C:\Program Files\VideoLAN\VLC\lua\extensions\`
        - On macOS, the extensions directory is usually located at `/Applications/VLC.app/Contents/MacOS/share/lua/extensions/`
        - On Linux, the extensions directory is usually located at `/usr/lib/vlc/lua/extensions/`
        - Restart VLC media player
        - Open a video file in VLC media player
        - Go to `View` > `Cut video using ffmpeg`
        
]]
-- VLC Lua Extension to cut video using ffmpeg
-- Save this file as `cut_video.lua` in VLC's extensions directory
function descriptor()
    return {
        title = "Cut Video",
        version = "1.0",
        author = "GitHub Copilot",
        url = 'https://github.com',
        shortdesc = "Cut video using ffmpeg",
        description = "Cut video using ffmpeg",
        capabilities = {"input-listener"}
    }
end

function activate()
    create_dialog()
end

function deactivate()
end

function close()
    vlc.deactivate()
end

function create_dialog()
    local input = vlc.input.item()
    if not input then
        vlc.msg.warn("No video playing")
        return
    end

    local current_time = vlc.var.get(vlc.object.input(), "time") / 1000000
    vlc.msg.info("Current time: " .. current_time)
    local input_path = input:uri()
    local input_name = input:name()
    local stem = input_path:gsub("file:///", ""):match("(.+)%..+")
    local extension = input_name:match(".+%.(.+)")
    local output_name = stem .. "_cut." .. extension

    local hours = math.floor(current_time / 3600)
    local minutes = math.floor((current_time % 3600) / 60)
    local seconds = math.floor(current_time % 60)
    local formatted_time = string.format("%02d:%02d:%02d", hours, minutes, seconds)

    dlg = vlc.dialog("Cut Video")
    dlg:add_label("Start Time (HH:MM:SS):", 1, 1, 1, 1)
    time_input = dlg:add_text_input(formatted_time, 2, 1, 1, 1)
    dlg:add_label("Output Filename:", 1, 2, 1, 1)
    filename_input = dlg:add_text_input(output_name, 2, 2, 1, 1)
    dlg:add_button("Cut", cut_video, 1, 3, 1, 1)
    dlg:add_button("Close", close, 2, 3, 1, 1)
end

function cut_video()
    local time = time_input:get_text()
    local filename = filename_input:get_text()

    -- Ensure filename is a valid path
    if filename:find('"') then
        vlc.msg.err("Invalid filename")
        return
    end
    local input = vlc.input.item()
    local input_path = input:uri():gsub("file:///", "")

    -- Convert Windows path to WSL path
    input_path = input_path:gsub("^([A-Za-z]):/", function(drive)
        return "/mnt/" .. drive:lower() .. "/"
    end)
    filename = filename:gsub("^([A-Za-z]):/", function(drive)
        return "/mnt/" .. drive:lower() .. "/"
    end)

    -- we use wsl because my ffmpeg is in wsl
    local command = string.format('start cmd /c "wsl ffmpeg -i \"%s\" -ss %s -c copy \"%s\" ; echo \"Press anything to continue...\" ; read"', input_path, time, filename)
    vlc.msg.info("Command to execute: " .. command)
    os.execute(command)
    close()
end