--[[
HoloFriends addon created by Holo, continued by Zappster, followed by Andymon

Get the latest version at wow.curse.com

See HoloFriends_change.log for more informations  
]]


local HF_Options_Loaded = false;

-- define all the names of the options
local HF_OptionNames = {};
HF_OptionNames[1]  = "ShowClassIcons";
HF_OptionNames[2]  = "ShowClassColor";
HF_OptionNames[3]  = "ShowGroups";
HF_OptionNames[4]  = "MergeNotes";
HF_OptionNames[5]  = "NotesPriorityOn";
HF_OptionNames[6]  = "NotesPriorityOff";
HF_OptionNames[7]  = "MergeComments";
HF_OptionNames[8]  = "MarkRemove";
HF_OptionNames[9]  = "MenuMod";
HF_OptionNames[10] = "ShowDropdownBG";
HF_OptionNames[11] = "ShowDropdownAllBG";

-- define the defaults for all options
local HF_DefaultOptions = {};
HF_DefaultOptions[1]  = true;
HF_DefaultOptions[2]  = true;
HF_DefaultOptions[3]  = true;
HF_DefaultOptions[4]  = true; -- trigger for option 5 and 6
HF_DefaultOptions[5]  = false;
HF_DefaultOptions[6]  = false;
HF_DefaultOptions[7]  = true;
HF_DefaultOptions[8]  = true;
HF_DefaultOptions[9]  = true;
HF_DefaultOptions[10] = true;
HF_DefaultOptions[11] = false;

-- temporary options for the options pannel
local HF_TempOptions = {};
for index = 1, table.getn(HF_DefaultOptions) do
	HF_TempOptions[index] = HF_DefaultOptions[index];
end

-- copy the option strings to the strings array (numbered by the IDs)
local HF_OptionStrings = {};
HF_OptionStrings[1]  = HOLOFRIENDS_OPTIONS1SECTIONFLW;
HF_OptionStrings[2]  = HOLOFRIENDS_OPTIONS1SHOWCLASSICONS;
HF_OptionStrings[3]  = HOLOFRIENDS_OPTIONS1SHOWCLASSCOLOR;
HF_OptionStrings[4]  = HOLOFRIENDS_OPTIONS1SHOWGROUPS;
HF_OptionStrings[5]  = HOLOFRIENDS_OPTIONS2SECTIONNOTES;
HF_OptionStrings[6]  = HOLOFRIENDS_OPTIONS2MERGENOTES;
HF_OptionStrings[7]  = HOLOFRIENDS_OPTIONS2NOTESPRIORITYON;
HF_OptionStrings[8]  = HOLOFRIENDS_OPTIONS2NOTESPRIORITYOFF;
HF_OptionStrings[9]  = HOLOFRIENDS_OPTIONS3SECTIONSHARE;
HF_OptionStrings[10] = HOLOFRIENDS_OPTIONS3MERGECOMMENTS.."\n|cffffd200("..HOLOFRIENDS_OPTIONS0NEEDACCEPT..")|r";
HF_OptionStrings[11] = HOLOFRIENDS_OPTIONS3MARKREMOVE;
HF_OptionStrings[12] = HOLOFRIENDS_OPTIONS4SECTIONMENU;
HF_OptionStrings[13] = HOLOFRIENDS_OPTIONS4MENUMOD.."\n|cffffd200("..HOLOFRIENDS_OPTIONS0NEEDRELOAD..")|r";
HF_OptionStrings[14] = HOLOFRIENDS_OPTIONS4SHOWDROPDOWNBG;
HF_OptionStrings[15] = HOLOFRIENDS_OPTIONS4SHOWDROPDOWNALLBG;

