-------------------------------------------------------------------------------
--
--  Mod Name : Blink Chat Header Configure
--  Author   : Blink
--  Date     : 2004/10/11
--  LastUpdate : 2006/09/04
--
-------------------------------------------------------------------------------

BCH_TempConfigs = {};

UIPanelWindows["BlinkChatHeadConfigure"] = { area = "center", pushable = 0, whileDead = 1 };

-- frame
function BlinkChatHeadConfigure_OnShow()
	BCH_TempConfigs = {};
	for ctype,head in pairs(BCH_DATA) do
		BCH_TempConfigs[ctype] = head;
	end
end

-- dropdown
function BlinkChatHeadConfigureDropDown_Initialize()
	local info,color;

	-- say
	info = {};
	color = ChatTypeInfo["SAY"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_SAY;
	info.value = "SAY";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- whisper
	info = {};
	color = ChatTypeInfo["WHISPER"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_WHISPER;
	info.value = "WHISPER";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- guild
	info = {};
	color = ChatTypeInfo["GUILD"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_GUILD;
	info.value = "GUILD";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- party
	info = {};
	color = ChatTypeInfo["PARTY"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_PARTY;
	info.value = "PARTY";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- yell
	info = {};
	color = ChatTypeInfo["YELL"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_YELL;
	info.value = "YELL";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- channel
	info = {};
	color = ChatTypeInfo["CHANNEL"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_CHANNEL;
	info.value = "CHANNEL";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- raid
	info = {};
	color = ChatTypeInfo["RAID"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_RAID;
	info.value = "RAID";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- raid warning
	info = {};
	color = ChatTypeInfo["RAID_WARNING"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_RAID_WARNING;
	info.value = "RAID_WARNING";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- raid leader
	info = {};
	color = ChatTypeInfo["RAID_LEADER"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_RAID_LEADER;
	info.value = "RAID_LEADER";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- battleground
	info = {};
	color = ChatTypeInfo["BATTLEGROUND"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_BATTLEGROUND;
	info.value = "BATTLEGROUND";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	-- battleground leader
	info = {};
	color = ChatTypeInfo["BATTLEGROUND_LEADER"];
	info.textR = color.r;
	info.textG = color.g;
	info.textB = color.b;
	info.text = BLINK_CHAT_HEAD_BATTLEGROUND_LEADER;
	info.value = "BATTLEGROUND_LEADER";
	info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
	UIDropDownMenu_AddButton(info);

	for i = 1, 10, 1 do
		local channelNum, channelName = GetChannelName(i);
		if ( channelNum > 0 and channelName ~= nil ) then
			if ( IsAddOnLoaded("oRA") and oRA_Core:GetOpt("channel") and oRA_Core:GetOpt("channel") == channelName )
				or ( IsAddOnLoaded("CT_RaidAssist") and CT_RA_Channel and CT_RA_Channel == channelName )
				or ( SW_SyncChanID and SW_SyncChanID > 0 and SW_SyncChanID == channelNum )
			then
				-- pass
			else
				local is_allowd_channel = false;
				for i=1, getn(BLINK_CHAT_HEAD_ALLOWED_CHANNELS), 1 do
					if( string.find(channelName,BLINK_CHAT_HEAD_ALLOWED_CHANNELS[i]) ) then
						is_allowd_channel = true;
					end
				end
				if ( not is_allowd_channel ) then
					-- custom channel
					info = {};
					color = ChatTypeInfo["CHANNEL"..channelNum];
					info.textR = color.r;
					info.textG = color.g;
					info.textB = color.b;
					info.text = "(사설채널) "..channelName;
					info.value = channelName;
					info.func = BlinkChatHeadConfigureDropDownButton_OnClick;
					UIDropDownMenu_AddButton(info);
				end
			end
		end
	end
end

function BlinkChatHeadConfigureDropDown_OnShow()
	UIDropDownMenu_SetWidth(150,BlinkChatHeadConfigureDropDown);
	UIDropDownMenu_Initialize(BlinkChatHeadConfigureDropDown, BlinkChatHeadConfigureDropDown_Initialize);
	UIDropDownMenu_SetSelectedValue(BlinkChatHeadConfigureDropDown, "SAY");
	BlinkChatHeadConfigure_RefreshEditBox();
end

function BlinkChatHeadConfigureDropDownButton_OnClick()
	local oldvalue = UIDropDownMenu_GetSelectedValue(BlinkChatHeadConfigureDropDown);
	if(oldvalue ~= this.value) then
		local editbox = getglobal("BlinkChatHeadConfigureEditBox");
		BCH_TempConfigs[oldvalue] = editbox:GetText();
		UIDropDownMenu_SetSelectedValue(BlinkChatHeadConfigureDropDown, this.value);
		BlinkChatHeadConfigure_RefreshEditBox();
	end
end

-- editbox
function BlinkChatHeadConfigureEditBox_EditBoxOnEnterPressed()
	BlinkChatHeadConfigureOkayButton_OnClick();
end

function BlinkChatHeadConfigureEditBox_EditBoxOnEscapePressed()
--	this:SetText("");
--	this:SetFocus();
	BlinkChatHeadConfigureCancelButton_OnClick();
end

function BlinkChatHeadConfigureEditBox_OnEditFocusLost()
--	local value = UIDropDownMenu_GetSelectedValue(BlinkChatHeadConfigureDropDown);
--	local editbox = getglobal("BlinkChatHeadConfigureEditBox");
--	BCH_TempConfigs[value] = this:GetText();
end

function BlinkChatHeadConfigure_RefreshEditBox()
	local value = UIDropDownMenu_GetSelectedValue(BlinkChatHeadConfigureDropDown);
	local editbox = getglobal("BlinkChatHeadConfigureEditBox");
	if ( BCH_TempConfigs[value] ) then
		editbox:SetText(BCH_TempConfigs[value]);
	elseif ( BCH_DATA[value] ) then
		editbox:SetText(BCH_DATA[value]);
	else
		editbox:SetText("");
	end
	editbox:SetFocus();
	editbox:HighlightText(0);
end

-- reset to default button
function BlinkChatHeadConfigureResetButton_OnClick()
	BlinkChatHeadConfigure_OnShow();
	local value = UIDropDownMenu_GetSelectedValue(BlinkChatHeadConfigureDropDown);
	local editbox = getglobal("BlinkChatHeadConfigureEditBox");
	if ( BCH_TempConfigs[value] ) then
		editbox:SetText(BCH_TempConfigs[value]);
	elseif ( BCH_DATA[value] ) then
		editbox:SetText(BCH_DATA[value]);
	else
		editbox:SetText("");
	end
	editbox:SetText("");
	editbox:SetFocus();
end

-- okay button
function BlinkChatHeadConfigureOkayButton_OnClick()
	local value = UIDropDownMenu_GetSelectedValue(BlinkChatHeadConfigureDropDown);
	local editbox = getglobal("BlinkChatHeadConfigureEditBox");
	BCH_TempConfigs[value] = editbox:GetText();
	--BCH_DATA = {};
	for ctype,head in pairs(BCH_TempConfigs) do
		ChatHead( ctype, head );
	end
	HideUIPanel(BlinkChatHeadConfigure);
end

-- cancel button
function BlinkChatHeadConfigureCancelButton_OnClick()
	HideUIPanel(BlinkChatHeadConfigure);
end
