 
 -- uguu no peeking *blush* OwO
 
local version = "2.1"
local addonName = "Beef's Auto Mark"
local addonLabel = "AutoMark"
local addonFolder = "AutoMark"

local marks = {
	[1] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_1:18\124t",
	[2] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_2:18\124t",
	[3] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_3:18\124t",
	[4] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_4:18\124t",
	[5] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_5:18\124t",
	[6] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_6:18\124t",
	[7] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_7:18\124t",
	[8] = " \124TInterface\\TargetingFrame\\UI-RaidTargetingIcon_8:18\124t",
}

local arrow = " \124TInterface\\chatframe\\chatframeexpandarrow:20\124t"

----------------------------------------------------------------------------------------------------

local function buildDefaultMarkDatabase()
	return {
		["Alyson Antille"] = { 5 },
		["Amani Dragonhawk"] = { 7, 6 },
		["Amani Elder Lynx"] = { 8 },
		["Amani'shi Axe Thrower"] = { 8, 7 },
		["Amani'shi Beast Tamer"] = { 8, 5 },
		["Amani'shi Berserker"] = { 8, 7 },
		["Amani'shi Flame Caster"] = { 8, 7, 5 },
		["Amani'shi Guardian"] = { 8, 7, 6 },
		["Amani'shi Handler"] = { 8, 7 },
		["Amani'shi Medicine Man"] = { 5, 3, 8 },
		["Amani'shi Protector"] = { 7 },
		["Amani'shi Scout"] = { 8, 2 },
		["Amani'shi Tempest"] = { 6 },
		["Amani'shi Trainer"] = { 8 },
		["Amani'shi Tribesman"] = { 7, 6, 3 },
		["Amani'shi Warbringer"] = { 8, 7 },
		["Amani'shi Wind Walker"] = { 8 },
		["Aqueous Lord"] = { 8, 7 },
		["Aqueous Spawn"] = { 8 },
		["Arcane Anomaly"] = { 8, 7 },
		["Arcane Protector"] = { 8 },
		["Arcane Watchman"] = { 8, 7 },
		["Baron Rafe Dreuger"] = { 7 },
		["Blindeye the Seer"] = { 8 },
		["Bonechewer Taskmaster"] = { 8 },
		["Bonechewer Worker"] = { 7, 6 },
		["Chaotic Sentience"] = { 8 },
		["Coilskar General"] = { 8, 7 },
		["Coilskar Harpooner"] = { 5, 1 },
		["Coilskar Sea-Caller"] = { 7, 5 },
		["Coilskar Wrangler"] = { 7, 6 },
		["Coldmist Widow"] = { 8, 7 },
		["Concubine"] = { 3, 8 },
		["Darkheart"] = { 1 },
		["Dragon Turtle"] = { 2, 4 },
		["Dragonmaw Sky Stalker"] = { 7 },
		["Dragonmaw Wind Reaver"] = { 6 },
		["Dragonmaw Wyrmcaller"] = { 8, 7 },
		["Doomfire Destroyer"] = { 8 },
		["Ethereal Spellfilcher"] = { 8 },
		["Ethereal Thief"] = { 8, 7, 6 },
		["Fenstalker"] = { 3 },
		["Fleshbeast"] = { 8, 7, 6 },
		["Gazakroth"] = { 3 },
		["Ghastly Haunt"] = { 8 },
		["Ghostly Philanthropist"] = { 8, 7 },
		["Ghostly Steward"] = { 8, 7 },
		["Gathios the Shatterer"] = { 8 },
		["Grand Warlock Alythess"] = { 7 },
		["Greater Fleshbeast"] = { 8 },
		["Gronn-Priest"] = { 8, 7 },
		["Hand of the Deceiver"] = { 8, 7, 6 },
		["High King Maulgar"] = { 1 },
		["High Nethermancer Zerevor"] = { 5 },
		["Homunculus"] = { 6 },
		["Hex Lord Malacrass"] = { 8 },
		["Illidari Boneslicer"] = { 4, 3 },
		["Illidari Centurion"] = { 8, 7 },
		["Illidari Defiler"] = { 1, 6 },
		["Illidari Fearbringer"] = { 8, 7 },
		["Illidari Heartseeker"] = { 3 },
		["Illidari Nightlord"] = { 8 },
		["Julianne"] = { 8 },
		["Kil'rek"] = { 8 },
		["Kiggler the Crazed"] = { 6 },
		["Koragg"] = { 1 },
		["Krosh Firehand"] = { 5 },
		["Lair Brute"] = { 8, 7, 6 },
		["Lady Catriona Von'Indi"] = { 5 },
		["Lady Keira Berrybuck"] = { 6 },
		["Lady Malande"] = { 6 },
		["Lady Sacrolash"] = { 8 },
		["Leviathan"] = { 8 },
		["Lord Raadan"] = { 2 },
		["Lord Robin Daris"] = { 8 },
		["Magical Horror"] = { 8 },
		["Mana Warp"] = { 7, 6 },
		["Midnight"] = { 8 },
		["Moroes"] = { 1, 8 },
		["Mother Shahraz"] = { 8 },
		["Night Mistress"] = { 7 },
		["Oblivion Mage"] = { 7, 6 },
		["Olm the Summoner"] = { 7 },
		["Phantom Attendant"] = { 7, 6 },
		["Phantom Guardsman"] = { 8, 7 },
		["Phantom Guest"] = { 8 },
		["Phantom Stagehand"] = { 8 },
		["Phantom Valet"] = { 8 },
		["Priestess of Delight"] = { 8 },
		["Priestess of Dementia"] = { 8, 7 },
		["Prince Malchezaar"] = { 8 },
		["Promenade Sentinel"] = { 8 },
		["Shade of Aran"] = { 8 },
		["Shadow Pillager"] = { 8, 7, 6 },
		["Shadowsword Commander"] = { 8 },
		["Shadowsword Guardian"] = { 8 },
		["Shadowsword Lifeshaper"] = { 5, 1, 3 },
		["Shadowsword Manafiend"] = { 3, 8, 4, 5 },
		["Shadowsword Soulbinder"] = { 6 },
		["Shadowsword Vanquisher"] = { 8, 5, 7 },
		["Sister of Pain"] = { 7 },
		["Sister of Pleasure"] = { 8 },
		["Skeletal Usher"] = { 8, 7, 4 },
		["Skeletal Waiter"] = { 8 },
		["Slither"] = { 2 },
		["Sorcerous Shade"] = { 8 },
		["Spectral Apprentice"] = { 8, 7, 6 },
		["Spectral Charger"] = { 8, 7 },
		["Spectral Patron"] = { 7, 6 },
		["Spectral Performer"] = { 8, 7, 6 },
		["Spectral Retainer"] = { 8 },
		["Spectral Sentry"] = { 8, 7 },
		["Spectral Servant"] = { 8 },
		["Spectral Stable Hand"] = { 8, 7, 6 },
		["Spectral Stallion"] = { 7, 8, 6 },
		["Spellbound Attendant"] = { 8, 7 },
		["Spell Shade"] = { 8, 7, 6 },
		["Sunblade Arch Mage"] = { 7 },
		["Sunblade Cabalist"] = { 5 },
		["Sunblade Dawn Priest"] = { 1 },
		["Sunblade Dragonhawk"] = { 2 },
		["Sunblade Dusk Priest"] = { 3 },
		["Sunblade Slayer"] = { 8 },
		["Sunblade Vindicator"] = { 8, 4 },
		["Thurg"] = { 5 },
		["Terestian Illhoof"] = { 7, 8 },
		["Trapped Soul"] = { 8 },
		["Wanton Hostess"] = { 6 },
	}
