Quantcast
local ns = select( 2, ... )
local config = CreateFrame("Frame");
local moduleconfig = CreateFrame("frame");
local AddMapButtons;
ns.config = config;
local L = ns.L;
local bossiconhook = false;



local function round(num, idp)
	local mult = 10^(idp or 0)
	return math.floor(num * mult + 0.5) / mult
end

function config:ChangeState()
	if(self.id=="UseGlobalConfig") then
		if(self:GetChecked()) then
			ns.pconf.UseGlobalConfig = 1;
		else
			ns.pconf.UseGlobalConfig = nil;
			if(not ns.pconf.cv) then
				ns.pconf.cv = {};

			end
			--wipe(ns.pconf.cv );
			--for i,v in pairs(ns.gconf.cv) do
			--	ns.pconf.cv[i] = v;
			--end
		end
		config:SetCurrent();
	else
		ns.activeconfig[self.id] = self:GetChecked();
		config:ChangeHandler(self.id,self:GetChecked());
	end
end

function config:SavePosSize()
	local a,b,c,d,e = EncounterJournal:GetPoint(); if(b~=nil) then b=b:GetName(); end;
	ns.activeconfig["position"] = {a,b,c,d,e}
end

function config:ChangeHandler(id,val)
	if(id=="HideIconsOnMap") then
		if(val) then
			config:HideMapButtons();
			config.ScaleBossIcons:Disable();
		else
			config.ScaleBossIcons:Enable();
			config:ShowMapButtons();
		end
	elseif(id=="ScaleBossIcons") then

		if(val) then
			config.HideIconsOnMap:Disable();
			config.ScaleBossIconsVal:Enable();
			config:ActivateBossIconHook();
		else
			ns.mainframe:DoBossButtonScaling(1);
			config.HideIconsOnMap:Enable();
			config.ScaleBossIconsVal:Disable();
		end
	elseif(id=="MoveJournalFrame") then
		if(val) then
			EncounterJournal:SetMovable(true);
			EncounterJournal:SetScript("OnDragStart", function () EncounterJournal:StartMoving();end);
			EncounterJournal:SetScript("OnDragStop", function () EncounterJournal:StopMovingOrSizing();config:SavePosSize();end);
			EncounterJournal:RegisterForDrag("LeftButton");
		else
			EncounterJournal:SetMovable(false);
			EncounterJournal:SetScript("OnDragStart", nil);
			EncounterJournal:SetScript("OnDragStop", nil);
			EncounterJournal:RegisterForDrag();
		end

	end
end

function config:SetCurrent()
	config:ChangeGlobal();
	config.HideIconsOnMap:SetChecked(ns.activeconfig.HideIconsOnMap);
	config.ScaleBossIcons:SetChecked(ns.activeconfig.ScaleBossIcons);
	config.ScaleBossIconsVal:SetValue(ns.activeconfig.ScaleBossIconsVal or 1);
	config.MoveJournalFrame:SetChecked(ns.activeconfig.MoveJournalFrame);
	config.ScaleJournalFrameVal:SetValue(ns.activeconfig.ScaleJournalFrameVal or 1);


	config:ChangeHandler("HideIconsOnMap",ns.activeconfig.HideIconsOnMap);
	config:ChangeHandler("ScaleBossIcons",ns.activeconfig.ScaleBossIcons);
	config:ChangeHandler("MoveJournalFrame",ns.activeconfig.MoveJournalFrame);
	if(ns.activeconfig.ScaleBossIcons) then
		config:ActivateBossIconHook()
	end

	for _,val in pairs (ns.modules) do
		if(ns.activeconfig.modules and ns.activeconfig.modules[val]) then
			_G["DungeonJournalEnhanced_Module_"..val]:SetChecked(1);
		else
			_G["DungeonJournalEnhanced_Module_"..val]:SetChecked(nil);
		end

	end
	--if(ns.activeconfig.modules) then
	--	for key, val in pairs(ns.activeconfig.modules) do
	--		_G["DungeonJournalEnhanced_Module_"..key]:SetChecked(val);
	--	end
	--end


end

function config:ActivateBossIconHook()
	if(bossiconhook== false) then
		hooksecurefunc("EncounterJournal_AddMapButtons",ns.mainframe.BossButtonScaler);
		bossiconhook = true;
	end
end

