-- ---------------------------------------------------------------------
-- COLOR VALUES
-- ---------------------------------------------------------------------
-- Download palettes and files from: http://ethanschoonover.com/solarized
--
-- L\*a\*b values are canonical (White D65, Reference D50), other values are
-- matched in sRGB space.
--
-- SOLARIZED HEX     16/8 TERMCOL  XTERM/HEX   L*A*B      sRGB        HSB
-- --------- ------- ---- -------  ----------- ---------- ----------- -----------
-- base03    #002b36  8/4 brblack  234 #1c1c1c 15 -12 -12   0  43  54 193 100  21
-- base02    #073642  0/4 black    235 #262626 20 -12 -12   7  54  66 192  90  26
-- base01    #586e75 10/7 brgreen  240 #4e4e4e 45 -07 -07  88 110 117 194  25  46
-- base00    #657b83 11/7 bryellow 241 #585858 50 -07 -07 101 123 131 195  23  51
-- base0     #839496 12/6 brblue   244 #808080 60 -06 -03 131 148 150 186  13  59
-- base1     #93a1a1 14/4 brcyan   245 #8a8a8a 65 -05 -02 147 161 161 180   9  63
-- base2     #eee8d5  7/7 white    254 #d7d7af 92 -00  10 238 232 213  44  11  93
-- base3     #fdf6e3 15/7 brwhite  230 #ffffd7 97  00  10 253 246 227  44  10  99
-- yellow    #b58900  3/3 yellow   136 #af8700 60  10  65 181 137   0  45 100  71
-- orange    #cb4b16  9/3 brred    166 #d75f00 50  50  55 203  75  22  18  89  80
-- red       #dc322f  1/1 red      160 #d70000 50  65  45 220  50  47   1  79  86
-- magenta   #d33682  5/5 magenta  125 #af005f 50  65 -05 211  54 130 331  74  83
-- violet    #6c71c4 13/5 brmagenta 61 #5f5faf 50  15 -45 108 113 196 237  45  77
-- blue      #268bd2  4/4 blue      33 #0087ff 55 -10 -45  38 139 210 205  82  82
-- cyan      #2aa198  6/6 cyan      37 #00afaf 60 -35 -05  42 161 152 175  74  63
-- green     #859900  2/2 green     64 #5f8700 60 -20  65 133 153   0  68 100  60
--
-- ---------------------------------------------------------------------
-- COLORSCHEME HACKING
-- ---------------------------------------------------------------------
--
-- Useful commands for testing colorschemes:
-- :source $VIMRUNTIME/syntax/hitest.vim
-- :help highlight-groups
-- :help cterm-colors
-- :help group-name
--
-- Useful links for developing colorschemes:
-- http://www.vim.org/scripts/script.php?script_id=2937
-- http://vimcasts.org/episodes/creating-colorschemes-for-vim/
-- http://www.frexx.de/xterm-256-notes/"

local table = require('table')

function highlight(opts)
	assert(opts.name, "The name of the group is missing")

	local histr = "highlight! " .. opts.name .. " "

	if opts.fg then
		histr = histr .. "ctermfg=" .. opts.fg[1] .. " "
		histr = histr .. "guifg=" .. opts.fg[2] .. " "
	end

	if opts.bg then
		histr = histr .. "ctermbg=" .. opts.bg[1] .. " "
		histr = histr .. "guibg=" .. opts.bg[2] .. " "
	end

	if opts.attrs then
		histr = histr .. "cterm=" .. table.concat(opts.attrs, ",") .. " "
		histr = histr .. "gui=" .. table.concat(opts.attrs, ",") .. " "
	end

	if opts.sp then
		histr = histr .. "guisp=" .. opts.sp[2] .. " "
	end

	if opts.link then
		histr = "highlight! link " .. opts.name .. " " .. opts.link
	end

	if opts.clear then
		histr = "highlight! clear " .. opts.name
	end

	vim.cmd(histr)
end

local config = {
    termtrans = false
}

local colors = {
    base03      = { "8",    "#002b36" },
    base02      = { "0",    "#073642" },
    base01      = { "10",   "#586e75" },
    base00      = { "11",   "#657b83" },
    base0       = { "12",   "#839496" },
    base1       = { "14",   "#93a1a1" },
    base2       = { "7",    "#eee8d5" },
    base3       = { "15",   "#fdf6e3" },
    yellow      = { "3",    "#859900" },
    orange      = { "9",    "#b58900" },
    red         = { "1",    "#cb4b16" },
    magenta     = { "5",    "#dc322f" },
    violet      = { "13",   "#d33682" },
    blue        = { "4",    "#6c71c4" },
    cyan        = { "6",    "#268bd2" },
    green       = { "2",    "#2aa198" },
    none        = { "NONE", "NONE"    },
    back        = { "8",    "#002b36" },
}