end

local function cloneMarkDatabase(source)
	local copy = {}

	if source == nil then
		return copy
	end

	for name, priorities in pairs(source) do
		copy[name] = {}
		if priorities ~= nil then
			for index, mark in pairs(priorities) do
				copy[name][index] = mark
			end
		end
	end

	return copy
end

local function databaseIsEmpty(db)
	if db == nil then
		return true
	end

	for _, _ in pairs(db) do
		return false
	end

	return true
end

local function applyDefaultMarks()
	AUTOMARK_DB = cloneMarkDatabase(buildDefaultMarkDatabase())
end

----------------------------------------------------------------------------------------------------

local function savingBlockedByCombatLock()
	return (InCombatLockdown() == 1) and (AUTOMARK_COMBATLOCK == true)
end

----------------------------------------------------------------------------------------------------

local function add(mark, name, manual)
	local skipSave = false

	if AUTOMARK_ENABLED == true then

		if (UnitName("target") ~= nil) and (name == nil) then
		
			name = UnitName("target")
			
		else
		end
		
		if (mark ~= nil) and (UnitName("target") == nil) and (manual == true) then
		
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FYou need to have a target to assign marks.|r", 1.0, 0.5, 0.3, 53, 5)
			return 
		
		else
		end
		
		if name ~= "Unknown" then
		
			if savingBlockedByCombatLock() == true then
				skipSave = true
			else
			end
		
			if (AUTOMARK_DB[name] == nil) and (skipSave ~= true) then
			
				AUTOMARK_DB[name] = {}
				
				if AUTOMARK_DB[name][1] == nil then
				
					AUTOMARK_DB[name][1] = mark
					DEFAULT_CHAT_FRAME:AddMessage(marks[mark]..arrow..name.." (|cffADFF2F1|r)", 1.0, 0.5, 0.3, 53, 5)
				
				else
				end
				
			else
			
				if (AUTOMARK_SINGLEMARK == true) and (AUTOMARK_DB[name][1] ~= nil) and (AUTOMARK_DB[name][1] ~= mark) then
					skipSave = true
				else
				end
				
				if skipSave ~= true then
					for i = 1,8,1 do
				
						if AUTOMARK_DB[name][i] == mark then
						
							break
					
						elseif i == 8 then
					
							for k = 1,8,1 do
						
								if AUTOMARK_DB[name][k] == nil then
							
									AUTOMARK_DB[name][k] = mark
									DEFAULT_CHAT_FRAME:AddMessage(marks[mark]..arrow..name.." (|cffADFF2F"..k.."|r)", 1.0, 0.5, 0.3, 53, 5)
									break
							
								else
								end
							
							end
					
						else
						end
				
					end
				else
				end
				
			end
			
		else
		end
		
		if (GetNumRaidMembers() > 0) or (GetRealNumPartyMembers() > 0) then
			
			if (manual == true) then
			
				if (IsRaidOfficer()) or (IsRaidLeader()) or (IsRealPartyLeader()) then
					SetRaidTarget("target", mark)
				else
				end
				
			else
			end
		
		else
		end
		
	else
	
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." is currently |cffFF0000Disabled|r. Start with /mark help", 1.0, 0.5, 0.3, 53, 5)
	
	end
	
