-- Blizzard Registrations
UIPanelWindows["CosmosMasterFrame"] =   { area = "center",      pushable = 0};
StaticPopupDialogs["CONFIRM_COSMOS_RESET"] = {
	text = COSMOS_RESET_CONFIRM_TEXT,
	button1 = YES,
	button2 = NO,
	OnAccept = function()
		CosmosMaster_CVars = { };
		PlaySound("igMainMenuOption");
		StaticPopup_Show("NOTICE_COSMOS_RECONNECT");
	end,
	timeout = 0,
	whileDead = 1,
	hideOnEscape = 1
};

StaticPopupDialogs["NOTICE_COSMOS_RECONNECT"] = {
	text = COSMOS_NOTICE_RECONNECT_TEXT,
	button1 = OKAY,
	timeout = 0,
	whileDead = 1,
	hideOnEscape = 1
};

local COSMOSMASTER_DISPLAY_LIMIT = 10;

-- Displays description text.
function CosmosMaster_SetSectionInfo( index )
	local realindex = index + FauxScrollFrame_GetOffset(CosmosMasterSectionScrollFrame);

	-- Make sure its a valid index.
	if ( realindex > CosmosMaster_GetSectCount() ) then return; end

	local config = CosmosMaster_GetOffsetSection(realindex);
	if (not config) then return; end
	local descriptiontext = config[CSM_DESCRIPTION];

	if ( realindex == 0 ) then
		Sea.io.error ("Invalid Cosmos Index: 0 ");
	else
		local config = CosmosMaster_GetOffsetSection(realindex);
		local descriptiontext = config[CSM_DESCRIPTION];

		if ( not (descriptiontext == "") ) then
			CosmosMaster_ClearInfo();
			CosmosMasterTextbox:AddMessage("\n"..descriptiontext.."\n ", 156/256, 212/256, 1.0 );
		end
	end
end

function CosmosMaster_Button (relativeindex)
	if ( not (CosmosMasterFrame_IsLoading==1) ) then
		local index = relativeindex;
		local funcOffset = FauxScrollFrame_GetOffset(CosmosMasterScrollFrame);
		local value = CosmosMaster_GetOffsetValue(index+funcOffset); -- change to + offset

		-- Call the referring function
		local callback = value[CSM_HANDLER];
		local setval = value[CSM_SLIDERVALUE];
		local checked = value[CSM_CHECKONOFF];
		if (setval == nil) then setval=1; end
		callback(checked, setval);
	end
end

-- Update callback
function CosmosMaster_Update()
	CosmosMaster_DrawData();
end

-- Loading function
function CosmosMasterFrame_Show()
	CosmosMasterFrame_IsLoading = true;
	CosmosMaster_DrawData();
	CosmosMasterFrame_IsLoading = false;
end