if config.termtrans then
	colors.back = colors.none
end

local attrs = {
    none        = "NONE",
    bbold       = "",
    bold        = "bold",
    underline   = "underline",
    italic      = "italic",
    curly       = "undercurl",
    reverse     = "reverse",
    standout    = "standout",
    strike      = "strikethrough",
    ou          = "",
    ob          = "",
}

vim.cmd [[
    highlight clear
    if exists("syntax_on")
      syntax reset
    endif
]]

vim.g.colors_name = "bsolarized2"

-- ----------------------------------------------------------------------
-- Builtin groups
-- ----------------------------------------------------------------------

-- ColorColumn	used for the columns set with 'colorcolumn'
highlight {name="ColorColumn", bg=colors.base02}

-- Conceal		placeholder characters substituted for concealed text (see
-- 'conceallevel')
highlight {name="Conceal", fg=colors.blue}

-- Cursor		character under the cursor
highlight {name="Cursor", fg=colors.base03, bg=colors.base01}

-- lCursor		the character under the cursor when |language-mapping| is
-- used (see 'guicursor')
highlight {name="lcursor", link="Cursor"}

-- CursorIM	like Cursor, but used when in IME mode |CursorIM|
highlight {name="CursorIM", link="Cursor"}

-- CursorColumn	Screen-column at the cursor, when 'cursorcolumn' is set.
highlight {name="CursorColumn", bg=colors.base02}

-- CursorLine	Screen-line at the cursor, when 'cursorline' is set.
-- Low-priority if foreground (ctermfg OR guifg) is not set.
highlight {name="CursorLine", bg=colors.base02, attrs={attrs.none}}

-- Directory	directory names (and other special names in listings)
highlight {name="Directory", fg=colors.blue}

-- DiffAdd		diff mode: Added line |diff.txt|
-- DiffChange	diff mode: Changed line |diff.txt|
-- DiffDelete	diff mode: Deleted line |diff.txt|
-- DiffText	diff mode: Changed text within a changed line |diff.txt|
highlight {name="DiffAdd", fg=colors.green, bg=colors.base02, attrs={attrs.bold}}
highlight {name="DiffChange", fg=colors.yellow, bg=colors.base02, attrs={attrs.bold}}
highlight {name="DiffDelete", fg=colors.red, bg=colors.base02, attrs={attrs.bold}}
highlight {name="DiffText", fg=colors.blue, bg=colors.base02, attrs={attrs.bold}}

-- EndOfBuffer	filler lines (~) after the end of the buffer. By default,
-- this is highlighted like |hl-NonText|.
--KEEP DEFAULT

-- TermCursor	cursor in a focused terminal
--KEEP DEFAULT

-- TermCursorNC	cursor in an unfocused terminal
--KEEP DEFAULT

-- ErrorMsg	error messages on the command line
highlight {name="ErrorMsg", fg=colors.base03, bg=colors.red, attrs={attrs.bold}}

-- WinSeparator	separators between window splits
highlight {name="WinSeparator", link="Normal"}

-- Folded		line used for closed folds
highlight {name="Folded", fg=colors.base0, bg=colors.base02, sp=colors.base03, attrs={attrs.underline, attrs.bold}}

-- FoldColumn	'foldcolumn'
highlight {name="FoldColumn", fg=colors.base0, bg=colors.base02}

-- SignColumn	column where |signs| are displayed
--highlight {name="SignColumn", fg=colors.base0, attrs={attrs.none}}
highlight {name="SignColumn", link="LineNr"}

-- IncSearch	'incsearch' highlighting; also used for the text replaced
-- with ":s///c"
highlight {name="IncSearch", link="Search"}

-- Substitute	|:substitute| replacement text highlighting
highlight {name="Substitute", link="Search"}

-- LineNr		Line number for ":number" and ":#" commands, and when 'number' or 'relativenumber' option is set.
highlight {name="LineNr", fg=colors.base01, bg=colors.base02, attrs={attrs.none}}

-- LineNrAbove	Line number for when the 'relativenumber' option is set,
-- above the cursor line.
--KEEP DEFAULT

-- LineNrBelow	Line number for when the 'relativenumber' option is set,
-- below the cursor line.
--KEEP DEFAULT

-- CursorLineNr	Like LineNr when 'cursorline' is set and 'cursorlineopt'
-- contains "number" or is "both", for the cursor line.
highlight {name="CursorLineNr", fg=colors.base0, bg=colors.back, attrs={attrs.bold}}

