local addon, ns = ...;

function myPrint( ... )
	print("\124cFF0088FFWarband Miser: \124r",  ...);
end;

local function isIncluded()
	return WarbandMiserDB.Toons[ns.Server][ns.Name].Include;
end;

local function iniToon()
	ns.Name, ns.Server = UnitFullName("player");
	ns.Level = UnitLevel("player");
	WarbandMiserDB = WarbandMiserDB or {};
	WarbandMiserDB.wbMinumum = WarbandMiserDB.wbMinumum or 0;
	WarbandMiserDB.Settings = WarbandMiserDB.Settings or {};
	if WarbandMiserDB.Settings.DepositOnly == nil then WarbandMiserDB.Settings.DepositOnly = false; end;
	WarbandMiserDB.Settings.Bracket1 = WarbandMiserDB.Settings.Bracket1 or 200;
	WarbandMiserDB.Settings.Bracket2 = WarbandMiserDB.Settings.Bracket2 or 500;
	WarbandMiserDB.Settings.Bracket3 = WarbandMiserDB.Settings.Bracket3 or 750;
	WarbandMiserDB.Settings.Bracket4 = WarbandMiserDB.Settings.Bracket4 or 1000;
	WarbandMiserDB.Settings.Bracket5 = WarbandMiserDB.Settings.Bracket5 or 2000;
	WarbandMiserDB.Settings.Bracket6 = WarbandMiserDB.Settings.Bracket6 or 10000;
	WarbandMiserDB.Settings.Range = WarbandMiserDB.Settings.Range or {};
	WarbandMiserDB.Settings.Range[1] = WarbandMiserDB.Settings.Range[1] or {["low"] = 1, ["high"] = 10};
	WarbandMiserDB.Settings.Range[2] = WarbandMiserDB.Settings.Range[2] or {["low"] = 11, ["high"] = 25};
	WarbandMiserDB.Settings.Range[3] = WarbandMiserDB.Settings.Range[3] or {["low"] = 26, ["high"] = 50};
	WarbandMiserDB.Settings.Range[4] = WarbandMiserDB.Settings.Range[4] or {["low"] = 51, ["high"] = 70};
	if WarbandMiserDB.Settings.Range[5] and ns.maxLevel == 70 then
		if WarbandMiserDB.Settings.Range[5].low > 70 then WarbandMiserDB.Settings.Range[5].low = 70; end;
		if WarbandMiserDB.Settings.Range[5].high > 70 then WarbandMiserDB.Settings.Range[5].high = 70; end;
		if  WarbandMiserDB.Settings.Range[6] then
			if WarbandMiserDB.Settings.Range[6].low > 70 then WarbandMiserDB.Settings.Range[6].low = 70; end;
			if WarbandMiserDB.Settings.Range[6].high > 70 then WarbandMiserDB.Settings.Range[6].high = 70; end;
		end;
	end;
	
	WarbandMiserDB.Settings.Range[5] = WarbandMiserDB.Settings.Range[5] or {["low"] = 71, ["high"] = 79};
	WarbandMiserDB.Settings.Range[6] = WarbandMiserDB.Settings.Range[6] or {["low"] = 80, ["high"] = 80};
	WarbandMiserDB.Settings.Servers = WarbandMiserDB.Settings.Servers or {};
	WarbandMiserDB.Settings.MinGold = WarbandMiserDB.Settings.MinGold or 2000;
	WarbandMiserDB.Settings.Servers[ns.Server] = WarbandMiserDB.Settings.Servers[ns.Server] or {};
	WarbandMiserDB.Settings.Servers[ns.Server].Default = WarbandMiserDB.Settings.Servers[ns.Server].Default or 0
	
	WarbandMiserDB.Toons = WarbandMiserDB.Toons or {};
	WarbandMiserDB.Toons[ns.Server] = WarbandMiserDB.Toons[ns.Server] or {};
	WarbandMiserDB.Toons[ns.Server][ns.Name] = WarbandMiserDB.Toons[ns.Server][ns.Name] or {};
	WarbandMiserDB.Toons[ns.Server][ns.Name].Level = ns.Level;
	if WarbandMiserDB.Toons[ns.Server][ns.Name].Include == nil then 
		WarbandMiserDB.Toons[ns.Server][ns.Name].Include = WarbandMiserDB.Settings.IncludeNew; 
	end;
	if WarbandMiserDB.Toons[ns.Server][ns.Name].Defaults == nil then 
		WarbandMiserDB.Toons[ns.Server][ns.Name].Defaults = true; 
	end;
	if WarbandMiserDB.Toons[ns.Server][ns.Name].GoldCap == nil then 
		WarbandMiserDB.Toons[ns.Server][ns.Name].GoldCap = ns:GetDefaultGoldForLevel(ns.Level); 
	end;
	WarbandMiserDB.Toons[ns.Server][ns.Name].Level = ns.Level;
	WarbandMiserDB.Toons[ns.Server][ns.Name].Gold = GetMoney();
