local dewdrop = DewdropLib:GetInstance('1.0');
local metro = Metrognome:GetInstance('1');
local tablet = TabletLib:GetInstance('1.0');

local label = "";
local queuetext = "";

FuBar_WhisperCastFu = FuBarPlugin:GetInstance("1.2"):new({
	name		= "FuBar - WhisperCastFu",
	description	= "WhisperCast Plugin for FuBar",
	version		= "1.2.05",
	releaseDate	= "2006-07-28",
	aceCompatible	= 103,
	author		= "HiroCT",
	email		= "hiro.ct@gmail.com",
	website		= "http://www.wowinterface.com/downloads/info5019-FuBar_-_WhisperCastFu.html",
	category	= "interface",
	db		= AceDatabase:new("FuBar_WhisperCastFuDB"),
	defaults	= {
		hideLabel = false,
		hideHint = false,
	},
	hasIcon		= true,
});

function FuBar_WhisperCastFu:IsHideLabel()
	return self.data.hideLabel;
end

function FuBar_WhisperCastFu:ToggleHideLabel()
	self.data.hideLabel = not self.data.hideLabel;
	self:Update();
end

function FuBar_WhisperCastFu:IsHideHint()
	return self.data.hideHint;
end

function FuBar_WhisperCastFu:ToggleHideHint()
	self.data.hideHint = not self.data.hideHint;
	self:Update();
end

function FuBar_WhisperCastFu:Enable()
	metro:Register(self.name, self.Update, 1, self);
	metro:Start(self.name);
end

function FuBar_WhisperCastFu:Disable()
	metro:Unregister(self.name);
end

function FuBar_WhisperCastFu:MenuSettings(level, value)
	if (level == 1) then
		dewdrop:AddLine(
			"text", "WhisperCast Options",
			"hasArrow", TRUE,
			"value", "wcoptions"
		);
		dewdrop:AddLine(
			"text", ""
		);
		dewdrop:AddLine(
			"text", "Hide Label",
			"arg1", self,
			"func", "ToggleHideLabel",
			"checked", self:IsHideLabel()
		);
		dewdrop:AddLine(
			"text", "Hide Tooltip Hint",
			"arg1", self,
			"func", "ToggleHideHint",
			"checked", self:IsHideHint()
		);
	elseif (level == 2) then
		if (value == "wcoptions") then
			dewdrop:AddLine(
				"text", format(WCLocale.UI.text.whisperCastVersion, WhisperCast_Version),
				"isTitle", TRUE
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownEnable,
				"arg1", self,
				"func", "ToggleEnabled",
				"checked", WhisperCast_getChecked(WhisperCast_Profile.enable)
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownGroupOnly,
				"arg1", self,
				"func", "ToggleGroupOnly",
				"checked", WhisperCast_getChecked(WhisperCast_Profile.grouponly)
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownCombatOnly,
				"arg1", self,
				"func", "ToggleRestrictCombat",
				"checked", not WhisperCast_getChecked(WhisperCast_Profile.restrictcombat)
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownHideWhispers,
				"arg1", self,
				"func", "ToggleHideWhispers",
				"checked", WhisperCast_getChecked(WhisperCast_Profile.hidewhispers)
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownFeedbackWhispers,
				"arg1", self,
				"func", "ToggleFeedbackWhispers",
				"checked", WhisperCast_getChecked(WhisperCast_Profile.feedbackwhisper)
			);
			dewdrop:AddLine(
				"text", ""
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownSoundSub,
				"hasArrow", TRUE,
				"value", "sound"
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownMatchingSub,
				"hasArrow", TRUE,
				"value", "matching"
			);
		end
	elseif (level == 3) then
		if (value == "sound") then
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownSoundFirstQueue,
				"arg1", self,
				"func", "ToggleSoundFirstQueue",
				"checked", WhisperCast_getChecked(WhisperCast_Profile.playsound.firstqueue)
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownSoundQueueEmpty,
				"arg1", self,
				"func", "ToggleSoundQueueEmpty",
				"checked", WhisperCast_getChecked(WhisperCast_Profile.playsound.emptyqueue)
			);
		elseif (value == "matching") then
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownMatchingExact,
				"arg1", self,
				"func", "ToggleMatchingExact",
				"checked", (WhisperCast_Profile.match == "exact")
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownMatchingStart,
				"arg1", self,
				"func", "ToggleMatchingStart",
				"checked", (WhisperCast_Profile.match == "start")
			);
			dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownMatchingAny,
				"arg1", self,
				"func", "ToggleMatchingAny",
				"checked", (WhisperCast_Profile.match == "any")
			);
		end
	end