-- MatchParen	The character under the cursor or just before it, if it is
-- a paired bracket, and its match. |pi_paren.txt|
highlight {name="MatchParen", fg=colors.red, bg=colors.base00, attrs={attrs.bold}}

-- ModeMsg		'showmode' message (e.g., "-- INSERT --")
highlight {name="ModeMsg", fg=colors.blue}

-- MsgArea		Area for messages and cmdline
--KEEP DEFAULT

-- MsgSeparator	Separator for scrolled messages, `msgsep` flag of 'display'
--KEEP DEFAULT

-- MoreMsg		|more-prompt|
highlight {name="MoreMsg", fg=colors.blue}

-- NonText		'@' at the end of the window, characters from 'showbreak'
-- and other characters that do not really exist in the text (e.g., ">"
-- displayed when a double-wide character doesn't fit at the end of the
-- line). See also |hl-EndOfBuffer|.
highlight {name="NonText", fg=colors.base01, attrs={attrs.none}}

-- Normal		normal text
highlight {name="Normal", fg=colors.base0, bg=colors.back, attr={attrs.none}}

-- NormalFloat	Normal text in floating windows.
highlight {name="NormalFloat", link="Normal"}

-- NormalNC	normal text in non-current windows
--KEEP DEFAULT : cleared

-- Pmenu		Popup menu: normal item.
highlight {name="Pmenu", fg=colors.base0, bg=colors.base02, attrs={attrs.reverse}}

-- PmenuSel	Popup menu: selected item.
highlight {name="PmenuSel", fg=colors.base01, bg=colors.base2, attrs={attrs.reverse}}

-- PmenuSbar	Popup menu: scrollbar.
highlight {name="PmenuSbar", fg=colors.base2, bg=colors.base0, attrs={attrs.reverse}}

-- PmenuThumb	Popup menu: Thumb of the scrollbar.
highlight {name="PmenuThumb", fg=colors.base0, bg=colors.base03, attrs={attrs.reverse}}

-- Question	|hit-enter| prompt and yes/no questions
highlight {name="Question", fg=colors.cyan, bg=colors.none, attrs={attrs.bold}}

-- QuickFixLine	Current |quickfix| item in the quickfix window. Combined
-- with |hl-CursorLine| when the cursor is there.
--KEEP DEFAULT : links to Search

-- Search		Last search pattern highlighting (see 'hlsearch'). Also
-- used for similar items that need to stand out.
highlight {name="Search", bg=colors.yellow}

-- SpecialKey	Unprintable characters: text displayed differently from
-- what it really is. But not 'listchars' whitespace. |hl-Whitespace|
highlight {name="SpecialKey", fg=colors.base00, bg=colors.base02, attrs={attrs.bold}}

-- SpellBad	Word that is not recognized by the spellchecker. |spell|
-- Combined with the highlighting used otherwise.
-- SpellCap	Word that should start with a capital. |spell| Combined with
-- the highlighting used otherwise.
-- SpellLocal	Word that is recognized by the spellchecker as one that is
-- used in another region. |spell| Combined with the highlighting used
-- otherwise.
-- SpellRare	Word that is recognized by the spellchecker as one that is
-- hardly ever used. |spell| Combined with the highlighting used otherwise.
highlight {name="SpellBad", bg=colors.none, attrs={attrs.curly}, sp=colors.red}
highlight {name="SpellCap", bg=colors.none, attrs={attrs.curly}, sp=colors.violet}
highlight {name="SpellRare", bg=colors.none, attrs={attrs.curly}, sp=colors.cyan}
highlight {name="SpellLocal", bg=colors.none, attrs={attrs.curly}, sp=colors.yellow}

-- StatusLine	status line of current window
-- StatusLineNC	status lines of not-current windows Note: if this is equal
-- to "StatusLine" Vim will use "^^^" in the status line of the current
-- window.
highlight {name="StatusLine", fg=colors.base1, bg=colors.base02, attrs={attrs.reverse}}
highlight {name="StatusLineNC", fg=colors.base00, bg=colors.base02, attrs={attrs.reverse}}

-- TabLine		tab pages line, not active tab page label
-- TabLineFill	tab pages line, where there are no labels
-- TabLineSel	tab pages line, active tab page label
highlight {name="TabLine", fg=colors.base0, bg=colors.base02, attrs={attrs.underline}, sp=colors.base0}
highlight {name="TabLineFill", fg=colors.base0, bg=colors.base02, attrs={attrs.underline}, sp=colors.base0}
highlight {name="TabLineSel", fg=colors.base01, bg=colors.base2, attrs={attrs.underline, attrs.reverse}, sp=colors.base0}

-- Title		titles for output from ":set all", ":autocmd" etc.
highlight {name="Title", fg=colors.orange, attrs={attrs.bold}}