return
end

----------------------------------------------------------------------------------------------------

local function saveCurrentMouseoverMark()

	local name = UnitName("mouseover")
	local mark = GetRaidTargetIndex("mouseover")

	if name == nil then
		return
	end

	if mark == nil then
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FThe current mouseover target has no raid mark to save.|r", 1.0, 0.5, 0.3, 53, 5)
		return
	end

	if savingBlockedByCombatLock() == true then
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FCombat Lock prevented saving that mark to the database.|r", 1.0, 0.5, 0.3, 53, 5)
		return
	end

	AUTOMARK_DB[name] = {}
	AUTOMARK_DB[name][1] = mark
	DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FSaved current mark priority for "..name..".|r", 1.0, 0.5, 0.3, 53, 5)

return
end

----------------------------------------------------------------------------------------------------

local function reset(target)

	if AUTOMARK_ENABLED == true then
	
		if target == nil then
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FAll assignments have been reset.|r", 1.0, 0.5, 0.3, 53, 5)
			AUTOMARK_DB = {}
		else
			local name = UnitName("target")
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2F"..name.." has been reset.|r", 1.0, 0.5, 0.3, 53, 5)
			AUTOMARK_DB[name] = nil
			
			if GetRaidTargetIndex("target") ~= nil then
			
				if (GetNumRaidMembers() > 0) or (GetNumPartyMembers() > 0) then
					
					if (IsRaidOfficer()) or (IsRaidLeader()) or (IsRealPartyLeader()) then
						SetRaidTarget("target", 0)
					else
					end
				
				else
				end
				
			else
			end
				
		end
		
	else
	
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." is currently |cffFF0000Disabled|r. Start with /mark help", 1.0, 0.5, 0.3, 53, 5)
	
	end
	
return
end