function config:SetDefault()

	ns.pconf["UseGlobalConfig"] = 1;
	ns.pconf["version"] = 1;

	ns.pconf.cv = {};
	if(not ns.gconf.cv) then
		ns.gconf.cv = {};
		config:ChangeGlobal();
		ns.activeconfig["ScaleBossIconsVal"] = 1;
		_G[config.ScaleBossIconsVal:GetName().."Text" ]:SetText( ns.activeconfig.ScaleBossIconsVal );
		config.ScaleBossIconsVal:SetValue(1);
		config.ScaleBossIconsVal:Disable();
	else
		config:SetCurrent();
	end
end

function config:ShowMapButtons()
	if(type(AddMapButtons)~="function") then
		return
	end
	EncounterJournal_AddMapButtons = AddMapButtons;
end

function config:HideMapButtons()

	if(type(AddMapButtons)~="function") then
		AddMapButtons = EncounterJournal_AddMapButtons;
	end
	for i=1,10 do
		local button = _G["EJMapButton"..i];
		if(button) then
			button:Hide();
		end
	end
	EncounterJournal_AddMapButtons = function() end;
end

function config:ChangeGlobal()
	if(ns.pconf.UseGlobalConfig) then
		config.UseGlobalConfig:SetChecked(1);
		ns.activeconfig = ns.gconf.cv;
	else
		ns.activeconfig = ns.pconf.cv;
	end

end

function config:ChangeSlider(val)
	local newval = round(val,2);
	if(self.id=="ScaleBossIconsVal") then
		_G[config.ScaleBossIconsVal:GetName().."Text"]:SetText(newval);
		ns.activeconfig["ScaleBossIconsVal"] = newval;
	else
		_G[config.ScaleJournalFrameVal:GetName().."Text"]:SetText(newval);
		ns.activeconfig["ScaleJournalFrameVal"] = newval;
		EncounterJournal:SetScale(newval);
	end

end


