--[[
	Define all Global Variables here
]]

-- Global Variables that are not supposed to be on-the-fly user configurable.
XPerlLocked		= 1
XPerl_OutOfCombatQueue	= {}

local conf

function XPerl_ShowMessage(cMsg)
	local str = "|c00FF7F00"..event.."|r"
	str = str..", "..tostring(arg1)
	if (arg1) then
		if (strfind(arg1, "^raid") or strfind(arg1, "^party") or strfind(arg1, "^player") or strfind(arg1, "^pet") or strfind(arg1, "^target")) then
			str = str.."("..UnitName(arg1)..")"
		end
	end

	str = str..", "..tostring(arg2)
	str = str..", "..tostring(arg3)
	str = str..", "..tostring(arg4)
	str = str..", "..tostring(arg5)
	str = str..", "..tostring(arg6)
	str = str..", "..tostring(arg7)
	str = str..", "..tostring(arg8)
	str = str..", "..tostring(arg9)

	if (cMsg) then
		str = cMsg.." - "..str
	end

	ChatFrame7:AddMessage("|c00007F7F"..this:GetName().."|r - "..str)
end

function XPerl_Notice(str)
	if (DEFAULT_CHAT_FRAME) then
		DEFAULT_CHAT_FRAME:AddMessage(XPerl_ProductName.." - |c00B03030"..str)
	end
end

function XPerl_SetMyGlobal()
	if (XPerlConfigSavePerCharacter) then
		if (not XPerlConfig_Global) then
			XPerlConfig_Global = {}
		end

		if (not XPerlConfig_Global[GetRealmName()]) then
			XPerlConfig_Global[GetRealmName()] = {}
		end

		XPerlConfig_Global[GetRealmName()][UnitName("player")] = XPerlConfig
	else
		if (XPerlConfig_Global[GetRealmName()][UnitName("player")]) then
			XPerlConfig = XPerlConfig_Global[GetRealmName()][UnitName("player")]
		end
	end

	conf = XPerlConfig
end

local function DefaultVar(name, value)
	if (conf[name] == nil or (type(value) ~= type(conf[name]))) then
		conf[name] = value
	end
end

function XPerl_DefaultBarColours(reset)
	if (reset) then
		conf.ClassicHealthBar = nil
		conf.ColourHealthEmpty = nil
		conf.ColourHealthFull = nil
		conf.ColourMana = nil
		conf.ColourEnergy = nil
		conf.ColourRage = nil
		conf.ColourFocus = nil
	end

	DefaultVar("ClassicHealthBar",		1)
	DefaultVar("ColourHealthEmpty",		{r = 1, g = 0, b = 0})
	DefaultVar("ColourHealthFull",		{r = 0, g = 1, b = 0})
	DefaultVar("ColourMana",		{r = 0, g = 0, b = 1})
	DefaultVar("ColourEnergy",		{r = 1, g = 1, b = 0})
	DefaultVar("ColourRage",		{r = 1, g = 0, b = 0})
	DefaultVar("ColourFocus",		{r = 1, g = 0.5, b = 0.25})
end

function XPerl_DefaultReactionColours(reset)
	if (reset) then
		conf.ColourReactionEnemy = nil
		conf.ColourReactionNeutral = nil
		conf.ColourReactionUnfriendly = nil
		conf.ColourReactionFriend = nil
		conf.ColourReactionNone = nil
	        conf.ColourTapped = nil
	end

	DefaultVar("ColourReactionEnemy",	{r = 1, g = 0, b = 0})
	DefaultVar("ColourReactionNeutral",	{r = 1, g = 1, b = 0})
	DefaultVar("ColourReactionUnfriendly",	{r = 1, g = 0.5, b = 0})
	DefaultVar("ColourReactionFriend",	{r = 0, g = 1, b = 0})
	DefaultVar("ColourReactionNone",	{r = 0.5, g = 0.5, b = 1})
        DefaultVar("ColourTapped",		{r = 0.5, g = 0.5, b = 0.5})
end

