local name, ns = ...
local debug = ns.debug
local Addon = _G[name]

local L = LibStub("AceLocale-3.0"):GetLocale(name)

local templates = {}
local templateValues = {}

local templateFuncs = {
	getValue = function(code, unit)
			local code = Addon:GetTemplateValue(code)
			return Addon:GetText(code, unit)
		end,
}
Addon:RegisterProvider("template", L["Template"], templateFuncs, {
		value = {
			type = 'select',
			name = L["Template"],
			desc = L["Template of code"],
			get = function(info)
					return info.arg.value
				end,
			set = function(info, value)
					info.arg.value = value
				end,
			values = function() return Addon:GetTemplates() end,
			order = 10,
		},
		code = {
			name = L["Code"],
			desc = L["Code the template uses"],
			type = 'input',
			multiline = true,
			width = "full",
			order = 11,
			get = function(info)
					return Addon:GetTemplateValue(info.arg.value)
				end,
			set = function() end,
		},
})


function Addon:GetTemplateValue(key)
	if not key then return "" end
	return templateValues[key]
end

function Addon:GetTemplates()
	return templates
end

function Addon:RegisterTemplate(key, localname, value)
	templates[key] = localname
	templateValues[key] = value
end

function Addon:GetLocalTemplateName(key)
	return localnames[key]
end

Addon:RegisterTemplate("None", NONE, "")
Addon:RegisterTemplate("Faction", L["Faction"], [[return Faction() or '']])
Addon:RegisterTemplate("NameWithTitle", L["Name with title"], [[local name = NameFull(unit)
if not name then
  return UNKNOWN
end
local _, class = UnitClass(unit)
local color = "%1"
if UnitIsPlayer(unit) then
  color = Colorize("%1", ClassColor(unit))
else
  color = Colorize("%1", HostileColor(unit))
end
name = name:gsub(Name(unit), color)
return name
]])
Addon:RegisterTemplate("GuildWithRank", L["Guild with rank"], [[local guild, rankname, rankindex = GetGuildInfo(unit)
if not guild then
  guild = NPCGuild()
  if not guild then return '' end
  return "<%s>", guild
end
local pguild, _, prankindex = GetGuildInfo("player")
if pguild == guild then
  guild = ("|cff00cc00<%s>|r"):format(guild)
  local rankcolor
  if rankindex > prankindex then
    rankcolor = "|cffff0000%s|r"
  elseif rankindex < prankindex then
    rankcolor = "|cff00ff00%s|r"
  else
    rankcolor = "|cffffff00%s|r"
  end
  rankname = rankcolor:format(rankname)
else
  guild = ("<%s>"):format(guild)
end
return "%s (%d) of %s", rankname, rankindex, guild
]])
Addon:RegisterTemplate("LevelClassRace", L["Level Class Race"], [[local level, race = Level(unit), SmartRace(unit)
local class = ""
if UnitIsPlayer(unit) then
  class = Colorize(Class(unit), ClassColor(unit))
else
  class = ""
end
level = Colorize(level, DifficultyColor(unit))
local diff = ShortClassification(unit)
local icon = GenderIcon(unit)
if icon then
  return "Level %s%s %s %s |T%s:12|t", level, diff, race, class, icon
else
  return "Level %s%s %s %s", level, diff, race, class
end
]])
Addon:RegisterTemplate("TalentsWithIcon", L["Talents with icon"], [[
local icon = TalentSpecIcon(unit)
local name = TalentSpecName(unit) or ''
if icon then
  return '|T%s:12|t %s', icon, name
else
  return name
end
]])

Addon:RegisterTemplate("Target", L["Target"], [[local name = Name(unit .. "target")
if not name then
  return NONE
elseif name == UnitName("player") then
  return Colorize(("<<%s>>"):format(YOU), 0, 0, 255)
else
  return Colorize(name, SmartClassColor(unit .. "target"))
end
]])
Addon:RegisterTemplate("Status", L["Status"], [[return Offline(unit) or AFK(unit) or DND(unit) or Status(unit) or '']])
Addon:RegisterTemplate("Zone", L["Zone"], "return Zone() or ''")
Addon:RegisterTemplate("TalentDetail", L["Talent Detail"], "return TalentSpec(unit) or ''")