----------------------------------------------------------------------------------------------------

local function list(target)

	if AUTOMARK_ENABLED == true then

		if UnitName("target") ~= nil then

			local t = UnitName("target")
			local m1, m2, m3, m4, m5, m6, m7, m8 = "", "", "", "", "", "", "", ""
			
			if AUTOMARK_DB[t] ~= nil then
			
				if AUTOMARK_DB[t][1] ~= nil then m1 = marks[AUTOMARK_DB[t][1]] end
				if AUTOMARK_DB[t][2] ~= nil then m2 = marks[AUTOMARK_DB[t][2]] end
				if AUTOMARK_DB[t][3] ~= nil then m3 = marks[AUTOMARK_DB[t][3]] end
				if AUTOMARK_DB[t][4] ~= nil then m4 = marks[AUTOMARK_DB[t][4]] end
				if AUTOMARK_DB[t][5] ~= nil then m5 = marks[AUTOMARK_DB[t][5]] end
				if AUTOMARK_DB[t][6] ~= nil then m6 = marks[AUTOMARK_DB[t][6]] end
				if AUTOMARK_DB[t][7] ~= nil then m7 = marks[AUTOMARK_DB[t][7]] end
				if AUTOMARK_DB[t][8] ~= nil then m8 = marks[AUTOMARK_DB[t][8]] end
			
				DEFAULT_CHAT_FRAME:AddMessage(" "..t..": "..m1..m2..m3..m4..m5..m6..m7..m8, 1.0, 0.5, 0.3, 53, 5)
				
			else
			
				DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FNo marks set for the current target.|r", 1.0, 0.5, 0.3, 53, 5)
			
			end
			
		else
		
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FYou need to target something to list their mark order.|r", 1.0, 0.5, 0.3, 53, 5)
		
		end
		
	else
	
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." is currently |cffFF0000Disabled|r. Start with /mark help", 1.0, 0.5, 0.3, 53, 5)
	
	end

return
end

----------------------------------------------------------------------------------------------------

local function combatlock()

	if AUTOMARK_ENABLED == true then

		if AUTOMARK_COMBATLOCK == false then
		
			AUTOMARK_COMBATLOCK = true
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cff00FF00Combat Lock Enabled.|r", 1.0, 0.5, 0.3, 53, 5)
		
		else
		
			AUTOMARK_COMBATLOCK = false
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffFF0000Combat Lock Disabled.|r", 1.0, 0.5, 0.3, 53, 5)

		end
		
	else
	
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." is currently |cffFF0000Disabled|r. Start with /mark help", 1.0, 0.5, 0.3, 53, 5)
	
	end

return
end

----------------------------------------------------------------------------------------------------

local login = CreateFrame("Frame")
login:RegisterEvent("PLAYER_LOGIN")
login:SetScript("OnEvent", function(self, event, arg1, ...)
	if AUTOMARK_ENABLED == nil then
		AUTOMARK_ENABLED = true
	else
	end
	
	if AUTOMARK_DB == nil then
		AUTOMARK_DB = {}
	else
	end

	if databaseIsEmpty(AUTOMARK_DB) == true then
		applyDefaultMarks()
	else
	end
	
	if AUTOMARK_CACHE == nil then
		AUTOMARK_CACHE = {}
	else
	end

	if AUTOMARK_SINGLEMARK == nil then
		AUTOMARK_SINGLEMARK = false
	else
	end
	
	if AUTOMARK_COMBATLOCK == nil then
		AUTOMARK_COMBATLOCK = true
	else
	end
	
	if AUTOMARK_ENABLED == true then
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." is |cff00FF00Enabled|r! Start with /mark help", 1.0, 0.5, 0.3, 53, 5)
	else
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." is |cffFF0000Disabled|r!", 1.0, 0.5, 0.3, 53, 5)
	end
	
end)

----------------------------------------------------------------------------------------------------

local configFrame
local function syncConfigChecks()
	if configFrame ~= nil then
		AutoMarkConfigEnable:SetChecked(AUTOMARK_ENABLED == true)
		AutoMarkConfigSingle:SetChecked(AUTOMARK_SINGLEMARK == true)
		AutoMarkConfigCombat:SetChecked(AUTOMARK_COMBATLOCK == true)
	end
end