function config:Init()


	config.name = "Dungeon Journal Enhanced";

	local UseGlobalConfig = CreateFrame( "CheckButton", nil, config, "InterfaceOptionsCheckButtonTemplate" );
	config.UseGlobalConfig = UseGlobalConfig;
	UseGlobalConfig.id = "UseGlobalConfig";
	UseGlobalConfig:SetPoint( "TOPLEFT", 16, -16 );
	UseGlobalConfig:SetScript("onClick",config.ChangeState);
	UseGlobalConfig.Text:SetText( L.USE_GLOBAL_CONFIG );

	local HideIconsOnMap = CreateFrame( "CheckButton", nil, config, "InterfaceOptionsCheckButtonTemplate" );
	config.HideIconsOnMap = HideIconsOnMap;
	HideIconsOnMap.id = "HideIconsOnMap";
	HideIconsOnMap:SetPoint( "TOPLEFT", UseGlobalConfig,"BOTTOMLEFT" ,0, -16 );
	HideIconsOnMap:SetScript("onClick",config.ChangeState);
	HideIconsOnMap.Text:SetText( L.HIDE_ICONS_ON_MAP );

	local ScaleBossIcons = CreateFrame( "CheckButton", nil, config, "InterfaceOptionsCheckButtonTemplate" );
	config.ScaleBossIcons = ScaleBossIcons;
	ScaleBossIcons.id = "ScaleBossIcons";
	ScaleBossIcons:SetPoint( "TOPLEFT", HideIconsOnMap,"BOTTOMLEFT" ,0, -16 );
	ScaleBossIcons:SetScript("onClick",config.ChangeState);
	ScaleBossIcons.Text:SetText( L.SCALE_BOSS_ICONS );

	local ScaleBossIconsVal = CreateFrame( "Slider", "DJE_ScaleBossIconsVal", config, "OptionsSliderTemplate" );
	config.ScaleBossIconsVal = ScaleBossIconsVal;
	ScaleBossIconsVal.id = "ScaleBossIconsVal";
	ScaleBossIconsVal:SetPoint( "TOPLEFT", ScaleBossIcons,"BOTTOMLEFT" ,0, -16  );
	ScaleBossIconsVal:SetWidth(300)
	ScaleBossIconsVal:SetHeight(20)
	ScaleBossIconsVal:SetOrientation('HORIZONTAL');
	ScaleBossIconsVal:SetScript("OnValueChanged",config.ChangeSlider);
	ScaleBossIconsVal:SetMinMaxValues(0.01, 0.99) ;
	ScaleBossIconsVal:SetValueStep(0.01);

	_G[ScaleBossIconsVal:GetName().."Low" ]:SetText( '0.01' );
	_G[ScaleBossIconsVal:GetName().."High" ]:SetText( '0.99' );

	local MoveJournalFrame = CreateFrame( "CheckButton", nil, config, "InterfaceOptionsCheckButtonTemplate" );
	config.MoveJournalFrame = MoveJournalFrame;
	MoveJournalFrame.id = "MoveJournalFrame";
	MoveJournalFrame:SetPoint( "TOPLEFT", ScaleBossIconsVal,"BOTTOMLEFT" ,0, -16 );
	MoveJournalFrame:SetScript("onClick",config.ChangeState);
	MoveJournalFrame.Text:SetText( L.MOVING_CHECKBOX );


	local ScaleJournalFrame = config:CreateFontString( nil, "OVERLAY", "GameFontHighlight" );
	config.ScaleJournalFrame = ScaleJournalFrame;
	ScaleJournalFrame.id = "ScaleJournalFrame";
	ScaleJournalFrame:SetPoint( "TOPLEFT", MoveJournalFrame,"BOTTOMLEFT" ,0, -16 );

	ScaleJournalFrame:SetText( L.SCALE_JOURNAL_FRAME );

	local ScaleJournalFrameVal = CreateFrame( "Slider", "DJE_ScaleJournalFrameVal", config, "OptionsSliderTemplate" );
	config.ScaleJournalFrameVal = ScaleJournalFrameVal;
	ScaleJournalFrameVal.id = "ScaleJournalFrameVal";
	ScaleJournalFrameVal:SetPoint( "TOPLEFT", ScaleJournalFrame,"BOTTOMLEFT" ,0, -16  );
	ScaleJournalFrameVal:SetWidth(300)
	ScaleJournalFrameVal:SetHeight(20)
	ScaleJournalFrameVal:SetOrientation('HORIZONTAL');
	ScaleJournalFrameVal:SetScript("OnValueChanged",config.ChangeSlider);
	ScaleJournalFrameVal:SetMinMaxValues(0.1, 2.0) ;
	ScaleJournalFrameVal:SetValueStep(0.01);

	_G[ScaleJournalFrameVal:GetName().."Low" ]:SetText( '0.1' );
	_G[ScaleJournalFrameVal:GetName().."High" ]:SetText( '2.0' );



	InterfaceOptions_AddCategory(config);


	moduleconfig.name = "DJE "..L.CONFIG_MODULES;
	moduleconfig.parent = "Dungeon Journal Enhanced";
	moduleconfig:SetHeight(10);

	local ModuleWarn = moduleconfig:CreateFontString( nil, "OVERLAY", "GameFontHighlight" );
	moduleconfig.ModuleWarn = ModuleWarn;
	ModuleWarn:SetPoint("TOPLEFT", moduleconfig,"TOPLEFT", 16, -16)
	ModuleWarn:SetWidth(InterfaceOptionsFramePanelContainer:GetRight() - InterfaceOptionsFramePanelContainer:GetLeft() - 30);
	ModuleWarn:SetHeight(0);
	ModuleWarn:SetJustifyH("LEFT");
	ModuleWarn:SetText("|c00dfb802"..L.CONFIG_MODULES_HELP_CAPTION.."|r|n"..L.CONFIG_MODULES_HELP_TEXT);


	local modulebn = CreateFrame( "CheckButton", "DungeonJournalEnhanced_Module_bn", moduleconfig, "InterfaceOptionsCheckButtonTemplate" );
	moduleconfig.modulebn = modulebn;
	modulebn.id = "bn";
	modulebn:SetPoint( "TOPLEFT", ModuleWarn, "BOTTOMLEFT", 0, -16);
	modulebn:SetScript("onClick",moduleconfig.ChangeState);
	modulebn.Text:SetText( "|c00dfb802"..L.BOSSNOTES );

	local Modulbnexplain = moduleconfig:CreateFontString( nil, "OVERLAY", "GameFontHighlight" );
	moduleconfig.Modulbnexplain = Modulbnexplain;
	Modulbnexplain:SetPoint("TOPLEFT", modulebn,"TOPLEFT", 0, -16)
	Modulbnexplain:SetWidth(InterfaceOptionsFramePanelContainer:GetRight() - InterfaceOptionsFramePanelContainer:GetLeft() - 30);
	modulebn:SetHeight(Modulbnexplain:GetHeight() + 15);
	Modulbnexplain:SetJustifyH("LEFT");
	Modulbnexplain:SetText( L.CONFIG_MODULES_BN_EXPLAIN);

	InterfaceOptions_AddCategory(moduleconfig);


	if(not ns.pconf["version"] or not ns.gconf.cv) then
		config:SetDefault();
	else
		--config:SetDefault();

		config:SetCurrent();
	end



end

function moduleconfig:ChangeState()
	if(not ns.activeconfig.modules) then
		ns.activeconfig.modules = {};
	end
	ns.activeconfig.modules[self.id] = self:GetChecked();
end