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);
	local c = format(" %02d", (copper % 100)) .. ns.Icon.Copper;
	local s = format(" %02d", (floor(copper / 100) % 100)) .. ns.Icon.Silver;
	local g = floor(copper / 10000);
	local gt = "";
	local neg = false;
	if g == 0 then gt = "0"; end;
	if g < 0 then neg = true; g = g * -1; end;
	while (g > 1000) do	
		gt = "," .. format("%03d", (g % 1000)) .. gt;
		g = floor(g / 1000);
	end;
	gt = g .. gt;
	if neg then gt = "-" .. gt; end;
	return strjoin("", gt, ns.Icon.Gold , s, c);
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;