Quantcast
local ns = select( 2, ... )
local f = CreateFrame("Frame");
ns.mainframe = f;
local L = ns.L;
local currentencounter,lastencounter,currentinstance=-1,0,-1;
local diffmode = false;
local EJ_GetSectionInfo_Orig=EJ_GetSectionInfo;
local DJE_DisplayEncounter_Orig = EncounterJournal_DisplayEncounter;
local vistable={};
local usedbuttons =0;
ns.modules = {"bn"};
DungeonJournalEnhanced_GConfig = {};
DungeonJournalEnhanced_PConfig = {};


function f:EventHandler(event,arg1,...)
	if(event=="ADDON_LOADED" and arg1=="DungeonJournalEnhanced") then
		ns.gconf = DungeonJournalEnhanced_GConfig;
		ns.pconf = DungeonJournalEnhanced_PConfig
		if(L.HEROIC_DIFFICULTY_SEARCH_WORD == "Heroic Difficulty" and GetLocale()~="enUS") then
				print("Dungeon Journal Enhanced: Attention the changed spell/add feature is not working, localisation is required, please go to http://wow.curseforge.com/addons/dungeon-journal-enhanced/localization/ and translate the 'HEROIC_DIFFICULTY_SEARCH_WORD' phrase to make the feature work in your language");
		end
		f:Init();
		ns.config:Init();
		ns:LoadModules();
		collectgarbage();

		f:UnregisterEvent("ADDON_LOADED");
	end


end






function f:ToggleHeaders(collapse)
	local used = EncounterJournal.encounter.usedHeaders;
	local i=1;
	if(collapse==true) then
		i = #used;

	end
	local header = used[i];
	local didchange = false;

	while(header) do
		if(collapse==true) then
			i=i-1;
		else
			i=i+1;
		end
		if(header.expanded == collapse) then

			_G[header:GetName().."HeaderButton"]:Click();

		end
		header = used[i];
	end

end


function DJE_EJ_GetSectionInfo_Hook(sectionid)

	local fileredByDifficulty = false;
	local title, description, headerType, abilityIcon, displayInfo, siblingID, nextSectionID, _, link, startsOpen, flag1, flag2, flag3, flag4 = EJ_GetSectionInfo_Orig(sectionid);
	if(vistable[sectionid]) then
		if(vistable[sectionid].mode) then
			if(vistable[sectionid].mode == "add") then
				title= "|cFF00FF00"..title;
			elseif(vistable[sectionid].mode == "rem") then
				title= "|cFFFF0000"..title;
			else
				title= "|cFFFFAA00"..title;
			end
		end
	else
		fileredByDifficulty = true;
	end


	return title, description, headerType, abilityIcon, displayInfo,siblingID, nextSectionID, fileredByDifficulty, link, startsOpen, flag1, flag2, flag3, flag4;

end

function f:GetCurrentEncounterID()

	return currentencounter;

end

function DJE_DisplayEncounter_Hook(encounterid,arg)


	currentencounter = encounterid;

	if(diffmode==true and lastencounter>0 and currentencounter~=lastencounter) then

		f:ChangeDiffMode();
	end

	if (currentencounter ~= lastencounter) then
		lastencounter=currentencounter;
	end
	f.diffbutton:Show();
	f.expandbutton:Show();
	f.collapsebutton:Show();
	if(ns.bn) then
		ns.bn.notesbutton:Show();
	end
	return DJE_DisplayEncounter_Orig(encounterid,arg);
end

function f:BossButtonScaler()

	if (not ns.activeconfig.ScaleBossIcons) then
		return
	end

	f:DoBossButtonScaling(ns.activeconfig.ScaleBossIconsVal);


end

function f:DoBossButtonScaling(newscale)
	for i=1,10 do
		local button = _G["EJMapButton"..i];
		if(button) then
			if (not button.DJE_scaled or button.DJE_scaled~=newscale ) then
				 if (not button.DJE_scaled) then
					button.origsize = {button:GetWidth(),button:GetHeight(),button.bgImage:GetWidth(),button.bgImage:GetHeight()}
				 end
				button:SetWidth(button.origsize[1]*newscale);
				button:SetHeight(button.origsize[2]*newscale);
				button.bgImage:SetWidth(button.origsize[3]*newscale);
				button.bgImage:SetHeight(button.origsize[4]*newscale);
				button.DJE_scaled = newscale;
			end
		else
			return;
		end
	end
end

function f:ShowDiff()
	f:ChangeDiffMode();
end

function f:GetDiff()
	local _, _, _, rootSectionID = EJ_GetEncounterInfo(currentencounter);
	wipe(vistable);
	EncounterJournal.isHeroic = false;
	EncounterJournal_Refresh()
	f:GetAllSpells(rootSectionID,"add",0,0);
	EncounterJournal.isHeroic = true;
	EncounterJournal_Refresh()
	f:GetAllSpells(rootSectionID,"rem",0,0);
	EncounterJournal_Refresh()
end

