local u = require("functions.utils")
require("functions.note")

-- Globals
function _G.P(...)
	local objects = {}
	for i = 1, select("#", ...) do
		local v = select(i, ...)
		table.insert(objects, vim.inspect(v))
	end

	print(table.concat(objects, "\n"))
	return ...
end

-- These are functions that could theoretically be called by the user
-- although they are generally used by other commands (in the commands.lua file)
-- or are used by mappings (in the mappings directory)

return {
	capture = function(cmd, raw)
		local f = assert(io.popen(cmd, "r"))
		local s = assert(f:read("*a"))
		f:close()
		if raw then
			return s
		end
		s = string.gsub(s, "^%s+", "")
		s = string.gsub(s, "%s+$", "")
		s = string.gsub(s, "[\n\r]+", " ")
		return s
	end,
	start_replace = function()
		vim.cmd.startreplace()
	end,
	reload = function(package_name)
		local t = u.split(package_name, " ")
		for _, value in ipairs(t) do
			package.loaded[value] = nil
			local status = pcall(require, value)
			if not status then
				require("notify")(value .. " is not available.", "warn")
			end
		end
	end,
	run_script = function(script_name, args)
		local nvim_scripts_dir = "~/.config/nvim/scripts"
		local f = nil
		if args == nil then
			f = io.popen(string.format("/bin/bash %1s/%2s", nvim_scripts_dir, script_name))
		else
			f = io.popen(string.format("/bin/bash %1s/%2s %3s", nvim_scripts_dir, script_name, args))
		end
		local output = f:read("*a")
		f:close()
		return output
	end,
	stash = function(name)
		vim.fn.system("git stash -u -m " .. name)
	end,
	buf_only = function()
		local option = vim.api.nvim_buf_get_option
		local del_non_modifiable = vim.g.bufonly_delete_non_modifiable or false

		local cur = vim.api.nvim_get_current_buf()

		local deleted, modified = 0, 0

		for _, n in ipairs(vim.api.nvim_list_bufs()) do
			-- If the iter buffer is modified one, then don't do anything
			if option(n, "modified") then
				-- iter is not equal to current buffer
				-- iter is modifiable or del_non_modifiable == true
				-- `modifiable` check is needed as it will prevent closing file tree ie. NERD_tree
				modified = modified + 1
			elseif n ~= cur and (option(n, "modifiable") or del_non_modifiable) then
				vim.api.nvim_buf_delete(n, {})
				deleted = deleted + 1
			end
		end

		require("notify")("BufOnly: " .. deleted .. " deleted buffer(s), " .. modified .. " modified buffer(s)")
	end,
	screenshot = function()
		local async_job_ok, job = pcall(require, "plenary.job")
		if not async_job_ok then
			require("notify")("Plenary is not installed!", "error")
		end

		local language = vim.bo.filetype
		local line_number = vim.api.nvim_win_get_cursor(0)[1]
		local create_screenshot = job:new({
			command = "silicon",
			args = { "--from-clipboard", "-l", language, "--to-clipboard", "--line-offset", line_number },
			on_exit = function(_, exit_code)
				if exit_code ~= 0 then
					require("notify")("Could not create screenshot! Do you have silicon installed?", "error")
					return
				end
				require("notify")("Screenshot copied to clipboard", vim.log.levels.INFO)
			end,
		})

		create_screenshot:start()
	end,
}
