local addon, ns = ...;

local icon = LibStub("LibDBIcon-1.0", true);
WarbandMiserIcon = WarbandMiserIcon or {};
if WarbandMiserIcon.Visible == nil then WarbandMiserIcon.Visible = true; end;

local function colorString(c, str)
	local color = 	{["red"] = "FF0000", ["green"]  = "00FF00", ["white"] = "FFFFFF",
				["blue"] = "0000FF", ["orange"] = "CF8C5A", ["yellow"] = "FFFF00" };
	if c == nil then return; end;
	if str == nil then str = "nil"; end;
	return string.format("|cff%s%s|r", color[c], str);
end

local function GetTotalPlayerGold()
	local gt = 0;
	WarbandMiserDB.Toons[ns.Server][ns.Name].Gold = GetMoney();
	for s,v in pairs(WarbandMiserDB.Toons) do
		for n,t in pairs(v) do
			gt = gt + t.Gold;
		end;
	end;
	return gt;
end;

local function fillInfoTooltip(tip)
	if tip == nil then return; end;
	local wb = ns:GetWbGold();
	local pg = GetTotalPlayerGold();
	tip:ClearLines();
	tip:AddDoubleLine(colorString("yellow", "Warband Miser"), "v" .. C_AddOns.GetAddOnMetadata(addon, "Version"));
	tip:AddLine("\n");
	tip:AddDoubleLine(colorString("orange", "LEFT Click"),  colorString("green", "Settings Page"));	
	tip:AddDoubleLine(colorString("orange", "<CTRL> LEFT Click"),  colorString("green", "Toggle Minimap Button"));
	if ns.sessionPause then
		tip:AddDoubleLine(colorString("orange", "RIGHT Click"),  colorString("green", "Toggle on"));
	else
		tip:AddDoubleLine(colorString("orange", "RIGHT Click"),  colorString("green", "Toggle off for session"));
	end;
	tip:AddLine("Toggle will reset to saved setting after this session.\n\n")
	tip:AddDoubleLine("My Gold", ns:GoldSilverCopper(GetMoney()));
	tip:AddDoubleLine("Total Player Gold", ns:GoldSilverCopper(pg));
	tip:AddDoubleLine("Warband Gold", ns:GoldSilverCopper(wb));
	tip:AddDoubleLine("Account Total", ns:GoldSilverCopper(wb + pg));
	tip:AddLine("\n");
	tip:Show();
end;

local function minimapButtonShowHide()
	if not icon then return; end;
	if WarbandMiserIcon.Visible == nil then WarbandMiserIcon.Visible = true; end;
	--flip visibility.
	WarbandMiserIcon.Visible = not WarbandMiserIcon.Visible;
	if WarbandMiserIcon.Visible == true then icon:Show(addon); else icon:Hide(addon); end;
end;

function ns:setMMButton()	
	if WarbandMiserIcon.Visible == true then icon:Show(addon); else icon:Hide(addon); end;
end;

local function WarbandMiserMiniMap(button)
	if not icon then return; end;
	if button == "LeftButton" then	
		if IsShiftKeyDown() then		
			--
		elseif IsControlKeyDown() then
			minimapButtonShowHide();
		else
			Settings.OpenToCategory(ns.category:GetID());
		end;
	elseif button == "RightButton" then
		ns.sessionPause = not ns.sessionPause;		
		GameTooltip:ClearLines();
		fillInfoTooltip(GameTooltip);
		GameTooltip:Show();
	end;
end;

AddonCompartmentFrame:RegisterAddon({
	text = "Warband Miser",
	icon = "Interface\\Icons\\achievement_guildperk_mobilebanking",
	notCheckable = true,
	registerForAnyClick = true,
	func = function(self, arg1, arg2)
		WarbandMiserMiniMap(arg1.buttonName);
	end,
	funcOnEnter = function(self)
		GameTooltip:SetOwner(self, "ANCHOR_NONE");
		GameTooltip:SetPoint("TOPRIGHT", self, "TOPLEFT", -10, 0);
		GameTooltip:ClearLines();
		fillInfoTooltip(GameTooltip);
	end,
	funcOnLeave = function()
		GameTooltip:Hide();
	end,
})

local iconFile = "Interface\\Icons\\achievement_guildperk_mobilebanking"
local wmLDB = LibStub("LibDataBroker-1.1"):NewDataObject("Warband Miser", {
	type = "data source",
	text = "Warband Miser",
	icon = iconFile,
	OnClick = function(_, button) WarbandMiserMiniMap(button) end,
})
function wmLDB:OnEnter()
	--ns:updateGold();
	GameTooltip:SetOwner(self, "ANCHOR_NONE");
	GameTooltip:SetPoint("TOPLEFT", self, "BOTTOMLEFT");
	fillInfoTooltip(GameTooltip);
end;
function wmLDB:OnLeave()
	GameTooltip:Hide();
end;

function ns:iniIcon()	
	if icon then icon:Register(addon, wmLDB, WarbandMiserIcon); end;
	if WarbandMiserIcon.Visible == true then icon:Show(addon); else icon:Hide(addon); end;
end;
--/ Mini map button stuff

SLASH_WARBANDMISER1 = "/warbandmiser";
SLASH_WARBANDMISER2 = "/wbm";

SlashCmdList.WARBANDMISER = function(arg)
	local arg1, arg2, arg3, arg4 = strsplit(" ", arg);
	msg = strlower(arg1);
	if msg == "settings" or msg == "s" or msg == "options" or msg == "o" then		
		Settings.OpenToCategory(ns.category:GetID());
	elseif msg == "toggle" or msg == "t" then
		ns.sessionPause = not ns.sessionPause;
	elseif msg == "mm" then
		minimapButtonShowHide();
	elseif msg == "gold" or msg == "g" then
		print( ns:GoldSilverCopper( ns:GetWbGold() ), " available in your Warband Bank" );
	else
		--Show a help screen
		local f = "off"
		if ns.sessionPause then f = "on"; end;
		local s = "/warbandmiser or /wbm Show help.\n" 	
			.. "/wbm settings, options, s or o - Open settings.\n"
			.. "/wbm toggle or t - toggle " .. f .. "\n"
			.. "/wbm mm - toggle minimap button on/off\n"
			.. "/wbm gold or g - Gold in your warbank.\n"
		print (s);
	end;
end;

--reload shortcut
SLASH_RELOADUI1 = "/rl"; 
SlashCmdList.RELOADUI = function(msg, editbox)
    ConsoleExec("reloadui");
end;


