--[[
 The Original Cosmos UI Configuration values
 
 This is the simplest example of how CosmosMaster can be used. 
 Simply include this lua file in CosmosIncludes.xml file,
 then register it under the proper event handlers.
 
 
 Original code by Justin Crites (Xiphoris)
 Registration design by Alex Brazie (AlexanderYoshi)
]]--

function ToggleCosmosFeatureFrame()
	local loaded = LoadAddOn("CosmosMenu");
	if ( loaded ) then
		if (CosmosFeatureFrame:IsVisible()) then
			HideUIPanel(CosmosFeatureFrame);
		else
			ShowUIPanel(CosmosFeatureFrame);
		end
	else
		Sea.io.print (COSMOS_BUTTON_NOTLOADED);
	end
end

Cosmos_TurnOnOpenAllBags = 1;

function Cosmos_ToggleOpenAllBags(value)
	if ( ( value == 1 ) or ( value == true ) ) then 
		Cosmos_TurnOnOpenAllBags = 1;
	else
		Cosmos_TurnOnOpenAllBags = 0;
	end
end

-- Adds ALT Invites to WhoLinks
function Cosmos_SetItemRef (link, text, button) 
-- Begin Wholink mod code

	if (WhoLink_ProcessLink) then
		local linkIsWhoLink = WhoLink_ProcessLink(link, button);
		if (linkIsWhoLink) then
			return;
		end
	end

-- End Wholink mod code
	if ( strsub(link, 1, 6) == "player" ) then
		local namelink = strsub(link, 8);
		local name, lineid = strsplit(":", namelink);
		if ( name and (strlen(name) > 0) ) then
			if ( IsModifiedClick("CHATLINK") ) then
				local staticPopup;
				staticPopup = StaticPopup_Visible("ADD_IGNORE");
				if ( staticPopup ) then
					-- If add ignore dialog is up then enter the name into the editbox
					getglobal(staticPopup.."EditBox"):SetText(name);
					return;
				end
				staticPopup = StaticPopup_Visible("ADD_MUTE");
				if ( staticPopup ) then
					-- If add ignore dialog is up then enter the name into the editbox
					getglobal(staticPopup.."EditBox"):SetText(name);
					return;
				end
				staticPopup = StaticPopup_Visible("ADD_FRIEND");
				if ( staticPopup ) then
					-- If add ignore dialog is up then enter the name into the editbox
					getglobal(staticPopup.."EditBox"):SetText(name);
					return;
				end
				staticPopup = StaticPopup_Visible("ADD_GUILDMEMBER");
				if ( staticPopup ) then
					-- If add ignore dialog is up then enter the name into the editbox
					getglobal(staticPopup.."EditBox"):SetText(name);
					return;
				end
				staticPopup = StaticPopup_Visible("ADD_TEAMMEMBER");
				if ( staticPopup ) then
					-- If add ignore dialog is up then enter the name into the editbox
					getglobal(staticPopup.."EditBox"):SetText(name);
					return;
				end
				staticPopup = StaticPopup_Visible("ADD_RAIDMEMBER");
				if ( staticPopup ) then
					-- If add ignore dialog is up then enter the name into the editbox
					getglobal(staticPopup.."EditBox"):SetText(name);
					return;
				end
				staticPopup = StaticPopup_Visible("CHANNEL_INVITE");
				if ( staticPopup ) then
					getglobal(staticPopup.."EditBox"):SetText(name);
					return;
				end
				if ( ChatFrameEditBox:IsVisible() ) then
					ChatFrameEditBox:Insert(name);
				else
					SendWho("n-"..name);
				end
				
			elseif ( button == "RightButton" ) then
				FriendsFrame_ShowDropdown(name, 1, lineid);
			else
				ChatFrame_SendTell(name);
			end
		end
		return;
	end
	return true;
end
Sea.util.hook("SetItemRef", "Cosmos_SetItemRef", "hide");

function Cosmos_ShiftSell(toggle)
	if ( toggle ~= 0 ) then
		Cosmos_ShiftToSell = true;
	else
		Cosmos_ShiftToSell = false;
	end
end

-- Quest Scroll speed
function Cosmos_SetQuestTextScrollSpeed(toggle, speed)
	if ( speed == 1 ) then
		speed = 400;
	end
	if ( toggle == 1 ) then 
		setglobal('QUEST_DESCRIPTION_GRADIENT_CPS',speed);
		Cosmos_SetCVar("COS_COS_QUESTSCROLL", speed);
	else
		setglobal('QUEST_DESCRIPTION_GRADIENT_CPS',40);
		Cosmos_SetCVar("COS_COS_QUESTSCROLL", 40);
	end
end