-- copy the option tooltip strings to the strings array (numbered by the IDs)
local HF_ToolTipStrings = {};
HF_ToolTipStrings[1]  = "";
HF_ToolTipStrings[2]  = HOLOFRIENDS_OPTIONS1SHOWCLASSICONSTT;
HF_ToolTipStrings[3]  = HOLOFRIENDS_OPTIONS1SHOWCLASSCOLORTT;
HF_ToolTipStrings[4]  = HOLOFRIENDS_OPTIONS1SHOWGROUPSTT;
HF_ToolTipStrings[5]  = "";
HF_ToolTipStrings[6]  = HOLOFRIENDS_OPTIONS2MERGENOTESTT;
HF_ToolTipStrings[7]  = HOLOFRIENDS_OPTIONS2NOTESPRIORITYONTT;
HF_ToolTipStrings[8]  = HOLOFRIENDS_OPTIONS2NOTESPRIORITYOFFTT;
HF_ToolTipStrings[9]  = "";
HF_ToolTipStrings[10] = HOLOFRIENDS_OPTIONS3MERGECOMMENTSTT;
HF_ToolTipStrings[11] = HOLOFRIENDS_OPTIONS3MARKREMOVETT;
HF_ToolTipStrings[12] = "";
HF_ToolTipStrings[13] = HOLOFRIENDS_OPTIONS4MENUMODTT;
HF_ToolTipStrings[14] = HOLOFRIENDS_OPTIONS4SHOWDROPDOWNBGTT;
HF_ToolTipStrings[15] = HOLOFRIENDS_OPTIONS4SHOWDROPDOWNALLBGTT;

-- relate all the IDs to the options
local HF_OptionIDs = {};
local idx = 0;
for index = 1, table.getn(HF_ToolTipStrings) do
	if ( HF_ToolTipStrings[index] ~= "" ) then
		idx = idx + 1;
		HF_OptionIDs[index] = idx;
	else
		HF_OptionIDs[index] = 0;
	end
end

local HF_FAQStrings = {};
-- FAQ
HF_FAQStrings[1] = "|cffffd200"..HOLOFRIENDS_FAQ11QUESTION.."|r\n"..HOLOFRIENDS_FAQ12ANSWER;
HF_FAQStrings[2] = "|cffffd200"..HOLOFRIENDS_FAQ21QUESTION.."|r\n"..HOLOFRIENDS_FAQ22ANSWER;
HF_FAQStrings[3] = "|cffffd200"..HOLOFRIENDS_FAQ31QUESTION.."|r\n"..HOLOFRIENDS_FAQ32ANSWER;
HF_FAQStrings[4] = "|cffffd200"..HOLOFRIENDS_FAQ41QUESTION.."|r\n"..HOLOFRIENDS_FAQ42ANSWER;
HF_FAQStrings[5] = "|cffffd200"..HOLOFRIENDS_FAQ51QUESTION.."|r\n"..HOLOFRIENDS_FAQ52ANSWER;

-- feature list upended to the FAQ
idx = 6;
HF_FAQStrings[idx] = "|cffffd200"..HOLOFRIENDS_LISTFEATURES0TITLE.."|r";
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES11.." (|cffff0000"..NEW.."|r)";
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES12;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES13;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES14;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES15.." (|cffff0000"..NEW.."|r)";
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES16.." (|cffff0000"..NEW.."|r)";
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES17.." (|cffff0000"..NEW.."|r)";
idx = idx + 1;
HF_FAQStrings[idx] = "- "..HOLOFRIENDS_LISTFEATURES21;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES22;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES23;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES24;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES25.." (|cffff0000"..NEW.."|r)";
idx = idx + 1;
HF_FAQStrings[idx] = "- "..HOLOFRIENDS_LISTFEATURES31;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES32.." (|cffff0000"..NEW.."|r)";
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES33;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES34;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES35;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES36;
idx = idx + 1;
HF_FAQStrings[idx] = "- "..HOLOFRIENDS_LISTFEATURES41;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES42;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES43;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES44;
idx = idx + 1;
HF_FAQStrings[idx] = "- "..HOLOFRIENDS_LISTFEATURES51;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES52;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES53;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES54;
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES55.." (|cffff0000"..NEW.."|r)";
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES56.." (|cffff0000"..NEW.."|r)";
HF_FAQStrings[idx] = HF_FAQStrings[idx].."\n- "..HOLOFRIENDS_LISTFEATURES57.." (|cffff0000"..NEW.."|r)";