function f:ChangeDiffMode()
	if(diffmode == true) then
		diffmode = false;
		EJ_GetSectionInfo = EJ_GetSectionInfo_Orig;
		f.diffbutton.selected:Hide();
		EncounterJournal_Refresh()
	else
		EJ_GetSectionInfo = DJE_EJ_GetSectionInfo_Hook;
		diffmode = true;
		f.heroicbutton.selected:Show();
		f.diffbutton.selected:Show();
		f:GetDiff();
	end

end


function f:GetAllSpells(nextSection,mode,parentid,depth)

	local title,subsection,heroic,desc;
	local currentsection;

	while(nextSection) do
		local heroicchange;
		currentsection = nextSection;
		title, desc, _, _, _, nextSection, subsection, heroic, _, _, _, _, _, _ = EJ_GetSectionInfo_Orig(nextSection);

		if(mode=="rem") then
			heroicchange=string.find(desc,L.HEROIC_DIFFICULTY_SEARCH_WORD);

		end

		if(heroic or heroicchange) then
			if (not vistable[currentsection] or not vistable[currentsection].mode) then
				vistable[currentsection] = {};
				vistable[currentsection].changed = true;
				if(heroicchange) then

					vistable[currentsection].mode = "change";
				else
					vistable[currentsection].mode = mode;
				end
				if(depth>0) then
					for i=1,depth do
						if not (vistable[parentid[i]]) then
							vistable[parentid[i]] = {};
							vistable[parentid[i]].changed = false;
						end
					end
				end
			end
		else

		end

		if(subsection) then
			if(depth == 0) then
				parentid = {};
			end
			parentid[depth+1] = currentsection;
			f:GetAllSpells(subsection,mode,parentid,depth+1);
		end
	end
end

function f:SetPosSize()
	if(ns.activeconfig.position) then
		EncounterJournal:ClearAllPoints();
		EncounterJournal:SetPoint(unpack(ns.activeconfig.position));
	end

end

function f:HideButton()

	if(type(self)=="number") then
		currentinstance = self;
		currentencounter = 'i'..currentinstance;
	else
		currentinstance =-1;
	end
	if(ns.bn ) then
		if(currentinstance<0) then
			ns.bn.notesbutton:Hide();
			ns.bn:HideBossNotes();
		else
			ns.bn.notesbutton:Show();
			ns.bn:HideBossNotes();
		end

	end
	f.expandbutton:Hide();
	f.collapsebutton:Hide();
	f.diffbutton:Hide();
end

function f:Init()

	local diffbutton = CreateFrame("Button","DungenJournalEnhancedDiffButton",EncounterJournal,"EncounterDifficultyTemplate");
	diffbutton:SetText("Diff");
	--diffbutton:SetHeight(22);
	--diffbutton:SetWidth(25);
	diffbutton:SetPoint("RIGHT","EncounterJournalEncounterFrameInfoHeroicButton",-80,0);
	diffbutton:Hide();
	diffbutton:SetScript("OnClick",f.ChangeDiffMode)
	f.diffbutton = diffbutton;

	--local button1 = CreateFrame("Button","DungenJournalEnhancedOptionButton",EncounterJournal,"MagicButtonTemplate");
	--button1:SetText("Open DJE options");
	--button1:SetWidth(_G[button1:GetName().."Text"]:GetWidth()+10);
	--button1:SetPoint("BOTTOMLEFT",EncounterJournal,0,-(button1:GetHeight()));
	--button1:SetScript("onclick",function () InterfaceOptionsFrame_OpenToCategory("Dungeon Journal Enhanced");end);




	local button2 = CreateFrame("Button","DungenJournalEnhancedCollapseButton",EncounterJournal,"MagicButtonTemplate");
	button2:SetText(L.COLLAPSE);
	button2:SetWidth(_G[button2:GetName().."Text"]:GetWidth()+10);
	button2:SetPoint("BOTTOMRIGHT",EncounterJournal,0,-(button2:GetHeight()));
	button2:SetScript("onclick",function () f:ToggleHeaders(true) ;end);
	button2:Hide();
	f.expandbutton = button2;

	local button3 = CreateFrame("Button","DungenJournalEnhancedExpandButton",EncounterJournal,"MagicButtonTemplate");
	button3:SetText(L.EXPAND);
	button3:SetWidth(_G[button3:GetName().."Text"]:GetWidth()+10);
	button3:SetPoint("RIGHT",button2,"LEFT",-10,0);
	button3:SetScript("onclick",function () f:ToggleHeaders(false);end);
	button3:Hide();
	f.collapsebutton = button3;

	f.heroicbutton = _G["EncounterJournalEncounterFrameInfoHeroicButton"];
	hooksecurefunc("EncounterJournal_DisplayInstance",f.HideButton);
	hooksecurefunc(EncounterJournalNavBarHomeButton,"myclick",f.HideButton);
	EncounterJournal:HookScript("OnShow",f.SetPosSize);
	EncounterJournal_DisplayEncounter = DJE_DisplayEncounter_Hook;



end

function ns:LoadModules()
	for key, val in pairs(ns.modules) do
		if(ns.activeconfig.modules and ns.activeconfig.modules[val]==1) then
			ns["init"..val]();
		end
		ns["init"..val] = nil;
	end
end



f:SetScript("OnEvent",f.EventHandler);
f:RegisterEvent("ADDON_LOADED");