WhisperCastFu = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceDB-2.0", "FuBarPlugin-2.0");
local L = AceLibrary("AceLocale-2.0"):new("WhisperCastFu");
local Dewdrop = AceLibrary("Dewdrop-2.0");
local Metro = AceLibrary("Metrognome-2.0");
local Tablet = AceLibrary("Tablet-2.0");

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

L:RegisterTranslations("enUS", function() return {
	["hideLabelName"] = "Hide Label",
	["hideLabelDescription"] = "Toggle the WhisperCast label on FuBar.",
	["hideTooltipName"] = "Hide Tooltip Hint",
	["hideTooltipDescription"] = "Toggle the tooltip that shows queue details.",
	["whisperCastOptions"] = "WhisperCast Options",
	["queueDetails"] = "Queue Details:",
	["noQueueableAbilities"] = "No queueable abilities.",
	["tabletHint"] = "Left-click to cast\nShift+Left-click to clear",
} end);

WhisperCastFu:RegisterDB("WhisperCastFuDB");
WhisperCastFu:RegisterDefaults("profile", {
	hideLabel = false,
	hideHint = false,
});

WhisperCastFu.hasIcon = true;
WhisperCastFu.defaultPosition = "CENTER";

local optionsTable = {
	handler = WhisperCastFu,
	type = "group",
	args = {
		hideLabel = {
			type = "toggle",
			name = L"hideLabelName",
			desc = L"hideLabelDescription",
			get = "IsHideLabel",
			set = "ToggleHideLabel",
		},
		hideHint = {
			type = "toggle",
			name = L"hideTooltipName",
			desc = L"hideTooltipDescription",
			get = "IsHideHint",
			set = "ToggleHideHint",
		}
	};
};

WhisperCastFu:RegisterChatCommand({ "/whispercastfu" }, optionsTable)
WhisperCastFu.OnMenuRequest = optionsTable

function WhisperCastFu:IsHideLabel()
	return self.db.profile.hideLabel;
end

function WhisperCastFu:ToggleHideLabel()
	self.db.profile.hideLabel = not self.db.profile.hideLabel;
	self:Update();
end

function WhisperCastFu:IsHideHint()
	return self.db.profile.hideHint;
end

function WhisperCastFu:ToggleHideHint()
	self.db.profile.hideHint = not self.db.profile.hideHint;
	self:Update();
end

function WhisperCastFu:OnEnable()
	Metro:Register(self.name, self.Update, 1, self);
	Metro:Start(self.name);
end

function WhisperCastFu:OnDisable()
	Metro:Unregister(self.name);
end

function WhisperCastFu:OnMenuRequest(level, value)
	if (level == 1) then
		Dewdrop:AddLine(
			"text", L"whisperCastOptions",
			"hasArrow", TRUE,
			"value", "wcoptions"
		);
		Dewdrop:AddLine(
			"text", ""
		);
		Dewdrop:AddLine(
			"text", L"hideLabelName",
			"arg1", self,
			"func", "ToggleHideLabel",
			"checked", self:IsHideLabel()
		);
		Dewdrop:AddLine(
			"text", L"hideTooltipName",
			"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"
			);
			Dewdrop:AddLine(
				"text", WCLocale.UI.text.dropdownDisabledSub,
				"hasArrow", TRUE,
				"value", "disabled"
			);
		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")
			);
		elseif (value == "disabled") then
			if (WhisperCast_Spells ~= nil) then
				for spell,attrib in WhisperCast_Spells do
					Dewdrop:AddLine(
						"text", spell,
						"arg1", self,
						"func", self:ToggleSpellDisable(spell),
						"checked", WhisperCast_Profile.disabledSpells[spell]
					);
				end
			else
				Dewdrop:AddLine(
					"text", "No available spells"
				);
			end
		end
	end
end

--					Dewdrop:AddLine(
--						"text", spell,
--						"arg1", self,
--						--"value", test,
--						-- Lots of my failed attempts (I either need to get the variable "spell" passed to self:ToggleSpellDisable or I need to get the value of the text field sent, either "should" work.
--						--"func", self:ToggleSpellDisable(spell),
--						--"func", "ToggleSpellDisable",
--						--"func", WhisperCast_ToggleSpellDisable(spell),
--						--"func", function() self:ToggleSpellDisable(button.text) end,
--						--"func", function() self:ToggleSpellDisable(spell) WhisperCast_ToggleSpellDisable(spell) end,
--						"checked", WhisperCast_Profile.disabledSpells[spell]
--					);

					-- 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 WhisperCastFu:ToggleEnabled()
	WhisperCast_ToggleEnabled();
end

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

function WhisperCastFu:ToggleRestrictCombat()
	WhisperCast_ToggleRestrictCombat();
end

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

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

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

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

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

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

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

function 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 WhisperCastFu:OnTextUpdate()
	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 WhisperCastFu:OnTooltipUpdate()
	if (WhisperCast_Spells) then
		local cat = Tablet:AddCategory(
			"text", L"queueDetails",
			"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", L"noQueueableAbilities",
			"columns", 1,
			"child_textR", 1,
			"child_textG", 1,
			"child_textB", 0
		);

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

	if (not self:IsHideHint()) then
		Tablet:SetHint(L"tabletHint");
	end
end

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