-- Cosmos START
function PartyMemberFrame_UpdateCosmos(type, info, arg1, arg2, arg3, arg4)
	--[[
	if ( type == "COSMOS_PARTY" ) then
		Sea.io.print ( "PMF_TYPE: '"..type.."'");
		Sea.io.print ( "PMF_UC1: '"..arg1.."'");
		Sea.io.print ( "PMF_UC2: '"..arg2.."'");
	end
	]]--
	if (arg1 == "<UC>" or arg1 == '<CL>') then -- UC : Use Cosmos
		for i = 1, MAX_PARTY_MEMBERS, 1 do
			if ( getglobal("PartyMemberFrame"..i.."CosmosIcon") ) then
				if (UnitName("party"..i) == arg2) then
					getglobal("PartyMemberFrame"..i.."CosmosIcon"):Show();
				end
			end
		end
		if ( CosmosMaster_CosmosUsers ) then
			CosmosMaster_CosmosUsers[arg2] = GetTime();
		end
	elseif (arg1 == "<AC>") then
		Cosmos_SendPartyMessage("<UC>");
	end

	return 1;
end
-- Cosmos END

function Cosmos_ResetPartyFrames()
	local lastFrame = PartyMemberFrame1;
	lastFrame:ClearAllPoints();
	lastFrame:SetPoint("TOPLEFT", "UIParent", "TOPLEFT", 10, -128);
	local newFrame = nil;
	for i = 2, MAX_PARTY_MEMBERS do
		newFrame = getglobal(string.format("PartyMemberFrame%d", i));
		if ( newFrame ) then
			newFrame:ClearAllPoints();
			newFrame:SetPoint("TOPLEFT", lastFrame:GetName(), "BOTTOMLEFT", 0, -10);
		end
		lastFrame = newFrame;
	end
end

function Cosmos_Registers()
	-- Register with the CosmosMaster
	Cosmos_RegisterConfiguration(
		"COS_COS",
		"SECTION",
		TEXT(COSMOS_CONFIG_SEP),
		TEXT(COSMOS_CONFIG_SEP_INFO)
		);
 	Cosmos_RegisterConfiguration(
		"COS_COS_DEFAULTSEPARATOR",
		"SEPARATOR",
		TEXT(COSMOS_CONFIG_SEP),
		TEXT(COSMOS_CONFIG_SEP_INFO)
		);
	Cosmos_RegisterConfiguration(
		"COS_COS_QUESTSCROLL",
		"BOTH",
		COSMOS_CONFIG_QUESTSCROLL,
		COSMOS_CONFIG_QUESTSCROLL_INFO,
		Cosmos_SetQuestTextScrollSpeed,
		1,
 		400,										 --Default Value
 		40,										 --Min value
 		400,										 --Max value
 		COSMOS_CONFIG_QUESTSCROLL_CHARS,	--Slider Text
 		20,									 --Slider Increment
 		1,										 --Slider state text on/off
 		COSMOS_CONFIG_QUESTSCROLL_APPEND,			--Slider state text append
 		1										 --Slider state text multiplier
		);
		
	if ( OpenAllBags_InitLoaded == true ) then
		Cosmos_RegisterConfiguration(
			"COS_COS_OPENALLBAGS",
			"CHECKBOX",
			TEXT(COSMOS_CONFIG_OPENALLBAGS),
			TEXT(COSMOS_CONFIG_OPENALLBAGS_INFO),
			Cosmos_ToggleOpenAllBags,
			1
		);
	end
	Cosmos_RegisterConfiguration(
		"COS_COS_RESET_PARTY_FRAMES",
		"BUTTON",
		TEXT(COSMOS_CONFIG_RESET_PARTY_FRAMES),
		TEXT(COSMOS_CONFIG_RESET_PARTY_FRAMES_INFO),
		Cosmos_ResetPartyFrames,
		0,
		0,
		0,
		0,
		TEXT(COSMOS_CONFIG_RESET_PARTY_FRAMES_TEXT)
		);

	-- Cosmos Configuration Button
	Cosmos_RegisterButton ( 
		COSMOS_BUTTON_COSMOS, 
		COSMOS_BUTTON_COSMOS_SUB, 
		COSMOS_BUTTON_COSMOS_TIP, 
		"Interface\\Icons\\Ability_Repair", 
		function()
			if (CosmosMasterFrame:IsVisible()) then 
				HideUIPanel(CosmosMasterFrame);
			else 
				PlaySound("igMainMenuOption");
				ShowUIPanel(CosmosMasterFrame);
				CosmosMasterFrame_Show();
			end
		end
		);
	-- Cosmos Reset Configuration Button
	Cosmos_RegisterButton ( 
		COSMOS_BUTTON_COSMOS_RESET, 
		COSMOS_BUTTON_COSMOS_RESET_SUB, 
		COSMOS_BUTTON_COSMOS_RESET_TIP, 
		"Interface\\Icons\\Ability_FiegnDead", 
		function()
			PlaySound("igMainMenuOption");
			StaticPopup_Show("CONFIRM_COSMOS_RESET");
		end
		);
end
