local ensure = {
  'php-debug-adapter',
  'netcoredbg',
}

return {
  {
    'mfussenegger/nvim-dap',
    init = function()
      for _, pkg in pairs(ensure) do
        local p = require('mason-registry').get_package(pkg)
        if not p:is_installed() then
          p:install()
        end
      end
    end,
    dependencies = {
      { 'rcarriga/nvim-dap-ui', dependencies = { 'nvim-neotest/nvim-nio' } },
      'williamboman/mason.nvim',
    },
    keys = {
      { '<F2>',       function() require('dap').step_over() end },
      { '<F3>',       function() require('dap').step_into() end },
      { '<F4>',       function() require('dap').step_out() end },
      { '<F5>',       function() require('dap').continue({}) end },
      { '<F10>',      function() require('dap').toggle_breakpoint() end },
      { '<leader>DD', function() require('dapui').toggle() end },
      { '<leader>DE', function()
        vim.ui.input({ prompt = 'DAP Evalulate expression: ' },
          function(input)
            require('dapui').eval(input)
          end)
      end },
    },
    opts = {
      cs = { -- {{{
        adapter = {
          type = 'executable',
          command = 'netcoredbg',
          args = { '--interpreter=vscode' },
        },
        confs = {
          {
            type = 'cs',
            name = 'launch - netcoredbg',
            request = 'launch',
            program = function()
              return vim.fn.input('Path to dll', vim.fn.getcwd() .. '/bin/Debug/', 'file')
            end,
          },
        },

      },      --}}}
      php = { -- {{{
        adapter = {
          type = 'executable',
          command = 'php-debug-adapter',
        },
        confs = {
          {
            type = 'php',
            request = 'launch',
            name = 'Listen for Xdebug',
            port = 9000,
          },
        },
      },             -- }}}
      dockerfile = { -- {{{
        adapter = {
          type = 'executable',
          command = 'buildg',
          args = { 'dap', 'serve' },
        },
        confs = {
          {
            type = 'dockerfile',
            name = 'Dockerfile configuration',
            request = 'launch',
            stopOnEntry = true,
            program = 'file',
          },
        },
      }, --- }}}
    },
    config = function(_plug, opts)
      -- signs - move to colorsheme? {{{
      vim.fn.sign_define('DapBreakpoint', {
        text = '',
        texthl = 'DiagnosticSignError',
        linehl = '',
        numhl = '',
      })
      vim.fn.sign_define('DapBreakpointCondition', {
        text = '',
        texthl = 'DiagnosticSignError',
        linehl = '',
        numhl = '',
      })
      vim.fn.sign_define('DapLogPoint', { text = '󱂅', texthl = '', linehl = '', numhl = '' })
      vim.fn.sign_define('DapStopped', { text = '', texthl = 'DiagnosticSignOk', linehl = '', numhl = '' })
      vim.fn.sign_define('DapBreakpointRejected', { text = '', texthl = 'DiagnosticSignError', linehl = '', numhl = '' })
      --- }}}

      local dap = require('dap')

      for lang, opt in pairs(opts) do
        dap.configurations[lang] = opt.confs
        dap.adapters[lang] = opt.adapter
      end

      local dapui = require('dapui')
      dapui.setup()

      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
    end,
  },
  {
    'leoluz/nvim-dap-go',
    ft = 'go',
    config = true,
  },
}