function XPerl_Defaults()

	conf = XPerlConfig

	DefaultVar("BarTextures",		1)
	DefaultVar("BackgroundTextures",	0)		-- 1.8.6
	DefaultVar("Transparency",		0.8)
	DefaultVar("TextTransparency",		1)
	DefaultVar("BackColour",		{r = 0, g = 0, b = 0, a = 1})
	DefaultVar("BorderColour",		{r = 0.5, g = 0.5, b = 0.5, a = 1})
	if (not conf.BackColour.a) then
		conf.BackColour.a = 1
	end
	if (not conf.BorderColour.a) then
		conf.BorderColour.a = 1
	end

	DefaultVar("OldCastBar",		1)
	DefaultVar("CastTime",			1)
	DefaultVar("ClassColouredNames",	1)
	DefaultVar("ApplyToGuildList",		1)		-- 1.8.9
	DefaultVar("HighlightSelection",	1)
	DefaultVar("MinimapButtonPosition",	186)
	DefaultVar("MinimapButtonShown",	1)
	DefaultVar("PerlCombatFlash",		1)
	DefaultVar("CombatHitIndicator",	1)
	DefaultVar("HighlightDebuffs",		1)
	DefaultVar("HighlightDebuffsBorder",	1)
	DefaultVar("HighlightDebuffsClass",	0)		-- 1.9.4
	DefaultVar("BuffTooltipHelper",		1)
	DefaultVar("FadingTooltip",		1)		-- 1.8.3
	DefaultVar("FatHealthBars",		1)
	DefaultVar("FadingBars",		1)		-- 1.8.9
	DefaultVar("FadingBarsTime",		0.5)		-- 1.9.1
	DefaultVar("MaximumScale",		1.5)
	DefaultVar("OptionsColour",		{r = 0.7, g = 0.2, b = 0.2})	-- 1.8.3
	DefaultVar("InverseBars",		0)		-- 1.8.6
	DefaultVar("XPerlTooltipInfo",		0)		-- 1.8.6
	DefaultVar("UnitTooltips",		1)		-- 2.0.5

	XPerl_DefaultBarColours()
	XPerl_DefaultReactionColours()

	local healerModeDefault = XPerlConfig.HealerMode
	if (not healerModeDefault) then
		healerModeDefault = 0
	end
	local healerModeDefaultType = XPerlConfig.HealerModeType
	if (not healerModeDefaultType) then
		healerModeDefaultType = 1
	end

	-- Player options
	DefaultVar("ArcaneBarPlayer",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("ShowPlayerPortrait",	1)
	DefaultVar("ShowPlayerPortrait3D",	1)
	DefaultVar("ShowPlayerLevel",		1)
	DefaultVar("ShowPlayerClassIcon",	1)
	DefaultVar("ShowPlayerXPBar",		0)
	DefaultVar("ShowPlayerPVPRank",		1)
	DefaultVar("ShowPlayerPVP",		1)		-- 1.8.3
	DefaultVar("ShowPlayerValues",		1)
	DefaultVar("ShowPlayerPercent",		1)
	DefaultVar("Scale_PlayerFrame",		0.8)
	DefaultVar("ShowPartyNumber",		1)
	DefaultVar("EnergyTicker",		1)
	DefaultVar("FullScreenStatus",		0)		-- 1.8.4
	DefaultVar("FullScreenStatusWarn",	20)
	DefaultVar("FullScreenStatusOK",	40)
	DefaultVar("HealerModePlayer",		healerModeDefault)	-- 1.9.1
	DefaultVar("HealerModePlayerType",	healerModeDefaultType)	-- 1.9.1

	-- Player Pet
	DefaultVar("ShowPlayerPetPortrait",	1)
	DefaultVar("ShowPlayerPetPortrait3D",	1)		-- 1.8.3
	DefaultVar("PetHappiness",		1)
	DefaultVar("PetHappinessSad",		1)		-- Only show happiness when not happy
	DefaultVar("PetFlashWhenSad",		1)
	DefaultVar("ShowPetLevel",		1)
	DefaultVar("Scale_PetFrame",		0.8)
	DefaultVar("ShowPetXP",			1)
	DefaultVar("ShowPlayerPetName",		1)
	DefaultVar("PlayerPetBuffSize",		20)
	DefaultVar("ShowPlayerPetValues",	1)

	-- Target
	DefaultVar("ArcaneBarTarget",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("ShowTargetPortrait",	1)
	DefaultVar("ShowTargetPortrait3D",	1)		-- 1.8.3
	DefaultVar("ShowTargetClassIcon",	1)
	DefaultVar("ShowTargetMobType",		1)
	DefaultVar("ShowTargetLevel",		1)
	DefaultVar("ShowTargetElite",		1)
	DefaultVar("ShowTargetMana",		1)
	DefaultVar("ShowTargetPercent",		1)
	DefaultVar("ShowTargetValues",		1)
	DefaultVar("UseCPMeter",		1)
	DefaultVar("BlizzardCPMeter",		0)
	DefaultVar("BlizzardCPPosition",	"top")
	DefaultVar("ShowTargetPVPRank",		1)
	DefaultVar("ShowTargetPVP",		1)		-- 1.8.3
	DefaultVar("Show30YardSymbol",		1)
	DefaultVar("Scale_TargetFrame",		0.8)
	DefaultVar("AlternateRaidIcon",		1)
	DefaultVar("TargetBuffSize",		22)
	DefaultVar("TargetBuffRows",		3)
	DefaultVar("TargetBuffsAbove",		0)
	DefaultVar("TargetReactionHighlight",	0)		-- 1.8.6
	DefaultVar("HealerModeTarget",		healerModeDefault)	-- 1.9.1
	DefaultVar("HealerModeTargetType",	healerModeDefaultType)	-- 1.9.1
	--DefaultVar("TargetChangeDefer",		1)		-- 1.9.5

	-- Focus
	DefaultVar("ArcaneBarFocus",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("ShowFocusPortrait",		1)		-- 2.0.0
	DefaultVar("ShowFocusPortrait3D",	1)		-- 2.0.0
	DefaultVar("ShowFocusClassIcon",	1)		-- 2.0.0
	DefaultVar("ShowFocusMobType",		1)		-- 2.0.0
	DefaultVar("ShowFocusLevel",		1)		-- 2.0.0
	DefaultVar("ShowFocusElite",		1)		-- 2.0.0
	DefaultVar("ShowFocusMana",		1)		-- 2.0.0
	DefaultVar("ShowFocusPercent",		1)		-- 2.0.0
	DefaultVar("ShowFocusValues",		1)		-- 2.0.0
	DefaultVar("ShowFocusPVPRank",		1)		-- 2.0.0
	DefaultVar("ShowFocusPVP",		1)		-- 2.0.0
	DefaultVar("Scale_FocusFrame",		0.8)		-- 2.0.0
	DefaultVar("FocusBuffSize",		22)		-- 2.0.0
	DefaultVar("FocusBuffRows",		3)		-- 2.0.0
	DefaultVar("FocusBuffsAbove",		0)		-- 2.0.0
	DefaultVar("FocusReactionHighlight",	0)		-- 2.0.0
	DefaultVar("HealerModeFocus",		healerModeDefault)		-- 2.0.0
	DefaultVar("HealerModeFocysType",	healerModeDefaultType)		-- 2.0.0

	-- Target's Target
	DefaultVar("ShowTargetTarget",		1)
	DefaultVar("TargetTargetHistory",	0)
	DefaultVar("ShowTargetTargetTarget",	1)
	DefaultVar("TargetTargetBuffs",		1)
	DefaultVar("TargetCastableBuffs",	0)
	DefaultVar("TargetCurableDebuffs",	0)
	DefaultVar("TargetTargetTargetBuffs",	1)
	DefaultVar("Scale_TargetTargetFrame",	0.8)
	DefaultVar("ShowTargetTargetPercent",	1)
	DefaultVar("ShowTargetTargetValues",	1)
	DefaultVar("ShowTargetTargetLevel",	1)
	DefaultVar("ShowTargetTargetMana",	1)
	DefaultVar("TargetTargetBuffsAbove",	0)

	-- Cast Party
	DefaultVar("CastParty",			1)
	DefaultVar("CastPartyRaidOnly",		0)		-- Cast Party works on raid frame only?

	-- Party
	DefaultVar("ArcaneBarParty",		XPerlConfig.ArcaneBar or 1)	-- 2.0.0
	DefaultVar("PartySpacing",		23)
	DefaultVar("PartyAnchor",		"TOP")
	DefaultVar("ShowPartyPortrait",		1)
	DefaultVar("ShowPartyPortrait3D",	1)		-- 1.8.3
	DefaultVar("ShowPartyTarget",		1)
	DefaultVar("ShowPartyLevel",		1)
	DefaultVar("ShowPartyNames",		1)
	DefaultVar("ShowPartyValues",		1)
	DefaultVar("ShowPartyPercent",		1)
	DefaultVar("ShowPartyClassIcon",	1)
	DefaultVar("ShowPartyPVP",		1)		-- 1.8.3
	DefaultVar("ShowPartyRaid",		1)
	DefaultVar("PartyBuffs",		1)
	DefaultVar("PartyDebuffs",		1)
	DefaultVar("PartyCastableBuffs",	0)
	DefaultVar("PartyCurableDebuffs",	0)
	DefaultVar("Scale_PartyFrame",		0.8)
	DefaultVar("PartyDebuffsBelow",		0)
	DefaultVar("ShowPartyPets",		1)
	DefaultVar("Scale_PartyPets",		conf.Scale_PartyFrame)
	DefaultVar("ShowPartyPetName",		1)
	DefaultVar("ShowPartyPetBuffs",		1)		-- 1.8.3
	DefaultVar("ShowPartyPetMana",		1)		-- 1.9.1
	DefaultVar("ShowPartyPetLevel",		1)		-- 1.9.1
	DefaultVar("ShowParty30YardSymbol",	1)
	DefaultVar("PartyBuffSize",		20)
	DefaultVar("HealerModeParty",		healerModeDefault)	-- 1.9.1
	DefaultVar("HealerModePartyType",	healerModeDefaultType)	-- 1.9.1

	-- Raid
	DefaultVar("SortRaidByClass",		0)
	DefaultVar("ShowRaid",			1)
	DefaultVar("ShowGroup1",		1)
	DefaultVar("ShowGroup2",		1)
	DefaultVar("ShowGroup3",		1)
	DefaultVar("ShowGroup4",		1)
	DefaultVar("ShowGroup5",		1)
	DefaultVar("ShowGroup6",		1)
	DefaultVar("ShowGroup7",		1)
	DefaultVar("ShowGroup8",		1)
	DefaultVar("ShowGroup9",		1)
	DefaultVar("ShowRaidTitles",		1)
	DefaultVar("ShowRaidPercents",		1)
	DefaultVar("Scale_Raid",		0.8)
	DefaultVar("RaidVerticalSpacing",	42)
	DefaultVar("RaidBuffs",			1)
	DefaultVar("RaidDebuffs",		0)
	DefaultVar("BuffsCastableCurable",	0)
	DefaultVar("RaidBuffsRight",		1)
	DefaultVar("RaidBuffsInside",		1)
	DefaultVar("RaidMana",			1)		-- 1.8.3
	DefaultVar("HealerModeRaid",		healerModeDefault)	-- 1.9.1
	DefaultVar("RaidAnchor",		"TOP")
	DefaultVar("SortRaidAlpha",		0)		-- 2.0.4

	XPerlConfig.ArcaneBar		= nil		-- 2.0.0 replaced by per frame options
	XPerlConfig.RaidUpward		= nil		-- 2.0.0 (beta10) replaced by anchor option
	XPerlConfig.RaidMotion		= nil		-- 2.0.0 removed, not possible
	XPerlConfig.RaidPositions	= nil		-- 2.0.0 unused
end

-- XPerl_ResetDefaults
function XPerl_ResetDefaults()

	XPerlConfig = {}
	conf = XPerlConfig
	XPerl_Defaults()

	if (XPerlConfigSavePerCharacter) then
		XPerlConfig_Global[GetRealmName()][UnitName("player")] = XPerlConfig
	end

	XPerl_OptionActions()

	if (XPerl_Options and XPerl_Options:IsShown()) then
		XPerl_Options:Hide()
		XPerl_Options:Show()
	end
end

-- XPerl_Globals_OnEvent
function XPerl_Globals_OnEvent(event)
	if (arg1 and XPerl_UnitEvent(arg1, event)) then
		return
	end

	if (event == "VARIABLES_LOADED") then
		-- Between sessions variable saving.
		this:UnregisterEvent(event)

		if (XPerlConfigSavePerCharacter) then
			local realm = GetRealmName()
			local name = UnitName("player")

			if (not XPerlConfig_Global) then
				XPerlConfig_Global = {}
			end

			if (not XPerlConfig_Global[realm]) then
				XPerlConfig_Global[realm] = {}
			end

			if (not XPerlConfig_Global[realm][name]) then
				XPerlConfig_Global[realm][name] = {}

				if (XPerlConfig and XPerlConfig.BarTextures) then
					XPerlConfig_Global[realm][name] = XPerlConfig
				end
			else
				XPerlConfig = XPerlConfig_Global[realm][name]
			end

			if (not XPerlConfig) then
				XPerlConfig = XPerlConfig_Global[realm][name]
			end
		else
			if (not XPerlConfig) then
				XPerlConfig = {}
			end
		end

		conf = XPerlConfig

		-- Tell DHUD to hide Blizzard default Player and Target frames
		if (DHUD_Config) then
			local bChanged = false
			if (XPerl_Player) then
				DHUD_Config["bplayer"] = 0
				bChanged = true
			end
			if (XPerl_Target) then
				DHUD_Config["btarget"] = 0
				bChanged = true
			end
		end

		XPerl_BlizzFramesDisable()

		-- Variable checking only occurs for new install and version number change
		if ((not conf.ConfigVersion) or (conf.ConfigVersion ~= XPerl_VersionNumber)) then
			conf.ConfigVersion = XPerl_VersionNumber;	-- Set ConfigVersion to current

			XPerl_Defaults()
		end

		XPerl_Init()

	elseif (event == "PLAYER_ENTERING_WORLD") then
		if (Perl_Globals_OnEvent or Perl_InitFadeFrame) then
			DEFAULT_CHAT_FRAME:AddMessage("|c00FF0000".."ERROR! X-Perl not installed correctly!")
			DEFAULT_CHAT_FRAME:AddMessage("|c00FFFF00".."ERROR!   Previous version of X-Perl must be removed entirely. Please delete all Perl* folders in AddOns")
			DEFAULT_CHAT_FRAME:AddMessage("|c00FFFF00".."ERROR!   X-Perl folders, frames and globals were renamed for version 1.8.6 due to numerous conflicts with other versions of Perl and addons expecting Nymbia's Perl, so X-Perl now resides in newly named folders")
		end

	elseif (event == "PLAYER_REGEN_ENABLED") then
		if (XPerl_OutOfCombatOptionSet) then
			XPerl_OptionActions()
		end

		local queue = XPerl_OutOfCombatQueue
		XPerl_OutOfCombatQueue = {}
		for k,v in pairs(queue) do
			if (type(v) == "function") then
				v()
			elseif (type(v) == "table") then
				v[1](v[2])
			elseif (type(v) == "string") then
				RunScript(v)
			end
		end
	end
end

-- XPerl_ProtectedCall
function XPerl_ProtectedCall(func, self)
	if (InCombatLockdown()) then
		if (self) then
			tinsert(XPerl_OutOfCombatQueue, {func, self})
		else
			tinsert(XPerl_OutOfCombatQueue, func)
		end
	else
		func(self)
	end
end

-- XPerl_Update_RaidIcon
function XPerl_Update_RaidIcon(unit, frame)
	local index = GetRaidTargetIndex(unit)
	if ( index ) then
		SetRaidTargetIconTexture(frame, index)
		frame:Show()
	else
		frame:Hide()
	end
end
