--[[----------------------------------------------------------
	Azurah - Interface Enhanced
	----------------------------------------------------------
	* An addon designed to allow for user customization of the
	* stock interface with optional components to provide
	* additional information while maintaining the stock feel.
	*
	* Version 2.11d
	* Kithayri
	*
	* (updated by Garkin, Phinix, Sounomi)
]]--
local Azurah = _G['Azurah'] -- grab addon table from global
local L = Azurah:GetLocale()

Azurah.name			= 'Azurah'
Azurah.slash		= '/azurah'
Azurah.version		= '2.11d'
Azurah.versionDB	= 2
Azurah.loaded		= false

Azurah.movers		= {}
Azurah.snapToGrid	= true
Azurah.uiUnlocked	= false

local defaults = {
	uiData						= {},
	compassPinScale				= 1.0,
	compassHidePinLabel			= false,

	attributes = {
		fadeMinAlpha			= 0,	-- when full
		fadeMaxAlpha			= 1,	-- when not full
		combatBars				= true,
		lockSize				= false,
		-- overlays
		healthOverlay			= 2,
		healthOverlayFancy		= false,
		healthFontFace			= 'Univers 67',
		healthFontColour		= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		healthFontOutline		= 'soft-shadow-thick',
		healthFontSize			= 16,
		magickaOverlay			= 2,
		magickaOverlayFancy		= false,
		magickaFontFace			= 'Univers 67',
		magickaFontColour		= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		magickaFontOutline		= 'soft-shadow-thick',
		magickaFontSize			= 16,
		staminaOverlay			= 2,
		staminaOverlayFancy		= false,
		staminaFontFace			= 'Univers 67',
		staminaFontColour		= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		staminaFontOutline		= 'soft-shadow-thick',
		staminaFontSize			= 16,
	},
	target = {
		lockSize				= false,
		colourByBar				= 2,
		colourByName			= 1,
		colourByLevel			= true,
		classShow				= false,
		classByName				= true,
		allianceShow			= false,
		allianceByName			= false,
		-- overlay
		overlay					= 2,
		overlayFancy			= false,
		fontFace				= 'Univers 67',
		fontColour				= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		fontOutline				= 'soft-shadow-thick',
		fontSize				= 16,
	},
	bossbar = {
		overlay					= 2,
		overlayFancy			= false,
		fontFace				= 'Univers 67',
		fontColour				= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		fontOutline				= 'soft-shadow-thick',
		fontSize				= 16,
	},
	actionBar = {
		hideBindBG				= false,
		hideBindText			= false,
		hideWeaponSwap			= false,
		-- overlays
		ultValueShow			= true,
		ultValueFontFace		= 'Univers 67',
		ultValueFontColour		= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		ultValueFontOutline		= 'soft-shadow-thick',
		ultValueFontSize		= 16,
		ultPercentShow			= true,
		ultPercentFontFace		= 'Univers 67',
		ultPercentFontColour	= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		ultPercentFontOutline	= 'soft-shadow-thick',
		ultPercentFontSize		= 16,
		ultPercentRelative		= false,
	},
	experienceBar = {
		displayStyle			= 1,
		-- overlay
		overlay					= 2,
		overlayFancy			= true,
		fontFace				= 'Univers 67',
		fontColour				= {r = 0.9, g = 0.9, b = 0.9, a = 1.0},
		fontOutline				= 'soft-shadow-thick',
		fontSize				= 18,
	},
}

function Azurah.OnStateChanged(eventCode, eventBool)
	local self = Azurah
	self:ConfigureAttributeFade()
end

function Azurah.OnPreferedModeChanged(code, gamepadPrefered)
	local self = Azurah

	if self.uiUnlocked then
		self:LockUI()
	end

	self:InitializeUnlock()
	self:ConfigureTargetIcons()
    self:ConfigureExperienceBarOverlay()
end

function Azurah.OnPlayerActivated()
	EVENT_MANAGER:UnregisterForEvent(Azurah.name, EVENT_PLAYER_ACTIVATED)

	Azurah:InitializeUnlock()
end

function Azurah.OnInitialize(code, addon)
	if (addon ~= Azurah.name) then return end

	local self = Azurah

	EVENT_MANAGER:UnregisterForEvent(self.name, EVENT_ADD_ON_LOADED)
	SLASH_COMMANDS[self.slash] = self.SlashCommand

	self.db = ZO_SavedVars:New('AzurahDB', self.versionDB, nil, defaults)

	self:InitializePlayer()
	self:InitializeTarget()
	self:InitializeBossbar()
	self:InitializeActionBar()
	self:InitializeExperienceBar()

	self:InitializeSettings()