end

					-- This is just some code from WhisperCast's menu system (need to convert this to Dewdrop)
					--for spell,attrib in WhisperCast_Spells do
						--info = { };
						--info.text = spell;
						--info.value = spell;
						--info.keepShownOnClick = 1;
						--info.checked = WhisperCast_Profile.disabledSpells[spell];
						--info.func = WhisperCast_ToggleSpellDisable;
						--UIDropDownMenu_AddButton(info, UIDROPDOWNMENU_MENU_LEVEL);
					--end

function FuBar_WhisperCastFu:ToggleEnabled()
	WhisperCast_ToggleEnabled();
end

function FuBar_WhisperCastFu:ToggleGroupOnly()
	WhisperCast_ToggleProfileKey("grouponly");
end

function FuBar_WhisperCastFu:ToggleRestrictCombat()
	WhisperCast_ToggleRestrictCombat();
end

function FuBar_WhisperCastFu:ToggleHideWhispers()
	WhisperCast_ToggleProfileKey("hidewhispers");
end

function FuBar_WhisperCastFu:ToggleFeedbackWhispers()
	WhisperCast_ToggleProfileKey("feedbackwhisper");
end

function FuBar_WhisperCastFu:ToggleSoundFirstQueue()
	WhisperCast_ToggleProfileKey({"playsound","firstqueue"});
end

function FuBar_WhisperCastFu:ToggleSoundQueueEmpty()
	WhisperCast_ToggleProfileKey({"playsound","emptyqueue"});
end

function FuBar_WhisperCastFu:ToggleMatchingExact()
	WhisperCast_SetMatch("exact");
end

function FuBar_WhisperCastFu:ToggleMatchingStart()
	WhisperCast_SetMatch("start");
end

function FuBar_WhisperCastFu:ToggleMatchingAny()
	WhisperCast_SetMatch("any");
end

function FuBar_WhisperCastFu:ToggleSpellDisable(spellname)
	if (spellname == nil) then
		DEFAULT_CHAT_FRAME:AddMessage("nil");
	else
		DEFAULT_CHAT_FRAME:AddMessage(spellname);
	end
	--WhisperCast_ToggleSpellDisable(value);	-- commented out so i don't keep breaking whispercast, the above check should be sufficieint as long as it returns the correct spell name and only once
end

function FuBar_WhisperCastFu:UpdateText()
	if (WhisperCast_Spells) then
		queuetext = "|cffffffff"..WhisperCast_Runtime.queueBrief.."|r";
	else
		queuetext = "|cffffffff"..WCLocale.UI.text.queueBriefUnavailable.."|r";
	end

	if (self:IsHideLabel()) then
		label = "";
		queuetext = " "..queuetext;
	else
		label = WCLocale.UI.text.whisperCast..": ";
	end

	self:SetText(label..queuetext);
end

function FuBar_WhisperCastFu:UpdateTooltip()
	if (WhisperCast_Spells) then
		local cat = tablet:AddCategory(
			"text", "Queue Details:",
			"columns", 1,
			"child_textR", 1,
			"child_textG", 1,
			"child_textB", 0
		);

		if (WhisperCast_Runtime.queueLength == 0) then
			cat:AddLine(
				"text", WCLocale.UI.text.queueBriefEmpty
			);
		else
			cat:AddLine(
				"text", WhisperCast_Runtime.queueDetail
			);
		end
	else
		local cat = tablet:AddCategory(
			"text", "No queueable abilities.",
			"columns", 1,
			"child_textR", 1,
			"child_textG", 1,
			"child_textB", 0
		);

		cat:AddLine(
			"text", ""
		);
	end

	if (not self:IsHideHint()) then
		tablet:SetHint("Left-click to cast\nShift+Left-click to clear");
	end
end

function FuBar_WhisperCastFu:OnClick()
	if (IsShiftKeyDown()) then
		WhisperCast_Clear();
	else
		WhisperCast_Cast();
	end
end

FuBar_WhisperCastFu:RegisterForLoad();