local M = {}

function M.config()
	local status_ok, lualine = pcall(require, "lualine")
	if not status_ok then
		return
	end

	local scolors = {
		fg = '#adb0b9',
		bg = '#252526',
		inset = '#569cd6',

		filetype = '#6a9955',
		filename = '#91bde1',

		added = '#6a9955',
		modified = '#db812e',
		removed = '#f6635a',

		error = '#f53226',
		warn = '#eed94e',
		info = '#569cd6',

		branch = '#dcdcaa',

		filetype = '#c5a6c0',

		treesitter = '#6a9955',

		scroll = '#dcdcaa',
	}

	local conditions = {
		buffer_not_empty = function()
			return vim.fn.empty(vim.fn.expand "%:t") ~= 1
		end,
		hide_in_width = function()
			return vim.fn.winwidth(0) > 80
		end,
		check_git_workspace = function()
			local filepath = vim.fn.expand "%:p:h"
			local gitdir = vim.fn.finddir(".git", filepath .. ";")
			return gitdir and #gitdir > 0 and #gitdir < #filepath
		end,
	}

	local config = {
		options = {
			disabled_filetypes = { "NvimTree", "dashboard", "Outline" },
			component_separators = "",
			section_separators = "",
			theme = {
				normal = { c = { fg = scolors.fg, bg = scolors.bg } },
				inactive = { c = { fg = scolors.fg, bg = scolors.bg } },
			},
		},
		sections = {
			lualine_a = {},
			lualine_b = {},
			lualine_y = {},
			lualine_z = {},
			lualine_c = {},
			lualine_x = {},
		},
		inactive_sections = {
			lualine_a = {},
			lualine_b = {},
			lualine_y = {},
			lualine_z = {},
			lualine_c = {},
			lualine_x = {},
		},
	}

	local function ins_left(component)
		table.insert(config.sections.lualine_c, component)
	end

	local function ins_right(component)
		table.insert(config.sections.lualine_x, component)
	end

	ins_left {
		function()
			return "▊"
		end,
		color = { fg = scolors.inset },
		padding = { left = 0, right = 0 },
	}

	ins_left {
		"branch",
		icon = "",
		color = { fg = scolors.branch, gui = "bold" },
		padding = { left = 2, right = 1 },
	}

	ins_left {
		"filetype",
		colored = false,
		color = { fg = scolors.filetype, gui = "bold" },
		cond = conditions.buffer_not_empty,
		padding = { left = 1, right = 1 },
	}

	ins_left {
		"filename",
		color = { fg = scolors.filename, gui = "bold" },
		padding = { left = 1, right = 1 },
	}

	ins_left {
		"diff",
		symbols = { added = " ", modified = "柳", removed = " " },
		diff_color = {
			added = { fg = scolors.added },
			modified = { fg = scolors.modified },
			removed = { fg = scolors.removed },
		},
		cond = conditions.hide_in_width,
		padding = { left = 1, right = 1 },
	}

	ins_left {
		"diagnostics",
		sources = { "nvim_diagnostic" },
		symbols = { error = " ", warn = " ", info = " ", hint = " " },
		diagnostics_color = {
			color_error = { fg = scolors.error },
			color_warn = { fg = scolors.warn },
			color_info = { fg = scolors.info },
		},
		padding = { left = 2, right = 1 },
	}

	ins_left {
		function()
			return "%="
		end,
	}

	ins_right {
		function(msg)
			msg = msg or "Inactive"
			local buf_clients = vim.lsp.buf_get_clients()
			if next(buf_clients) == nil then
				if type(msg) == "boolean" or #msg == 0 then
					return "Inactive"
				end
				return msg
			end
			local buf_ft = vim.bo.filetype
			local buf_client_names = {}

			for _, client in pairs(buf_clients) do
				if client.name ~= "null-ls" then
					table.insert(buf_client_names, client.name)
				end
			end

			local formatters = require "core.utils"
			local supported_formatters = formatters.list_registered_formatters(buf_ft)
			vim.list_extend(buf_client_names, supported_formatters)

			local linters = require "core.utils"
			local supported_linters = linters.list_registered_linters(buf_ft)
			vim.list_extend(buf_client_names, supported_linters)

			return table.concat(buf_client_names, ", ")
		end,
		icon = " ",
		color = { gui = "none" },
		padding = { left = 1, right = 0 },
		cond = conditions.hide_in_width,
	}

	ins_right {
		function()
			local b = vim.api.nvim_get_current_buf()
			if next(vim.treesitter.highlighter.active[b]) then
				return " 綠S"
			end
			return ""
		end,
		color = { fg = scolors.treesitter },
		padding = { left = 1, right = 0 },
		cond = conditions.hide_in_width,
	}

	ins_right {
		"location",
		padding = { left = 1, right = 1 },
	}

	ins_right {
		"progress",
		color = { gui = "none" },
		padding = { left = 0, right = 0 },
	}

	ins_right {
		function()
			local current_line = vim.fn.line "."
			local total_lines = vim.fn.line "$"
			local chars = { "__", "▁▁", "▂▂", "▃▃", "▄▄", "▅▅", "▆▆", "▇▇", "██" }
			local line_ratio = current_line / total_lines
			local index = math.ceil(line_ratio * #chars)
			return chars[index]
		end,
		padding = { left = 1, right = 1 },
		color = { fg = scolors.scroll },
		cond = nil,
	}

	ins_right {
		function()
			return "▊"
		end,
		color = { fg = scolors.inset },
		padding = { left = 1, right = 0 },
	}

	lualine.setup(config)
end

return M