local function createConfigFrame()

	configFrame = CreateFrame("Frame", "AutoMarkConfigFrame", UIParent)
	configFrame:SetWidth(330)
	configFrame:SetHeight(266)
	configFrame:SetPoint("CENTER")
	configFrame:SetFrameStrata("DIALOG")
	configFrame:SetToplevel(true)
	configFrame:EnableMouse(true)
	configFrame:SetMovable(true)
	configFrame:RegisterForDrag("LeftButton")
	configFrame:SetScript("OnDragStart", configFrame.StartMoving)
	configFrame:SetScript("OnDragStop", configFrame.StopMovingOrSizing)
	configFrame:SetBackdrop({
		bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
		edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
		tile = true,
		tileSize = 32,
		edgeSize = 32,
		insets = { left = 11, right = 12, top = 12, bottom = 11 }
	})
	configFrame:SetBackdropColor(1, 1, 1, 1)
	configFrame:Hide()

	local title = configFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	title:SetPoint("TOP", 0, -10)
	title:SetText(addonName)

	local close = CreateFrame("Button", nil, configFrame, "UIPanelCloseButton")
	close:SetPoint("TOPRIGHT", -6, -6)

	local enable = CreateFrame("CheckButton", "AutoMarkConfigEnable", configFrame, "UICheckButtonTemplate")
	enable:SetPoint("TOPLEFT", 20, -32)
	enable:SetWidth(24)
	enable:SetHeight(24)
	local enableText = enable:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	enableText:SetPoint("LEFT", enable, "RIGHT", 4, 0)
	enableText:SetText("Enable Auto Marking")
	enable:SetScript("OnClick", function(self)
		AUTOMARK_ENABLED = self:GetChecked() and true or false
		if AUTOMARK_ENABLED == true then
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." |cff00FF00Enabled|r.", 1.0, 0.5, 0.3, 53, 5)
		else
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." |cffFF0000Disabled|r.", 1.0, 0.5, 0.3, 53, 5)
		end
	end)

	local combat = CreateFrame("CheckButton", "AutoMarkConfigCombat", configFrame, "UICheckButtonTemplate")
	combat:SetPoint("TOPLEFT", 20, -60)
	combat:SetWidth(24)
	combat:SetHeight(24)
	local combatText = combat:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	combatText:SetPoint("LEFT", combat, "RIGHT", 4, 0)
	combatText:SetText("Combat lock")
	combat:SetScript("OnClick", function(self)
		AUTOMARK_COMBATLOCK = self:GetChecked() and true or false
		if AUTOMARK_COMBATLOCK == true then
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cff00FF00Combat Lock Enabled.|r", 1.0, 0.5, 0.3, 53, 5)
		else
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffFF0000Combat Lock Disabled.|r", 1.0, 0.5, 0.3, 53, 5)
		end
	end)

	local single = CreateFrame("CheckButton", "AutoMarkConfigSingle", configFrame, "UICheckButtonTemplate")
	single:SetPoint("TOPLEFT", 20, -88)
	single:SetWidth(24)
	single:SetHeight(24)
	local singleText = single:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	singleText:SetPoint("LEFT", single, "RIGHT", 4, 0)
	singleText:SetText("Single saved mark per target")
	single:SetScript("OnClick", function(self)
		AUTOMARK_SINGLEMARK = self:GetChecked() and true or false
		if AUTOMARK_SINGLEMARK == true then
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cff00FF00Single-mark mode Enabled.|r", 1.0, 0.5, 0.3, 53, 5)
		else
			DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffFF0000Single-mark mode Disabled.|r", 1.0, 0.5, 0.3, 53, 5)
		end
	end)

	local resetBtn = CreateFrame("Button", nil, configFrame, "UIPanelButtonTemplate")
	resetBtn:SetWidth(86)
	resetBtn:SetHeight(20)
	resetBtn:SetPoint("BOTTOMLEFT", 20, 18)
	resetBtn:SetText("Clear All")
	resetBtn:SetScript("OnClick", function()
		StaticPopup_Show("AUTOMARK_RESET_ALL")
	end)

	local resetDefaultsBtn = CreateFrame("Button", nil, configFrame, "UIPanelButtonTemplate")
	resetDefaultsBtn:SetWidth(108)
	resetDefaultsBtn:SetHeight(20)
	resetDefaultsBtn:SetPoint("BOTTOMLEFT", resetBtn, "BOTTOMRIGHT", 8, 0)
	resetDefaultsBtn:SetText("Load Defaults")
	resetDefaultsBtn:SetScript("OnClick", function()
		StaticPopup_Show("AUTOMARK_RESET_DEFAULTS")
	end)

	local resetTargetBtn = CreateFrame("Button", nil, configFrame, "UIPanelButtonTemplate")
	resetTargetBtn:SetWidth(86)
	resetTargetBtn:SetHeight(20)
	resetTargetBtn:SetPoint("BOTTOMLEFT", resetDefaultsBtn, "BOTTOMRIGHT", 8, 0)
	resetTargetBtn:SetText("Reset Target")
	resetTargetBtn:SetScript("OnClick", function()
		reset(UnitName("target"))
	end)

	local infoTitle = configFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
	infoTitle:SetPoint("TOPLEFT", 20, -120)
	infoTitle:SetText("Hotkeys")

	local infoShift = configFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
	infoShift:SetPoint("TOPLEFT", infoTitle, "BOTTOMLEFT", 0, -6)
	infoShift:SetJustifyH("LEFT")
	infoShift:SetText("SHIFT: assigned mark")

	local infoAlt = configFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
	infoAlt:SetPoint("TOPLEFT", infoShift, "BOTTOMLEFT", 0, -4)
	infoAlt:SetJustifyH("LEFT")
	infoAlt:SetText("ALT: reset mob data")

	local infoCtrl = configFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
	infoCtrl:SetPoint("TOPLEFT", infoAlt, "BOTTOMLEFT", 0, -4)
	infoCtrl:SetJustifyH("LEFT")
	infoCtrl:SetText("CTRL: clear mark")

	local infoCtrlShift = configFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
	infoCtrlShift:SetPoint("TOPLEFT", infoCtrl, "BOTTOMLEFT", 0, -4)
	infoCtrlShift:SetJustifyH("LEFT")
	infoCtrlShift:SetText("CTRL+SHIFT: save current mark")

	configFrame:SetScript("OnShow", function()
		syncConfigChecks()
	end)