end;
	
local EventFrame = CreateFrame("FRAME");
EventFrame:RegisterEvent("PLAYER_INTERACTION_MANAGER_FRAME_SHOW");
EventFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
EventFrame:RegisterEvent("PLAYER_MONEY");

function EventFrame:OnEvent(event, arg1)
	if event == "PLAYER_ENTERING_WORLD" then 
		iniToon();
		ns:LoadOptionsPage();
		ns:iniIcon();
		EventFrame:UnregisterEvent("PLAYER_ENTERING_WORLD");
	end;
	if event == "PLAYER_MONEY" then 
		ns.Level = UnitLevel("player");
		WarbandMiserDB.Toons[ns.Server][ns.Name].Level = ns.Level;
		WarbandMiserDB.Toons[ns.Server][ns.Name].Gold = GetMoney();
	end;		
	if event == "PLAYER_INTERACTION_MANAGER_FRAME_SHOW" and 
				(arg1 == Enum.PlayerInteractionType.Banker or  Enum.PlayerInteractionType.AccountBanker) then
		if ns.sessionPause then return; end;
		if C_Bank.CanDepositMoney(Enum.BankType.Account) == false then return; end;
		local n, r = UnitFullName("player");
		local l = UnitLevel("player");	
		local keepGold = 0;
		local WbGold = ns:GetWbGold();
		ns:VerifyData(r, n)
		local data = WarbandMiserDB.Toons[r][n];
		if isIncluded() == true then
			if data.Defaults == true then
				keepGold = ns:GetDefaultGoldForLevel(l) * 10000;
			elseif data.ServerDefault == true then
				keepGold = WarbandMiserDB.Settings.Servers[r].Default * 10000;
			elseif data.AllGold == true then
				keepGold = GetMoney() + WbGold - (WarbandMiserDB.wbMinumum * 10000)
				if keepGold > 95000000000 then keepGold = 95000000000; end;
			else
				keepGold = WarbandMiserDB.Toons[r][n].GoldCap * 10000;
			end;
			local dif = GetMoney() - keepGold;
			if dif > 0 then
				if WbGold == ns.WbCap then
					dif = 0;	--at cap so do nothing
				elseif WbGold + dif > ns.WbCap then
					dif = ns.WbCap - WbGold;
				end;
				if dif ~= 0 then 
					C_Bank.DepositMoney(Enum.BankType.Account, dif);
					myPrint("Deposited " .. ns:GoldSilverCopper(dif) .. "  to Warbank."); 
				end;
			elseif dif < 0  and WarbandMiserDB.Settings.DepositOnly ~= true then
				dif = abs(dif);
				if dif > WbGold then dif = WbGold; end;
				if dif ~= 0 then
					C_Bank.WithdrawMoney(Enum.BankType.Account, dif);
					myPrint("Withdrew " .. ns:GoldSilverCopper(dif) .. "  from Warbank.");	
				end;
			end;
		end;
	end;
end;
EventFrame:SetScript("OnEvent", EventFrame.OnEvent); 