-- Draws the boxes data
function CosmosMaster_DrawData()
	local sectCount = CosmosMaster_GetSectCount();
	FauxScrollFrame_Update(CosmosMasterSectionScrollFrame, sectCount, 18, 32 );
	local sectOffset = FauxScrollFrame_GetOffset(CosmosMasterSectionScrollFrame);
	for index=1, 18, 1 do
		local button = getglobal("CosmosSection"..index);
		local section = CosmosMaster_GetOffsetSection(index+sectOffset);

		if (section) then
			CosmosMaster_SectionSetup(button, true, section);
		else
			CosmosMaster_SectionSetup(button);
		end
	end

	local confCount = CosmosMaster_GetConfCount();
	FauxScrollFrame_Update(CosmosMasterScrollFrame, confCount, 10, 32 );
	
	local funcOffset = FauxScrollFrame_GetOffset(CosmosMasterScrollFrame);
	local limit = COSMOSMASTER_DISPLAY_LIMIT;

	if ( CosmosMaster_DrawDataStarted == 1 ) then return;
	else CosmosMaster_DrawDataStarted = 1; end

	CosmosMaster_HideAll();

	-- Update all boxes from the configuration
	for index=1, 10, 1 do
		if ( index <= 10 and funcOffset >= 0 and index < getn(CosmosMaster_Configurations)) then
			-- Acquire the value object
			local value = CosmosMaster_GetOffsetValue(index+funcOffset);
			index = index;
			if ( value == nil ) then break; end

			-- Update checkboxes
			local checkbox = getglobal("Cosmos"..index.."Checkbox");
			local checkboxtext = getglobal("Cosmos"..index.."FrameText");
			local chatbox = getglobal("DEFAULT_CHAT_FRAMEEditBox");

			if(  value[CSM_TYPE] == "CHECKBOX" or value[CSM_TYPE] == "BOTH"
			  or value[CSM_TYPE] == "SLIDER"   or value[CSM_TYPE] == "BUTTON") then
				if ( value[CSM_STRING] == null ) then
					checkboxtext:Hide();
				else
					checkboxtext:SetText(value[CSM_STRING]);
					checkboxtext:Show();
				end
			end

			if ( value[CSM_TYPE] == "CHECKBOX" or value[CSM_TYPE] == "BOTH" ) then
				checkbox:Show();

				if ( value[CSM_CHECKONOFF] == 1)	then
					checkbox:SetChecked(1);
				else
					checkbox:SetChecked(0);
				end
			end

			-- Update sliders

			if ( value[CSM_TYPE] == "SLIDER" or value[CSM_TYPE] == "BOTH" ) then
				local slider = getglobal ("Cosmos"..index.."Slider");
				local slidertext = getglobal("Cosmos"..index.."Slider".."Text");
				local slidervaluetext = getglobal("Cosmos"..index.."Slider".."ValueText");

				if ( value[CSM_SLIDERVALUETEXTTOGGLE] == 1 ) then
					local valuetext = floor(value[CSM_SLIDERVALUE]*value[CSM_SLIDERVALUETEXTMULTIPLIER]*100+.5)/100;

					valuetext = valuetext..value[CSM_SLIDERTEXTAPPEND];

					slidervaluetext:SetText ( valuetext );
					slidervaluetext:Show();
				else
					slidervaluetext:Hide();
				end
				slidertext:SetText ( value[CSM_SLIDERSTRING] );
				slidertext:Show();
				local slidervalue =  value[CSM_SLIDERVALUE];
				slider:SetMinMaxValues( value[CSM_SLIDERMIN], value[CSM_SLIDERMAX] );
				slider:SetValueStep( value[CSM_SLIDERSTEP] );
				slider:SetValue( slidervalue );
				slider:Show();
			end

			-- Update Separators

			if ( value[CSM_TYPE] == "SEPARATOR" ) then
				local separator = getglobal("Cosmos"..index.."Separator");
				local separatortext = getglobal("Cosmos"..index.."Separator".."Text");

				if ( value[CSM_STRING] == nil ) then
					separatortext:Hide();
					separator:Show();
				else
					separatortext:SetText ( value[CSM_STRING] );
					separatortext:Show();
					separator:Show();
				end

				checkbox:Hide();
			end

			-- Update Buttons
			if ( value[CSM_TYPE] == "BUTTON" ) then
				local button = getglobal("Cosmos"..index.."Button");

				if ( value[CSM_SLIDERSTRING] == nil ) then
					button:SetText("Options");
				else
					button:SetText ( value[CSM_SLIDERSTRING] );
				end
				button:Show();
			end
		end
	end	

	CosmosMasterFrame_IsLoading = false;
	CosmosMaster_DrawDataStarted = 0;

end

--[[ Handles Checkbox Events ]]--
function CosmosMaster_CheckBox (relativeindex, checkedvalue )
	if ( not CosmosMasterFrame_IsLoading ) then
		local index = relativeindex;
		local funcOffset = FauxScrollFrame_GetOffset(CosmosMasterScrollFrame);
		index = index + funcOffset;
		local value = CosmosMaster_GetOffsetValue(index);

		local checkbox = getglobal("Cosmos"..index.."Checkbox");
		local checkboxtext = getglobal("Cosmos"..index.."FrameText");

		if ( value[CSM_TYPE] == "CHECKBOX") then
			value[CSM_CHECKONOFF] = checkedvalue;
		else
			if ( value[CSM_TYPE] == "BOTH" ) then
				value[CSM_CHECKONOFF] = checkedvalue;
			else
				value[CSM_CHECKONOFF] = 1;
			end
		end

		-- Call the referring function
		local callback = value[CSM_HANDLER];
		local setval = (0);
		if ( value[CSM_SLIDERVALUE] ) then 
			setval = value[CSM_SLIDERVALUE];			
		end
		callback(value[CSM_CHECKONOFF], setval);

	end
end

-- Helper function to set up a section button
function CosmosMaster_SectionSetup(button, visible, section)
	local normalTexture = getglobal(button:GetName().."NormalTexture");

	if (section) then
		if (section[CSM_STRING]) then
			button:SetText(section[CSM_STRING]);
		elseif (section[CSM_SECTION]) then
			button:SetText(section[CSM_SECTION]);
		else
			button:SetText("Missing section string.");
		end

		if (section[CSM_VARIABLE] == CosmosMaster_Section) then
			button:LockHighlight();
		else
			button:UnlockHighlight();
		end
	end
	
	if (visible) then
		if (not button:IsVisible()) then
			button:Show();
		end
	else
		if (button:IsVisible()) then
			button:Hide();
		end
	end
	
	if (CosmosMaster_GetSectCount() <= 18) then
		button:SetWidth(160);
	else
		button:SetWidth(132);
	end