end

local function toggleConfig()
	if configFrame == nil then
		createConfigFrame()
	end

	if configFrame:IsShown() then
		configFrame:Hide()
	else
		configFrame:Show()
		syncConfigChecks()
	end
end

----------------------------------------------------------------------------------------------------

StaticPopupDialogs["AUTOMARK_RESET_ALL"] = {
	text = "Delete all AutoMark assignments?",
	button1 = YES,
	button2 = NO,
	OnAccept = function()
		reset(nil)
	end,
	timeout = 0,
	whileDead = true,
	hideOnEscape = true,
}

StaticPopupDialogs["AUTOMARK_RESET_DEFAULTS"] = {
	text = "Reset AutoMark assignments to plugin defaults?",
	button1 = YES,
	button2 = NO,
	OnAccept = function()
		applyDefaultMarks()
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FDefault assignments have been restored.|r", 1.0, 0.5, 0.3, 53, 5)
	end,
	timeout = 0,
	whileDead = true,
	hideOnEscape = true,
}

----------------------------------------------------------------------------------------------------

SLASH_AUTOMARK1, SLASH_AUTOMARK2, SLASH_AUTOMARK3 = "/am", "/automark", "/amark";
local function handler(msg, editBox)
	local arg = strsplit(" ", msg, 1)
	arg = string.lower(arg)

	if arg == "" then
		arg = "help"
	end

	if arg == "help" then
	
		DEFAULT_CHAT_FRAME:AddMessage("|cffADFF2F"..addonName.." is an addon for quick mouseover marking while out of combat.|r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage("|cffADFF2FThe addon will use any previously seen marks, unless manually assigned by the user.|r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage("|cffADFF2FHold SHIFT and hover your cursor over the mobs to mark them according to assignments.|r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage("|cffADFF2FHold CTRL+SHIFT and hover to save the mob's current raid mark as top priority.|r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage("|cffADFF2FHold ALT and hover your cursor over the mobs to reset saved assignments.|r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage("|cffADFF2FHold CTRL and hover your cursor over the mobs to unmark them.|r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" "..addonName.." ("..version..") README.txt at \\Interface\\AddOns\\"..addonFolder.." ", 0.0, 1.0, 0.0, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage("=========", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" /am, /automark |cffADFF2F- Shows this help. |r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" /am on | off |cffADFF2F- Enables or disables the addon. |r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" /am star |  circle |  diamond |  triangle |  moon |  square |  cross |  skull  |cffADFF2F- Assigns a mark for the currently targeted mob. |r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" /am reset |cffADFF2F- Clears all previously saved marks. (Can be used to reset only one mob's assignments, if you are currently targeting it) |r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" /am list |cffADFF2F- Lists the mark assignments (priority) on the currently targeted mob. |r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" /am combat |cffADFF2F- Safety switch for preventing "..addonLabel.." use during combat. |r", 1.0, 0.5, 0.3, 53, 5)
		DEFAULT_CHAT_FRAME:AddMessage(" /am config |cffADFF2F- Toggle the config window. |r", 1.0, 0.5, 0.3, 53, 5)

	elseif arg == "on" then

		AUTOMARK_ENABLED = true
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." |cff00FF00Enabled|r.", 1.0, 0.5, 0.3, 53, 5)

	elseif arg == "off" then

		AUTOMARK_ENABLED = false
		DEFAULT_CHAT_FRAME:AddMessage(addonLabel.." |cffFF0000Disabled|r.", 1.0, 0.5, 0.3, 53, 5)
		
	elseif arg == "star" then 		add(1, nil, true)
	elseif arg == "circle" then		add(2, nil, true)
	elseif arg == "nipple" then		add(2, nil, true)
	elseif arg == "diamond" then	add(3, nil, true)
	elseif arg == "triangle" then	add(4, nil, true)
	elseif arg == "moon" then		add(5, nil, true)
	elseif arg == "square" then		add(6, nil, true)
	elseif arg == "cross" then		add(7, nil, true)
	elseif arg == "skull" then		add(8, nil, true)
	
	elseif arg == "reset" then 		reset(UnitName("target"))

	elseif arg == "list" then		list(UnitName("target"))
	
	elseif arg == "combat" then		combatlock()

	elseif arg == "config" then		toggleConfig()
	
	else
	end