end

function Azurah.SlashCommand(text)
	if (text == 'lock') then
		Azurah:LockUI()
	elseif (text == 'unlock') then
		Azurah:UnlockUI()
	else
		CHAT_SYSTEM:AddMessage(Azurah:GetLocale().Usage)
	end
end

EVENT_MANAGER:RegisterForEvent(Azurah.name, EVENT_ADD_ON_LOADED, Azurah.OnInitialize)
EVENT_MANAGER:RegisterForEvent(Azurah.name, EVENT_PLAYER_ACTIVATED, Azurah.OnPlayerActivated)
EVENT_MANAGER:RegisterForEvent(Azurah.name, EVENT_GAMEPAD_PREFERRED_MODE_CHANGED, Azurah.OnPreferedModeChanged)
EVENT_MANAGER:RegisterForEvent(Azurah.name, EVENT_PLAYER_COMBAT_STATE, Azurah.OnStateChanged)
EVENT_MANAGER:RegisterForEvent(Azurah.name, EVENT_MOUNTED_STATE_CHANGED, Azurah.OnStateChanged)
EVENT_MANAGER:RegisterForEvent(Azurah.name, EVENT_WEREWOLF_STATE_CHANGED, Azurah.OnStateChanged)

-- ----------------------------
-- OVERLAY BASE ---------------
-- ----------------------------

local strformat		= string.format
local strgsub		= string.gsub
local captureStr	= '%1' .. L.ThousandsSeperator .. '%2'

local k
local function comma_value(amount)
	while (true) do
		amount, k = strgsub(amount, '^(-?%d+)(%d%d%d)', captureStr)

		if (k == 0) then
			break
		end
	end

	return amount
end

Azurah.overlayFuncs = {
	[1] = function(current, max, effMax)
		return '' -- dummy, returns an empty string
	end,
	-- standard overlays
	[2] = function(current, max, effMax)
		effMax = effMax > 0 and effMax or 1 -- ensure we don't do a divide by 0
		return strformat('%d / %d (%d%%)', current, effMax, (current / effMax) * 100)
	end,
	[3] = function(current, max, effMax)
		return strformat('%d / %d', current, effMax)
	end,
	[4] = function(current, max, effMax)
		effMax = effMax > 0 and effMax or 1 -- ensure we don't do a divide by 0
		return strformat('%d (%d%%)', current, (current / effMax) * 100)
	end,
	[5] = function(current, max, effMax)
		return strformat('%d', current)
	end,
	[6] = function(current, max, effMax)
		effMax = effMax > 0 and effMax or 1 -- ensure we don't do a divide by 0
		return strformat('%d%%', (current / effMax) * 100)
	end,
	-- comma-seperated overlays
	[12] = function(current, max, effMax)
		effMax = effMax > 0 and effMax or 1 -- ensure we don't do a divide by 0
		return strformat('%s / %s (%d%%)', comma_value(current), comma_value(effMax), (current / effMax) * 100)
	end,
	[13] = function(current, max, effMax)
		return strformat('%s / %s', comma_value(current), comma_value(effMax))
	end,
	[14] = function(current, max, effMax)
		effMax = effMax > 0 and effMax or 1 -- ensure we don't do a divide by 0
		return strformat('%s (%d%%)', comma_value(current), (current / effMax) * 100)
	end,
	[15] = function(current, max, effMax)
		return strformat('%s', comma_value(current))
	end,
	[16] = function(current, max, effMax)
		effMax = effMax > 0 and effMax or 1 -- ensure we don't do a divide by 0
		return strformat('%d%%', (current / effMax) * 100)
	end
}

function Azurah:CreateOverlay(parent, rel, relPoint, x, y, width, height, vAlign, hAlign)
	local o = WINDOW_MANAGER:CreateControl(nil, parent, CT_LABEL)
	o:SetResizeToFitDescendents(true)
	o:SetInheritScale(false)
	o:SetDrawTier(DT_HIGH)
	o:SetDrawLayer(DL_OVERLAY)
	o:SetAnchor(rel, parent, relPoint, x, y)
	o:SetHorizontalAlignment(hAlign or TEXT_ALIGN_CENTER)
	o:SetVerticalAlignment(vAlign or TEXT_ALIGN_CENTER)

	return o
end