end

-- Displays description text.
function CosmosMaster_SetInfo( index )
	local realindex = index + FauxScrollFrame_GetOffset(CosmosMasterScrollFrame);

	-- Make sure its a valid index.
	if ( realindex > getn ( CosmosMaster_Configurations ) ) then return; end

	local config = CosmosMaster_GetOffsetValue(realindex);
	if (not config) then return; end
	local descriptiontext = config[CSM_DESCRIPTION];

	if ( realindex == 0 ) then 
		Sea.io.error ("Invalid Cosmos Index: 0 ");
	else 
		local config = CosmosMaster_GetOffsetValue(realindex);
		local descriptiontext = config[CSM_DESCRIPTION];

		if ( not (descriptiontext == "") ) then
			CosmosMaster_ClearInfo();
			CosmosMasterTextbox:AddMessage("\n"..descriptiontext.."\n ", 156/256, 212/256, 1.0 );
		end
	end
end

-- Switches sections
function CosmosMaster_Section_OnClick(index)
	if ( not (CosmosMasterFrame_IsLoading == 1) and (CosmosMaster_DrawDataStarted==0) ) then
		local sectOffset = FauxScrollFrame_GetOffset(CosmosMasterSectionScrollFrame);
		local sectionButton = getglobal ("CosmosSection"..index);
		local section = CosmosMaster_GetOffsetSection(index+sectOffset); -- change to + offset
		if (section[CSM_VARIABLE] and (section[CSM_VARIABLE] ~= CosmosMaster_Section)) then
			CosmosMaster_Section = section[CSM_VARIABLE];
			CosmosMaster_DrawData();
		end
	end
end

-- Updates the value when a slider changes...
function CosmosMaster_Slider (relativeindex, slidervalue )

	if ( not (CosmosMasterFrame_IsLoading == 1) and (CosmosMaster_DrawDataStarted==0) ) then
			local append = "";
			local index = relativeindex;
			local funcOffset = FauxScrollFrame_GetOffset(CosmosMasterScrollFrame);
			local value = CosmosMaster_GetOffsetValue(index+funcOffset); -- change to + offset
			local slider = getglobal ("Cosmos"..index.."Slider");
			local slidervaluetext = getglobal("Cosmos"..index.."Slider".."ValueText");

			-- This makes sure that the correct type is being used before setting the value
			if ( value[CSM_TYPE] == "SLIDER" or value[CSM_TYPE] == "BOTH" ) then

				-- Rounds off value to 2 Decimal places
				local newvalue = slidervalue;
				newvalue = floor(newvalue * 100+.5)/100;

    				value[CSM_SLIDERVALUE] = newvalue;

				if ( value[CSM_SLIDERVALUETEXTTOGGLE] == 1 ) then
					-- Show only 2 decimal places of accuracy in text
					local valuetext = floor(value[CSM_SLIDERVALUE]*value[CSM_SLIDERVALUETEXTMULTIPLIER]*100+.5)/100;
					valuetext = valuetext..value[CSM_SLIDERTEXTAPPEND];

					slidervaluetext:SetText ( valuetext );
					slidervaluetext:Show();
				else
					slidervaluetext:Hide();

				end

			slidervalue =  value[CSM_SLIDERVALUE];
				
				-- Call the referring function
				local callback = value[CSM_HANDLER];
				callback(value[CSM_CHECKONOFF],slidervalue);
			end
	end

end

function CosmosMaster_OnOkayClick()
	PlaySound("gsTitleOptionOK");
	CosmosMaster_Save();
	HideUIPanel(CosmosMasterFrame);
end

function CosmosMaster_ClearInfo()
	CosmosMasterTextbox:AddMessage("\n\n\n\n");
end

--[[  Hides all gui items ]]--
function CosmosMaster_HideAll() 
	-- Reset all boxes.
	for index = 1, COSMOSMASTER_DISPLAY_LIMIT, 1 do
			local checkbox = getglobal("Cosmos"..index.."Checkbox");
			local checkboxtext = getglobal("Cosmos"..index.."FrameText");
			local slider = getglobal ("Cosmos"..index.."Slider");
			local slidertext = getglobal("Cosmos"..index.."Slider".."Text");
			local slidervaluetext = getglobal("Cosmos"..index.."Slider".."ValueText");
			local button = getglobal("Cosmos"..index.."Button");
			local separator = getglobal("Cosmos"..index.."Separator");

			checkbox:SetChecked(0);
			checkbox:Hide();
			checkboxtext:Hide();
			checkboxtext:SetText("Reset");
			slider:Hide();
			slidertext:Hide();
			slidervaluetext:Hide();
			button:SetText("");
			button:Hide();
			separator:Hide();
	end
end