end
SlashCmdList["AUTOMARK"] = handler
SLASH_AUTOMARKCFG1, SLASH_AUTOMARKCFG2, SLASH_AUTOMARKCFG3, SLASH_AUTOMARKCFG4 = "/amarkconfig", "/automarkconfig", "/emarkconfig", "/emconfig";
SlashCmdList["AUTOMARKCFG"] = toggleConfig

----------------------------------------------------------------------------------------------------

local mouseover = CreateFrame("Frame")
mouseover:RegisterEvent("UPDATE_MOUSEOVER_UNIT")
mouseover:SetScript("OnEvent",function(self,event,arg1)
	
	if AUTOMARK_ENABLED == true then

		local name = UnitName("mouseover")
		
		if (IsControlKeyDown() == 1) and (IsShiftKeyDown() == 1) then
		
			saveCurrentMouseoverMark()
		
		elseif IsShiftKeyDown() == 1 then
		
			if AUTOMARK_DB[name] ~= nil then
			
				if (GetNumRaidMembers() > 0) or (GetNumPartyMembers() > 0) then
					
					if (IsRaidOfficer()) or (IsRaidLeader()) or (IsRealPartyLeader()) then
					
						if GetRaidTargetIndex("mouseover") == nil then
						
							for i = 1,8,1 do
							
								if (AUTOMARK_CACHE[AUTOMARK_DB[name][i]] == nil) and (AUTOMARK_CACHE[UnitGUID("mouseover")] == nil) then
									
									if AUTOMARK_DB[name][i] ~= nil then
									
										SetRaidTarget("mouseover", AUTOMARK_DB[name][i])
										AUTOMARK_CACHE[AUTOMARK_DB[name][i]] = true
										AUTOMARK_CACHE[UnitGUID("mouseover")] = true
										
									else
									end
									
								else
								end
							
							end
							
						else
						end
					
					else
					end
				
				else
				end
				
			else
			end
			
		else
		
			if IsControlKeyDown() == 1 then
			
				SetRaidTarget("mouseover", 0)
			
			else
			end
			
			if IsAltKeyDown() == 1 then
			
				if GetRaidTargetIndex("mouseover") ~= nil then
					SetRaidTarget("mouseover", 0)
				end

				if AUTOMARK_DB[name] ~= nil then
					AUTOMARK_DB[name] = nil
					DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FRemoved assignments for "..name..".|r", 1.0, 0.5, 0.3, 53, 5)
				else
					DEFAULT_CHAT_FRAME:AddMessage(addonLabel..": |cffADFF2FNo assignments found for "..name..".|r", 1.0, 0.5, 0.3, 53, 5)
				end
			
			end
		end
	
	else
	end

end)