function HoloFriendsFAQ_OnLoad(self)
	self.name = HELP_LABEL;
	self.parent = HOLOFRIENDS_OPTIONS0LISTENTRY;
	InterfaceOptions_AddCategory(self);
end


-- executed if every text field in the FAQ frame is loaded
function HoloFriendsOptions_FAQ_OnLoad(self)
	local tname = self:GetName().."_Text";
	local text = HF_FAQStrings[self:GetID()];
	getglobal(tname):SetText(text);
end


function HoloFriendsOptions_OnLoad(self)
	self.name = HOLOFRIENDS_OPTIONS0LISTENTRY;
	self.okay = function (self) HoloFriendsOptions_Okay(); end;
	self.cancel = function (self) HoloFriendsOptions_Cancel(); end;
	self.default = function (self) HoloFriendsOptions_SetAllToDefault(); end;
	InterfaceOptions_AddCategory(self);
end


-- executet by HoloFriends_OnLoad
function HoloFriendsOptions_VariablesLoaded()
	-- initialize our SavedVariable
	if ( not HOLOFRIENDS_OPTIONS ) then 
	 	HOLOFRIENDS_OPTIONS = {}; 
	end

	-- load each option, set default if not there
	for index = 1, table.getn(HF_TempOptions) do
		if ( HOLOFRIENDS_OPTIONS[HF_OptionNames[index]] == nil ) then
			HOLOFRIENDS_OPTIONS[HF_OptionNames[index]] = HF_DefaultOptions[index];
		else
			HF_TempOptions[index] = HOLOFRIENDS_OPTIONS[HF_OptionNames[index]];
		end
	end

	-- record that we have been loaded
	HF_Options_Loaded = true;

	-- apply the options
	HoloFriendsOptions_Apply();
end


-- executed if every text field in the option frame is loaded
function HoloFriendsOptions_Text_OnLoad(self)
	local chapter = ( HF_ToolTipStrings[self:GetID()] == "" );
	local cname = self:GetName().."_Chapter";
	local tname = self:GetName().."_Text";
	local bname = self:GetName().."_Button";
	local text = HF_OptionStrings[self:GetID()];
	if ( chapter ) then
		getglobal(cname):SetText(text);
		getglobal(tname):Hide();
		getglobal(bname):Hide();
	else
		getglobal(cname):Hide();
		getglobal(tname):SetText(text);
	end
end


-- executed if the options window is shown
function HoloFriendsOptions_OnShow()
	-- make sure that our profile has been loaded
	if ( not HF_Options_Loaded ) then
		return;
	end

	local button;
	for index = 1, table.getn(HF_OptionIDs) do
		if ( HF_OptionIDs[index] > 0 ) then
			button = getglobal("HoloFriends_OptionsFrameScrollChild_ID"..index.."_Button");
			button:SetChecked(HF_TempOptions[HF_OptionIDs[index]]);
		end
	end

	if ( HF_TempOptions[4] ) then
		HoloFriends_OptionsFrameScrollChild_ID7_Button:Enable();
		HoloFriends_OptionsFrameScrollChild_ID7_Text:SetTextColor(1.0,1.0,1.0);
		HoloFriends_OptionsFrameScrollChild_ID8_Button:Disable();
		HoloFriends_OptionsFrameScrollChild_ID8_Text:SetTextColor(0.4,0.4,0.4);
	else
		HoloFriends_OptionsFrameScrollChild_ID7_Button:Disable();
		HoloFriends_OptionsFrameScrollChild_ID7_Text:SetTextColor(0.4,0.4,0.4);
		HoloFriends_OptionsFrameScrollChild_ID8_Button:Enable();
		HoloFriends_OptionsFrameScrollChild_ID8_Text:SetTextColor(1.0,1.0,1.0);
	end
end


