local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local events = child['events']
local db, dbr, dbp, faction, realm
LibStub("zzHelper"):Embed(child)
local session = { start = 0,	income = 0,	expense = 0 }
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
local function prepareDB(self)
	local player = UnitName('player')
	realm = GetNormalizedRealmName()
	if(not realm) then return end
	faction = UnitFactionGroup('player')
	local className, classFilename, classId = UnitClass('player')
	addon['db']['profile'][childName] = addon['db']['profile'][childName] or {}
	db = addon['db']['profile'][childName]
	db[realm] = db[realm] or {}
	db[faction] = db[faction] or {}
	db[faction]['banker'] = db[faction]['banker'] or {
		['maxLimit'] = 150000,
		['minLimit'] = 30000
	}
	db[faction]['banker']['banker'] = db[faction]['banker']['banker'] or player
	dbr = db[realm]
	dbr['activity'] = dbr['activity'] or { Horde = {}, Alliance={}}
	dbr['Horde'] = dbr['Horde'] or {}
	dbr['Alliance'] = dbr['Alliance'] or {}
	dbr[player] = dbr[player] or {}
	dbp = dbr[player]
	dbp['faction'] = faction
	dbp['currentGold'] = dbp['currentGold'] or 0
	dbp['class'] = classFilename
	dbp['faction'] = faction
	child['storage'] = {
		['db'] = db,
		['dbr'] = dbr,
		['dbp'] = dbp,
		['dbf'] = db[faction],
		['L'] = L,
		['session'] = session,
		['faction'] = faction,
		['realm'] = realm,
	}
	child['db'] = db
	addon['callbacks']:Fire(format("Init%s", childName))
	child['events']['PLAYER_MONEY']()
end
function child:getDateString()
	return format("%s%s",tostring(date("%Y")),tostring(1000 + date("%j")))
end
function child:GetGold(val)
	local val = val or 2
	local sums = { dbp['currentGold'], 0, 0, 0	}
	if(addon['db']['sv']['profileKeys']) then
		for k,v in pairs(addon['db']['sv']['profileKeys']) do
			local n,_,m = strsplit(' ',k,3)
			if(n) then
				local cr = child.Helper_Tooltip3(m)
				child.cdbCheck(n, m)
				local ch = db[m][n]
				sums[3] = sums[3] + ch['currentGold'] -- global all
				if(faction == ch['faction']) then
					if(m == realm or cr) then
						sums[2] = sums[2] + ch['currentGold'] -- realm/pool faction
					end
					sums[4] = sums[4] + ch['currentGold'] -- global faction
				end
			end
		end
	end
	return sums[val]
end
function child:displayMoney(money, abr)
	if(abr) then
		if(db['shortMoney'] and money>10000)then
			money = child:round(money / 10000,0) * 10000
		end		
	end
	return child:MoneyString(money, false, abr and (db['abrevK'] and L['abrK'] or nil) or nil, abr and (db['abrevM'] and L['abrM'] or nil) or nil, db['noIcon'], db['noIconColor'])
end
function events:PLAYER_MONEY(event)
	local money = GetMoney()
	if(money == 0) then  return end
	if(session['start'] == 0) then
		session['start'] = money
	end
	dbp['currentGold'] = dbp['currentGold'] or 0
	dbp['incoming'] = nil
	if(money ~= dbp['currentGold']) then
		local diff = money - dbp['currentGold']
		local out = GetCoinTextureString(abs(diff))
		local color = "ffffff"
		local stamp = time()
		local day = child:getDateString()
		local dba = dbr['activity'][faction]
		dba[day] = dba[day] or {}
		local dbd = dba[day]
		while(dbd[stamp]) do
			stamp = stamp + 1
		end
		if(diff < 0) then
			color = "ff0000"
			session['expense'] = session['expense'] + diff
		else
			session['income'] = session['income'] + diff
		end
		dbd[stamp] = diff
		if(db['showLog']) then
			print(child:colorize(out, color))
		end
		dbp['currentGold'] = money
	end
	local sum = child:GetGold(db['displaySel'] or 2)
	child:OnText(childName, child:displayMoney(sum, true))
end
child['specs']  = {
	['name'] = childName,
	['sv'] = true,
	['cfg'] = true,
	['cfgReset'] = prepareDB,
	['ldb'] = child['ldb'],
	['defaults'] = {
		['profile'] = {
			[childName] = {
				['disableBanker'] = true,
				['displaySelTT'] = 2,
				['displaySel'] = 2,
				['connectedRealms'] = true
			}
		}
	},
	['events'] = events
}
function events:PLAYER_ENTERING_WORLD()
	if(not realm) then
		prepareDB()
	else
		child:UnregisterEvent('PLAYER_ENTERING_WORLD')
	end
end
child:NewAddOn(...)