local addon, ns = ...;

ns.WbCap = 999999990000;
ns.OptionsLoaded = false;
ns.sessionPause = false;
ns.maxLevel = GetMaxLevelForPlayerExpansion();

ns.Icon = { ["Gold"] = "|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t ", 
			["Silver"] = "|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t ",
			["Copper"] = "|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t"};

function ns:GoldSilverCopper(copper)
	copper = tonumber(copper or 0);									-- make numeric
	if copper == 0 then return "0"; end;							-- return "0" if 0
	local gIcon = "|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:2:0|t "
	local sIcon = "|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:2:0|t "
	local cIcon = "|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:2:0|t"	
	local neg = false;												-- Flag for negative values
	if copper < 0 then neg = true; copper = copper * -1; end;		-- if - set flag and flip to +
	local c = format("%02d", (copper % 100)) .. cIcon;				-- format copper to 0# and add icon
	local s = format("%02d", (floor(copper / 100) % 100)) .. sIcon;	-- format silver to 0# and add icon
	local g = floor(copper / 10000);								-- get the gold value (drop last 4 digits)
	local gt = "";													-- variable to build the formated gold balu into
	
	while (g > 999) do												-- 999 or less we just need the numbers
		-- get least 3 digits and place a separator character, then 3 digits, then add characters from previous loops
		gt = WarbandMiserDB.Thousands.Seperator .. format("%03d", (g % 1000)) .. gt;
		g = floor(g / 1000);										-- drop the last 3 digits
	end;
	gt = g .. gt;													-- add remaining characters to beginning of string
	local ret = "";													-- build the return string
	if neg then ret = "-"; end;										-- if negative add the - sign
	if copper > 9999 then ret = ret .. gt .. gIcon; end;			-- add gold if any
	if copper > 99 then ret = ret .. s; end;						-- add silver is any
	ret = ret .. c;													-- add copper (if no copper the first line returned "0")
	return ret;	
end;			

function ns:VerifyData(r, n)
	local level = UnitLevel("player");
	WarbandMiserDB.Toons[r] = WarbandMiserDB.Toons[r] or {};
	WarbandMiserDB.Toons[r][n] = WarbandMiserDB.Toons[r][n] or {};
	if WarbandMiserDB.Toons[r][n].Level == nil then WarbandMiserDB.Toons[r][n].Level = level; end;
	if WarbandMiserDB.Toons[r][n].Include == nil then WarbandMiserDB.Toons[r][n].Include = WarbandMiserDB.Settings.IncludeNew; end;
	if WarbandMiserDB.Toons[r][n].Defaults == nil then WarbandMiserDB.Toons[r][n].Defaults = true; end;
	if WarbandMiserDB.Toons[r][n].ServerDefault == nil then WarbandMiserDB.Toons[r][n].ServerDefault = false; end;
	if WarbandMiserDB.Toons[r][n].AllGold == nil then WarbandMiserDB.Toons[r][n].AllGold = false; end;
	if WarbandMiserDB.Toons[r][n].GoldCap == nil then WarbandMiserDB.Toons[r][n].GoldCap = ns:GetDefaultGoldForLevel(level); end;
	if WarbandMiserDB.Toons[r][n].Gold == nil then WarbandMiserDB.Toons[r][n].Gold = GetMoney(); end;
end;

function ns:GetDefaultGoldForLevel(level)
	if level >= WarbandMiserDB.Settings.Range[1].low and level <= WarbandMiserDB.Settings.Range[1].high then return WarbandMiserDB.Settings.Bracket1; end;
	if level >= WarbandMiserDB.Settings.Range[2].low and level <= WarbandMiserDB.Settings.Range[2].high then return WarbandMiserDB.Settings.Bracket2; end;
	if level >= WarbandMiserDB.Settings.Range[3].low and level <= WarbandMiserDB.Settings.Range[3].high then return WarbandMiserDB.Settings.Bracket3; end;
	if level >= WarbandMiserDB.Settings.Range[4].low and level <= WarbandMiserDB.Settings.Range[4].high then return WarbandMiserDB.Settings.Bracket4; end;
	if level >= WarbandMiserDB.Settings.Range[5].low and level <= WarbandMiserDB.Settings.Range[5].high then return WarbandMiserDB.Settings.Bracket5; end;
	if level >= WarbandMiserDB.Settings.Range[6].low and level <= WarbandMiserDB.Settings.Range[6].high then return WarbandMiserDB.Settings.Bracket6; end;
	return 100;
end;

function ns:GetWbGold()	
	if C_Bank then 
		return C_Bank.FetchDepositedMoney(Enum.BankType.Account); 
	end;
	return 0;
end;