local VERSION_MAJOR = "1";
local VERSION_MINOR = "02";
local VERSION_BOSSES = "25a";
ATLASLOOT_VERSION = "|cff4169e1AtlasLoot|r v|cff00ff00"..VERSION_MAJOR.."|r.|cff00ff00"..VERSION_MINOR.."|r.|cff9F3FFF"..VERSION_BOSSES.."|r";

local Hooked_Atlas_Refresh;


--------------------------------------------------------------------------------
-- OnEvent
--------------------------------------------------------------------------------
function AtlasLoot_OnEvent(event)
	if(event == "VARIABLES_LOADED") then
		AtlasLoot_OnVariablesLoadet();
  end
end


--------------------------------------------------------------------------------
-- OnEvent - VariablesLoadet
--------------------------------------------------------------------------------
function AtlasLoot_OnVariablesLoadet()
	Hooked_Atlas_Refresh = Atlas_Refresh;
	Atlas_Refresh = AtlasLoot_Refresh;
end


--------------------------------------------------------------------------------
-- OnLoad
--------------------------------------------------------------------------------
function AtlasLoot_OnLoad()
	this:RegisterEvent("VARIABLES_LOADED");
end


--------------------------------------------------------------------------------
-- Hooked AtlasRefresh
--------------------------------------------------------------------------------
--DEFAULT_CHAT_FRAME:AddMessage("|cff4169e1AtlasLoot:|r |cffff0000<DebugMSG>|r "..zoneID);
function AtlasLoot_Refresh()
	Hooked_Atlas_Refresh();
	
	if ( AtlasOptions.AtlasType == 1 ) then
		local zoneID = ATLAS_DROPDOWN_LIST[AtlasOptions.AtlasZone];
		local text;
		
		if(AtlasLootBossButtons[zoneID] ~= nil) then
			for i = 1, 27, 1 do
				if(AtlasLootBossButtons[zoneID][i] ~= nil and AtlasLootBossButtons[zoneID][i] ~= "" and AtlasLootItems[AtlasLootBossButtons[zoneID][i]] ~= nil) then
					getglobal("AtlasText_"..i):Hide();
					getglobal("AtlasBossLine_"..i):Show();
					if(getglobal("AtlasText_"..i):GetText() == nil and getglobal("AtlasText_"..i-1):GetText() == nil and getglobal("AtlasText_"..i-2):GetText() ~= nil) then
						getglobal("AtlasBossLine_"..i):SetText("0) Trash Mobs");
					else
						getglobal("AtlasBossLine_"..i):SetText(strsub(getglobal("AtlasText_"..i):GetText(), 11));
					end
				else
					getglobal("AtlasText_"..i):Show();
					getglobal("AtlasBossLine_"..i):Hide();
				end
			end
			getglobal("AtlasLootInfo"):Show();
		else
			for i = 1, 27, 1 do
				getglobal("AtlasText_"..i):Show();
				getglobal("AtlasBossLine_"..i):Hide();
			end
			getglobal("AtlasLootInfo"):Hide();
		end
		
	else
		for i = 1, 27, 1 do
			getglobal("AtlasText_"..i):Show();
			getglobal("AtlasBossLine_"..i):Hide();
		end
		getglobal("AtlasLootInfo"):Hide();
	end
	AtlasLootItemsFrame:Hide();

end


--------------------------------------------------------------------------------
-- Click on boss line
--------------------------------------------------------------------------------
function AtlasLootBoss_OnClick(id)
	if(AtlasLootItemsFrame:IsVisible() and AtlasLootItemsFrame.activeBoss == id) then
		AtlasLootItemsFrame:Hide();
		AtlasLootItemsFrame.activeBoss = nil;
	else
		AtlasLootItemsFrame.activeBoss = id;
		
		local zoneID = ATLAS_DROPDOWN_LIST[AtlasOptions.AtlasZone];
		local dataID = AtlasLootBossButtons[zoneID][id];
		
		local itemName, itemLink, itemQuality, itemLevel, itemType, itemSubType, itemCount, itemEquipLoc, itemTexture, itemColor;
		local iconFrame, nameFrame, extraFrame;
		local text, extra;
		for i = 1, 30, 1 do
		
			if(AtlasLootItems[dataID][i] ~= nil and AtlasLootItems[dataID][i][1] ~= 0) then
			
				itemName, itemLink, itemQuality, itemLevel, itemType, itemSubType, itemCount, itemEquipLoc, itemTexture = GetItemInfo(AtlasLootItems[dataID][i][1]);
				if(itemName ~= nil) then
					_, _, _, itemColor = GetItemQualityColor(itemQuality);
					text = itemColor..itemName;
				else
					text = AtlasLootItems[dataID][i][3];
					text = gsub(text, "=q0=", "|cff9d9d9d");
					text = gsub(text, "=q1=", "|cffFFFFFF");
					text = gsub(text, "=q2=", "|cff1eff00");
					text = gsub(text, "=q3=", "|cff0070dd");
					text = gsub(text, "=q4=", "|cff9F3FFF");
					text = gsub(text, "=q5=", "|cffFF8400");
					text = gsub(text, "=q6=", "|cffFF0000");
				end
				
				extra = AtlasLootItems[dataID][i][4];
				extra = gsub(extra, "=q0=", "|cff9d9d9d");
				extra = gsub(extra, "=q1=", "|cffFFFFFF");
				extra = gsub(extra, "=q2=", "|cff1eff00");
				extra = gsub(extra, "=q3=", "|cff0070dd");
				extra = gsub(extra, "=q4=", "|cff9F3FFF");
				extra = gsub(extra, "=q5=", "|cffFF8400");
				extra = gsub(extra, "=q6=", "|cffFF0000");
				extra = gsub(extra, "=ds=", "|cffFFd200");
				if(itemName == nil) then
					extra = extra.." |cffff0000(no iteminfo)";
--				else
--					if(getglobal(itemEquipLoc) ~= nil) then
--						extra = getglobal(itemEquipLoc).." "..itemSubType;
--					else
--						extra = "??? "..itemSubType;
--					end
				end
				
				iconFrame  = getglobal("AtlasLootItem_"..i.."_Icon");
				nameFrame  = getglobal("AtlasLootItem_"..i.."_Name");
				extraFrame = getglobal("AtlasLootItem_"..i.."_Extra");
			
				iconFrame:SetTexture("Interface\\Icons\\"..AtlasLootItems[dataID][i][2]);
				nameFrame:SetText(text);
				extraFrame:SetText(extra);
			
				getglobal("AtlasLootItem_"..i).itemID = AtlasLootItems[dataID][i][1];
				getglobal("AtlasLootItem_"..i):Show();
			else
				getglobal("AtlasLootItem_"..i):Hide();
			end
		end
		
		local boss = getglobal("AtlasBossLine_"..id):GetText();
		local a,b,c = strfind(boss, "(%S+)");
 		boss = strsub(boss, b+2);
				
		AtlasLoot_BossName:SetText("|cffFFd200Boss: |cffFFFFFF"..boss);
		AtlasLootItemsFrame:Show();
	end
end