----------------------------------------------------------------------------------------------------

local shiftreset = CreateFrame("Frame")
shiftreset:RegisterEvent("MODIFIER_STATE_CHANGED")
shiftreset:SetScript("OnEvent",function(self,event,arg1,arg2)

	if AUTOMARK_ENABLED == true then
	
		if ((arg1 == "LSHIFT") or (arg1 == "RSHIFT") or (arg1 == "LALT") or (arg1 == "RALT")) and (arg2 == 0) then
			
			AUTOMARK_CACHE = {}
			
		else
		end
	
	else
	end

end)

----------------------------------------------------------------------------------------------------

local raidtarget = CreateFrame("Frame")
raidtarget:RegisterEvent("RAID_TARGET_UPDATE")
raidtarget:SetScript("OnEvent",function(self,event,arg1)
	
	if (AUTOMARK_ENABLED == true) then
	
		if GetNumRaidMembers() > 0 then
			
			for i = 1,40,1 do
					
				local name = UnitName("raid"..i.."-target")
					
				if (UnitIsPlayer("raid"..i.."-target") == nil) and (UnitIsDead("raid"..i.."-target") == nil) then
					
					if GetRaidTargetIndex("raid"..i.."-target") ~= nil then
							
						local mark = GetRaidTargetIndex("raid"..i.."-target")
					
						add(mark, name)
							
					else
					end
					
				else
				end
				
			end
			
		elseif GetNumPartyMembers() > 0 then
			
			local tar = ""
			local i = GetPartyLeaderIndex()
				
			if IsRealPartyLeader() == 1 then
					
				tar = "target"
					
			else
				
				tar = "party"..i.."-target"
				
			end

			local name = UnitName(tar)
				
			if (UnitIsPlayer("party"..i.."-target") == nil) and (UnitIsDead("party"..i.."-target") == nil) then
				
				if GetRaidTargetIndex(tar) ~= nil then
						
					local mark = GetRaidTargetIndex(tar)
				
					add(mark, name)
						
				else
				end
					
			else
			end

		else
		end
	end

end)

----------------------------------------------------------------------------------------------------

local function eastertext(name)

	if GetNumRaidMembers() > 0 then
	
		SendChatMessage("\124cffADFF2F\All Hail "..name.."! \124h\124r", "RAID", nil)
	
	else
	
		SendChatMessage("\124cffADFF2F\All Hail "..name.."! \124h\124r", "PARTY", nil)

	end

return
end

local easteregg = CreateFrame("Frame")
easteregg:RegisterEvent("CHAT_MSG_SYSTEM")
easteregg:SetScript("OnEvent", function(self, event, arg1)

	if AUTOMARK_ENABLED == true then
	
		local function joined()
		
			if string.find(arg1, "has joined the raid group") ~= nil then
			
				return true
				
			end
			
			if string.find(arg1, "joins the party") ~= nil then
			
				return true
				
			end
			
		end
		
		if event=="CHAT_MSG_SYSTEM" and joined() then
		
			local name = strsplit(" ", arg1, 2)
			
			if 		(name == "Lusmu") 		then eastertext(name)
			elseif 	(name == "Luzmu") 		then eastertext(name)
			elseif 	(name == "Luxmu") 		then eastertext(name)
			elseif 	(name == "Fluffmu") 	then eastertext(name)
			elseif 	(name == "Blizmu") 		then eastertext(name)
			elseif 	(name == "Lasmoo") 		then eastertext(name)
			elseif 	(name == "Spiritnova")	then eastertext(name)		
			else
			end
			
		else
		end
	
	else
	end

end)