-- executed from every option if the options window is changed
function HoloFriendsOptions_Button_OnUpdate(self)
	local chapter = ( HF_ToolTipStrings[self:GetID()] == "" );
	local text;
	if ( chapter ) then
		text = getglobal(self:GetName().."_Chapter");
	else
		text = getglobal(self:GetName().."_Text");
	end
	text:SetWidth(InterfaceOptionsFramePanelContainer:GetWidth()-65);
	local height = text:GetHeight();
	if ( math.max(20,height) == 20 ) then height = 20; end
	if ( chapter ) then height = height + 10; end
	self:SetHeight(height + 5);
end


-- executed if the mouse enter the check button
function HoloFriendsOptions_Button_OnEnter(self)
	local text = HF_ToolTipStrings[self:GetParent():GetID()];
	GameTooltip:SetOwner(self, "ANCHOR_TOPRIGHT");
	GameTooltip:SetText(text, 1.0, 1.0, 1.0, 1.0, 1);
end


-- executed if a check button is clicked
function HoloFriendsOptions_Button_OnClick(self)
	local index = HF_OptionIDs[self:GetParent():GetID()];
	if ( index ) then
		if(self:GetChecked()) then
			PlaySound("igMainMenuOptionCheckBoxOn");
			HF_TempOptions[index] = true;
			if ( index == 4 ) then
				HoloFriends_OptionsFrameScrollChild_ID7_Button:Enable();
				HoloFriends_OptionsFrameScrollChild_ID7_Text:SetTextColor(1.0,1.0,1.0);
				HoloFriends_OptionsFrameScrollChild_ID8_Button:Disable();
				HoloFriends_OptionsFrameScrollChild_ID8_Text:SetTextColor(0.4,0.4,0.4);
			end
		else
			HF_TempOptions[index] = false;
			if ( index == 4 ) then
				HoloFriends_OptionsFrameScrollChild_ID7_Button:Disable();
				HoloFriends_OptionsFrameScrollChild_ID7_Text:SetTextColor(0.4,0.4,0.4);
				HoloFriends_OptionsFrameScrollChild_ID8_Button:Enable();
				HoloFriends_OptionsFrameScrollChild_ID8_Text:SetTextColor(1.0,1.0,1.0);
			end
		end

	end
end


-- executed if Okay-Button is pressed
function HoloFriendsOptions_Okay()
	-- make sure that our profile has been loaded
	if ( not HF_Options_Loaded ) then
		return;
	end

	for index = 1, table.getn(HF_TempOptions) do
		HOLOFRIENDS_OPTIONS[HF_OptionNames[index]] = HF_TempOptions[index];
	end

	HoloFriendsOptions_Apply();
end


-- executed if Cancel-Button is pressed
function HoloFriendsOptions_Cancel()
	-- make sure that our profile has been loaded
	if ( not HF_Options_Loaded ) then
		return;
	end

	for index = 1, table.getn(HF_TempOptions) do
		HF_TempOptions[index] = HOLOFRIENDS_OPTIONS[HF_OptionNames[index]];
	end
end


-- reset all options to defaults (Reset-Button pressed)
function HoloFriendsOptions_SetAllToDefault()
	-- make sure that our profile has been loaded
	if ( not HF_Options_Loaded ) then
		return;
	end

	-- set our profile to defaults
	for index = 1, table.getn(HF_TempOptions) do
		HF_TempOptions[index] = HF_DefaultOptions[index];
	end

	HoloFriendsOptions_OnShow()
end


-- apply the options
function HoloFriendsOptions_Apply()
	-- make sure that our profile has been loaded
	if ( not HF_Options_Loaded ) then
		return;
	end

	if ( HOLOFRIENDS_OPTIONS.ShowDropdownAllBG ) then
		HoloFriends_DropDownBG_Show();
	else
		HoloFriends_DropDownBG_Hide();
	end

	if ( HOLOFRIENDS_OPTIONS.MergeNotes ) then
		HOLOFRIENDS_OPTIONS.NotesPriority = HOLOFRIENDS_OPTIONS.NotesPriorityOn;
	else
		HOLOFRIENDS_OPTIONS.NotesPriority = HOLOFRIENDS_OPTIONS.NotesPriorityOff;
	end

	HoloFriends_List_Update();
end
