local M = {}

local config = require('keytrail.config')

-- Create namespace for virtual text
local ns = vim.api.nvim_create_namespace('keytrail')

-- Track the current popup window and buffer
local current_popup = nil
local current_buf = nil

---Close the current popup if it exists
function M.close()
    if current_popup and vim.api.nvim_win_is_valid(current_popup) then
        vim.api.nvim_win_close(current_popup, true)
    end
    if current_buf and vim.api.nvim_buf_is_valid(current_buf) then
        vim.api.nvim_buf_delete(current_buf, { force = true })
    end
    current_popup = nil
    current_buf = nil
end

---Create a new popup window
---@param path_width number The width needed for the path text
---@return number, number The buffer and window IDs
function M.create(path_width)
    -- Create a new buffer
    local buf = vim.api.nvim_create_buf(false, true)
    vim.api.nvim_buf_set_option(buf, 'modifiable', false)
    vim.api.nvim_buf_set_option(buf, 'modified', false)
    vim.api.nvim_buf_set_option(buf, 'buftype', 'nofile')
    vim.api.nvim_buf_set_option(buf, 'bufhidden', 'wipe')
    vim.api.nvim_buf_set_option(buf, 'swapfile', false)

    -- Get window dimensions
    local win_width = vim.api.nvim_win_get_width(0)
    local win_height = vim.api.nvim_win_get_height(0)

    -- Calculate popup position and size
    local row = config.get().position == "bottom" and win_height - 1 or 0
    local popup_width = math.min(path_width, win_width)
    local col = win_width - popup_width

    -- Create the popup window
    local popup = vim.api.nvim_open_win(buf, false, {
        relative = 'win',
        row = row,
        col = col,
        width = popup_width,
        height = 1,
        style = 'minimal',
        border = 'none',
        noautocmd = true,
        focusable = false,
        zindex = config.get().zindex,
    })

    -- Set popup window options
    vim.api.nvim_win_set_option(popup, 'winblend', 0)
    vim.api.nvim_win_set_option(popup, 'cursorline', false)
    vim.api.nvim_win_set_option(popup, 'cursorcolumn', false)
    vim.api.nvim_win_set_option(popup, 'number', false)
    vim.api.nvim_win_set_option(popup, 'relativenumber', false)
    vim.api.nvim_win_set_option(popup, 'signcolumn', 'no')
    vim.api.nvim_win_set_option(popup, 'foldcolumn', '0')
    vim.api.nvim_win_set_option(popup, 'list', false)
    vim.api.nvim_win_set_option(popup, 'wrap', false)
    vim.api.nvim_win_set_option(popup, 'linebreak', false)
    vim.api.nvim_win_set_option(popup, 'scrolloff', 0)
    vim.api.nvim_win_set_option(popup, 'sidescrolloff', 0)

    -- Set the window highlight to transparent
    vim.api.nvim_win_set_hl_ns(popup, ns)
    vim.api.nvim_win_set_option(popup, 'winhighlight', 'Normal:KeyTrailPopup')

    return buf, popup
end

---Show the popup with the given path
---@param path string The path to display
function M.show(path)
    -- Always close existing popup first
    M.close()

    if path == "" then
        return
    end

    -- Split path into segments and create colored text
    local segments = vim.split(path, config.get().delimiter, { plain = true })
    local colored_text = {}
    local total_width = 0

    for i, segment in ipairs(segments) do
        local color_idx = ((i - 1) % #config.get().colors) + 1
        -- Handle array indices with blue brackets
        if segment:match("^%[.*%]$") then
            local index = segment:match("%[(%d+)%]")
            table.insert(colored_text, { "[", "KeyTrailBracket" })
            table.insert(colored_text, { index, "YAMLPathline" .. color_idx })
            table.insert(colored_text, { "]", "KeyTrailBracket" })
            total_width = total_width + #segment
        else
            table.insert(colored_text, { segment, "YAMLPathline" .. color_idx })
            total_width = total_width + #segment
        end
        -- Add delimiter if not the last segment and next segment is not an array index
        if i < #segments and not segments[i + 1]:match("^%[.*%]$") then
            table.insert(colored_text, { config.get().delimiter, "KeyTrailDelimiter" })
            total_width = total_width + #config.get().delimiter
        end
    end

    -- Add some padding
    total_width = total_width + 2

    -- Create new popup with calculated width
    current_buf, current_popup = M.create(total_width)

    -- Add virtual text with colors, but use overlay positioning instead of right_align
    vim.api.nvim_buf_set_extmark(current_buf, ns, 0, 0, {
        virt_text = colored_text,
        virt_text_pos = "overlay",
    })
end

return M