-- Visual		Visual mode selection
-- VisualNOS	Visual mode selection when vim is "Not Owning the
-- Selection".
highlight {name="Visual", fg=colors.base01, bg=colors.base03, attrs={attrs.reverse}}
highlight {name="VisualNOS", fg=colors.none, bg=colors.base03, attrs={attrs.reverse, attrs.standout}}

-- WarningMsg	warning messages
highlight {name="WarningMsg", fg=colors.red, bg=colors.none, attrs={attrs.bold}}

-- Whitespace	"nbsp", "space", "tab" and "trail" in 'listchars'
-- KEEP DEFAULT : links to NonText

-- WildMenu	current match in 'wildmenu' completion
highlight {name="WildMenu", fg=colors.base2, bg=colors.base02, attrs={attrs.reverse}}

-- ----------------------------------------------------------------------
-- Syntax groups
-- ----------------------------------------------------------------------

-- *Comment	any comment
highlight {name="Comment", fg=colors.base01, attrs={attrs.italic}}

-- *Constant	any constant
--  String		a string constant: "this is a string"
--  Character	a character constant: 'c', '\n'
--  Number		a number constant: 234, 0xff
--  Boolean	a boolean constant: TRUE, false
--  Float		a floating point constant: 2.3e10
highlight {name="Constant", fg=colors.cyan}

-- *Identifier	any variable name
--  Function	function name (also: methods for classes)
highlight {name="Identifier", fg=colors.blue}

-- *Statement	any statement
--  Conditional	if, then, else, endif, switch, etc.
--  Repeat		for, do, while, etc.
--  Label		case, default, etc.
--  Operator	"sizeof", "+", "*", etc.
--  Keyword	any other keyword
--  Exception	try, catch, throw
highlight {name="Statement", fg=colors.green}

-- *PreProc	generic Preprocessor
--  Include	preprocessor #include
--  Define		preprocessor #define
--  Macro		same as Define
--  PreCondit	preprocessor #if, #else, #endif, etc.
highlight {name="PreProc", fg=colors.orange}

-- *Type		int, long, char, etc.
--  StorageClass	static, register, volatile, etc.
--  Structure	struct, union, enum, etc.
--  Typedef	A typedef
highlight {name="Type", fg=colors.yellow}

-- *Special	any special symbol
--  SpecialChar	special character in a constant
--  Tag		you can use CTRL-] on this
--  Delimiter	character that needs attention
--  SpecialComment	special things inside a comment
--  Debug		debugging statements
highlight {name="Special", fg=colors.red}

-- *Underlined	text that stands out, HTML links
highlight {name="Underlined", fg=colors.violet}

-- *Ignore		left blank, hidden  |hl-Ignore|
highlight {name="Ignore", fg=colors.none, bg=colors.none, attrs={attrs.none}}

-- *Error		any erroneous construct
highlight {name="Error", fg=colors.base03, bg=colors.red,  attrs={attrs.bold}}

-- *Todo		anything that needs extra attention; mostly the keywords
-- TODO FIXME and XXX
highlight {name="Todo", fg=colors.magenta, bg=colors.none, attrs={attrs.bold}}


-- ----------------------------------------------------------------------
-- NvimTree syntax
-- ----------------------------------------------------------------------

highlight {name="NvimTreeSymlink", fg=colors.violet}
highlight {name="NvimTreeFolderName", link="Directory"}
highlight {name="NvimTreeRootFolder", link="Directory"}
highlight {name="NvimTreeFolderIcon", link="Directory"}
highlight {name="NvimTreeEmptyFolderName", link="Directory"}
highlight {name="NvimTreeOpenedFolderName", link="Directory"}
highlight {name="NvimTreeExecFile", fg=colors.blue}
highlight {name="NvimTreeOpenedFile", attrs={attrs.bold}}
highlight {name="NvimTreeSpecialFile", fg=colors.magenta}
highlight {name="NvimTreeImageFile", fg=colors.cyan}
highlight {name="NvimTreeIndentMarker", fg=colors.base01}

highlight {name="NvimTreeGitDirty", fg=colors.yellow, attrs={attrs.bold}}
highlight {name="NvimTreeGitStaged", fg=colors.green, attrs={attrs.bold}}
highlight {name="NvimTreeGitMerge", fg=colors.orange, attrs={attrs.bold}}
highlight {name="NvimTreeGitRenamed", fg=colors.green, attrs={attrs.bold}}
highlight {name="NvimTreeGitNew", fg=colors.green, attrs={attrs.bold}}
highlight {name="NvimTreeGitDeleted", fg=colors.red, attrs={attrs.strike}}

--highlight {name="NvimTreeWindowPicker", link=""}
