local M = {}

-- DAP: Debug Adapter Protocol >>>
M.nvim_dap = function()
   local dap = require('dap')
   -- keybinds
   vim.cmd [[
   " Debugger movement
   nnoremap <silent> <leader>d<space> <cmd>lua require'dap'.continue()<CR>
   nnoremap <silent> <leader>dj       <cmd>lua require'dap'.step_over()<CR>
   nnoremap <silent> <leader>dl       <cmd>lua require'dap'.step_into()<CR>
   nnoremap <silent> <leader>dh       <cmd>lua require'dap'.step_out()<CR>
   " Breakpoints
   nnoremap <silent> <leader>dbb      <cmd>lua require'dap'.toggle_breakpoint()<CR>
   nnoremap <silent> <leader>dbc      <cmd>lua require'dap'.set_breakpoint(vim.fn.input('Breakpoint condition<cmd> '))<CR>
   nnoremap <silent> <leader>dbl      <cmd>lua require'dap'.set_breakpoint(nil, nil, vim.fn.input('Log point message<cmd> '))<CR>
   " Extra
   nnoremap <silent> <leader>dp       <cmd>lua require'dap'.run_last()<CR>
   nnoremap <silent> <leader>dr       <cmd>lua require'dap'.repl.open()<CR>
   ]]
   require("dap")
   vim.fn.sign_define('DapBreakpoint',         {text = '綠', texthl = 'ErrorMsg', linehl = '', numhl = ''}) -- custom attributes of breakpointed lines (use an emoji?)
   vim.fn.sign_define('DapBreakpointRejected', {text = '祿', texthl = 'ErrorMsg', linehl = '', numhl = ''})
   vim.fn.sign_define('DapLogPoint',           {text = ' ', texthl = 'ErrorMsg', linehl = '', numhl = ''})
   vim.fn.sign_define('DapStopped',            {text = '→ ', texthl = 'String',   linehl = '', numhl = ''})
   vim.cmd "au FileType dap-repl lua require('dap.ext.autocompl').attach()"
   -- dap configuration
   dap.defaults.fallback.terminal_win_cmd = "10new"
   -- per-language config:
   -- c++ dap congiguration >>>
   dap.configurations.cpp = {
      {
         name = "Launch file",
         type = "cppdbg",
         request = "launch",
         program = function()
            return vim.fn.input('Path to executable: ', vim.fn.getcwd() .. '/', 'file')
         end,
         cwd = '${workspaceFolder}',
         stopOnEntry = true,
      },
   }
   dap.adapters.cppdbg = {
      type = 'executable',
      command = '/home/blake/.local/share/nvim/dapinstall/ccppr_vsc/extension/debugAdapters/bin/OpenDebugAD7',
   }
   -- <<<
end -- <<<

-- DAP installer >>>
M.dap_install = function()
   require("dap-install").setup({
      installation_path = vim.fn.stdpath("data") .. "/dapinstall/",
   })
end -- <<<

-- UI for nvim-dap >>>
M.dap_ui = function()
   -- external keybinds
   -- require("dapui").open()
   -- require("dapui").close()
   -- require("dapui").toggle()
   -- toggle dap-ui with leader di as in dap interface
   vim.cmd 'nnoremap <silent> <leader>di <cmd>lua require("dapui").toggle("sidebar")<CR>'
   require("dapui").setup({
      icons = { expanded = "▾", collapsed = "▸" },
      mappings = {
         expand = { "l", "h", "<CR>", "<2-LeftMouse>", }, -- expand (or close)
         open = "o", -- action depends on the dap server
         remove = "d", -- delete the variable
         edit = "s", -- (as in substitute)
         repl = "r", -- open a repl
      },
      sidebar = {
         -- You can change the order of elements in the sidebar
         elements = {
            -- size can be float or integer > 1
            { id = "scopes", size = 0.85, },
            { id = "breakpoints", size = 0.15 },
            -- { id = "stacks", size = 0.15 },
            -- { id = "watches", size = 0.15 },
         },
         size = 40,
         position = "left", -- Can be "left", "right", "top", "bottom"
      },
      tray = {
         elements = { "repl" },
         size = 0,
         position = "top", -- Can be "left", "right", "top", "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.
         mappings = {
            close = { "q", "<Esc>" },
         },
      },
      windows = { indent = 1 },
   })
end -- <<<

-- dap-virtual-text: show values of variables inline while debugging >>>
M.dap_vtext = function()
   require'nvim-dap-virtual-text'
   -- virtual text deactivated (default)
   vim.g.dap_virtual_text = true
   -- show virtual text for current frame (recommended)
   vim.g.dap_virtual_text = true
   -- request variable values for all frames (experimental)
   vim.g.dap_virtual_text = 'all frames'
end -- <<<

return M

-- vim:fdm=marker:fmr=>>>,<<<:expandtab:tabstop=3:sw=3