local M = {}

function M.signature()
    require('lsp_signature').setup {
        bind = true, -- This is mandatory, otherwise border config doesn't work
        floating_window = true,
        fix_pos = true,
        hint_enable = false,
        hint_prefix = ' ',
        hint_scheme = 'String',
        hi_parameter = 'Visual',
        handler_opts = { border = vim.g.border_style },
        zindex = 50, -- set to 200 to make the float window on top of others
        toggle_key = '<C-k>',
    }
end

function M.fidget()
    require('fidget').setup {
        text = {
            spinner = 'dots',
            done = '✔',
            commenced = 'Started',
            completed = 'Done',
        },
        align = {
            bottom = true,
            right = true,
        },
        window = {
            relative = 'win',
            blend = 0,
            zindex = nil,
        },
        timer = {
            spinner_rate = 125,
            fidget_decay = 2000,
            task_decay = 1000,
        },
        fmt = {
            leftpad = true,
            fidget = function(fidget_name, spinner)
                return string.format('%s %s', spinner, fidget_name)
            end,
            task = function(task_name, message, percentage)
                return string.format(
                    '%s%s [%s]',
                    message,
                    percentage and string.format(' (%s%%)', percentage) or '',
                    task_name
                )
            end,
        },
    }
end

function M.lsp()
    require('user.modules.lsp.lspconfig')
end

function M.null_ls()
    require('user.modules.lsp.null_ls')
end

function M.trouble()
    require('trouble').setup {
        position = 'bottom',
        height = 10,
        icons = true,
        mode = 'workspace_diagnostics',
        fold_open = ' ',
        fold_closed = '',
        group = true,
        padding = true,
        action_keys = {
            open_split = { '<c-h>' },
            open_vsplit = { '<c-v>' },
            open_tab = { '<c-t>' },
        },
        auto_open = false,
        auto_close = true,
        signs = {
            error = '',
            warning = '',
            information = '',
            hint = '',
            other = '',
        },
        use_diagnostic_signs = false,
    }
end

function M.symbols_outline()
    vim.g.symbols_outline = {
        highlight_hovered_item = false,
        show_guides = true,
        auto_preview = true,
        position = 'right',
        border = vim.g.border_style,
        width = 30,
        show_numbers = false,
        show_relative_numbers = false,
        show_symbol_details = true,
        preview_bg_highlight = 'NormalFloat',
        keymaps = { -- Can be string or a table
            close = { '<Esc>', 'q' },
            goto_location = '<Cr>',
            focus_location = 'o',
            hover_symbol = '<C-space>',
            toggle_preview = 'K',
            rename_symbol = 'r',
            code_actions = 'a',
        },
    }
end

function M.dap()
    local dap = require('dap')
    vim.fn.sign_define('DapBreakpoint', { text = '', texthl = 'DapSignDefault' })
    vim.fn.sign_define('DapLogPoint', { text = '', texthl = 'DapSignDefault' })
    vim.fn.sign_define('DapStopped', { text = 'ﱢ', texthl = 'DapSignDefault' })
    vim.fn.sign_define('DapBreakpointRejected', { text = '', texthl = 'DapSignRejected' })

    require('user.keymap.plugmap').dap()
end

function M.dapui()
    local dap, dapui = require('dap'), require('dapui')
    dap.listeners.after.event_initialized['dapui_config'] = function()
        dapui.open()
    end
    dap.listeners.before.event_terminated['dapui_config'] = function()
        dapui.close()
    end
    dap.listeners.before.event_exited['dapui_config'] = function()
        dapui.close()
    end

    require('dapui').setup {
        icons = { expanded = '', collapsed = '' },
        mappings = {
            expand = { '<CR>', '<2-LeftMouse>' },
            open = 'o',
            remove = 'd',
            edit = 'e',
            repl = 'r',
        },
        sidebar = {
            elements = {
                -- Provide as ID strings or tables with 'id' and 'size' keys
                -- 'size' can be float or integer > 1
                { id = 'scopes', size = 0.25 },
                { id = 'breakpoints', size = 0.25 },
                { id = 'stacks', size = 0.25 },
                { id = 'watches', size = 0.25 },
            },
            size = 40,
            position = 'left',
        },
        tray = {
            elements = { 'repl' },
            size = 10,
            position = 'bottom',
        },
        floating = {
            max_height = nil, -- These can be integers or a float between 0 and 1.
            max_width = nil, -- Floats will be treated as percentage of your screen.
            border = vim.g.border_style,
            mappings = {
                close = { 'q', '<Esc>' },
            },
        },
        windows = { indent = 1 },
    }

    require('user.keymap.plugmap').dapui()
end

return M
