-- Imports the plugin's additional Lua modules.
-- local fetch = require("example-plugin.fetch")
-- local update = require("example-plugin.update")

-- Creates an object for the module. All of the module's
-- functions are associated with this object, which is
-- returned when the module is called with `require`.
local M = {}

-- Routes calls made to this module to functions in the
-- plugin's other modules.
-- M.fetch_todos = fetch.fetch_todos
-- M.insert_todo = update.insert_todo
-- M.complete_todo = update.complete_todo

function M.setup()
    vim.api.nvim_create_user_command('CritiqueHelloWorld', M.hello_world, {})
    -- vim.api.nvim_create_user_command('CritiqueOpenTree', M.open_window, {})
end

local buf, win

function M.hello_world()
  vim.notify("HELLO WORLD!!!")
end

function M.open_window()
  buf = vim.api.nvim_create_buf(false, true) -- create new emtpy buffer

  vim.api.nvim_buf_set_option(buf, 'bufhidden', 'wipe')

  -- get dimensions
  local width = vim.api.nvim_get_option("columns")
  local height = vim.api.nvim_get_option("lines")

  -- calculate our floating window size
  local win_height = math.ceil(height * 0.8 - 4)
  local win_width = math.ceil(width * 0.8)

  -- and its starting position
  local row = math.ceil((height - win_height) / 2 - 1)
  local col = math.ceil((width - win_width) / 2)

  -- set some options
  local opts = {
    style = "minimal",
    relative = "editor",
    width = win_width,
    height = win_height,
    row = row,
    col = col
  }

  -- and finally create it with buffer attached
  win = vim.api.nvim_open_win(buf, true, opts)
end

local function update_view()
  -- we will use vim systemlist function which run shell
  -- command and return result as list
  local result = vim.fn.systemlist('hg xl')

  -- with small indentation results will look better
  for k,v in pairs(result) do
    result[k] = '  '..result[k]
  end

  api.nvim_buf_set_lines(buf, 0, -1, false, {
      center('Critique'),
      ''
  })

  api.nvim_buf_set_lines(buf, 0, -1, false, result)
end

local function center(str)
  local width = api.nvim_win_get_width(0)
  local shift = math.floor(width / 2) - math.floor(string.len(str) / 2)
  return string.rep(' ', shift) .. str
end

return M
