local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local db, dbr, dbp, L, lineIn, lineOut, session, faction, realm
local zzLDB = LibStub:GetLibrary("zzLDB")
local function cdbCheck(n,realm)
	db[realm] = db[realm] or {}
	db[realm][n] = db[realm][n] or { currentGold = 0 }
end
child.cdbCheck = cdbCheck
local function getSumToday(d)
	if(faction=='Neutral') then return 0,0 end
	local sumIn,sumOut = 0,0
	d = d or 0
	local day = child:getDateString() + d
	for a,b in pairs(dbr['activity'][faction][tostring(day)] or {}) do
		if(b < 0) then
			sumOut = sumOut + b
		else
			sumIn = sumIn + b
		end
	end
	return sumOut,sumIn
end
local function getSumWeek()
	local sumIn, sumOut = 0,0
	local sumWeekIn, sumWeekOut = 0,0
	for a=0,6 do
		sumOut, sumIn = getSumToday(a * -1)
		sumWeekIn = sumWeekIn + sumIn
		sumWeekOut = sumWeekOut + sumOut
	end
	return sumWeekOut,sumWeekIn
end
local connectedRealms
local function Helper_Tooltip3(realm)
	if(not db['connectedRealms']) then return end
	connectedRealms = connectedRealms or GetAutoCompleteRealms()
	for k, v in pairs(GetAutoCompleteRealms()) do
		if(v == realm) then
			return true
		end
	end
end
child.Helper_Tooltip3 = Helper_Tooltip3
local function OnClick(self, button)
	zzLDB:DefaultOnClick(self, button, addon, childName)
end
local function OnTooltipShow(tip)
	tip:AddLine(childName)
	if(faction == 'Neutral') then 
		tip:AddLine(L['neutral'])
		return
	end
	local sessdiff = dbp['currentGold'] - session['start']
	local sdr,sdg,sdb = 1, 1, 1
	if(sessdiff<0) then
		sdr,sdg,sdb = 1, 0, 0
	end
	tip:AddDoubleLine(L['Sitzung'], child:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
	tip:AddDoubleLine(lineIn, child:displayMoney(session['income'], db['shortMoneyTip']),1,1,1,1,1,1)
	tip:AddDoubleLine(lineOut, child:displayMoney(abs(session['expense']), db['shortMoneyTip']),1,1,1,1,1,1)
	tip:AddLine(' ')
	local sumOut,sumIn = getSumToday()
	sessdiff = sumOut+sumIn
	if(sessdiff<0) then
		sdr,sdg,sdb = 1, 0, 0
	else
		sdr,sdg,sdb = 1, 1, 1
	end
	tip:AddDoubleLine(L['Heute'], child:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
	tip:AddDoubleLine(lineIn, child:displayMoney(sumIn, db['shortMoneyTip']),1,1,1,1,1,1)
	tip:AddDoubleLine(lineOut, child:displayMoney(abs(sumOut), db['shortMoneyTip']),1,1,1,1,1,1)
	tip:AddLine(' ')
	local sumOut,sumIn = getSumToday(-1)
	sessdiff = sumOut+sumIn
	if(sessdiff<0) then
		sdr,sdg,sdb = 1, 0, 0
	else
		sdr,sdg,sdb = 1, 1, 1
	end
	tip:AddDoubleLine(L['Gestern'], child:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
	tip:AddDoubleLine(lineIn, child:displayMoney(sumIn, db['shortMoneyTip']),1,1,1,1,1,1)
	tip:AddDoubleLine(lineOut, child:displayMoney(abs(sumOut), db['shortMoneyTip']),1,1,1,1,1,1)		
	tip:AddLine(' ')
	local sumOut,sumIn = getSumWeek()
	sessdiff = sumOut+sumIn
	if(sessdiff<0) then
		sdr,sdg,sdb = 1, 0, 0
	else
		sdr,sdg,sdb = 1, 1, 1
	end
	tip:AddDoubleLine(L['7-Tage'], child:displayMoney(abs(sessdiff), db['shortMoneyTip']),1,1,0,sdr,sdg,sdb)
	tip:AddDoubleLine(lineIn, child:displayMoney(sumIn, db['shortMoneyTip']),1,1,1,1,1,1)
	tip:AddDoubleLine(lineOut, child:displayMoney(abs(sumOut), db['shortMoneyTip']),1,1,1,1,1,1)
		tip:AddLine(' ')
	local sum = 0
	local ttlist = {}
	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 = Helper_Tooltip3(m)
				if(m == realm or cr) then
					cdbCheck(n, m)
					if( faction == db[m][n]['faction']) then
--							sum = sum + db[m][n]['currentGold']
						ttlist[k] = db[m][n]['currentGold']
					end
				elseif(db['accountMoney']) then
				
--						sum = sum + db[m][n]['currentGold']
				end
			end
		end
	end
	for _,v in pairs(child:sortArray(ttlist,db['sortDir'])) do
		local n,_,m = strsplit(' ',v,3)
		local cr = Helper_Tooltip3(m)
		local rm = cr and m or realm
		tip:AddDoubleLine(
			child:classcolor(n,db[rm][n]['class']),
			child:displayMoney(db[rm][n]['currentGold']+(db[rm][n]['incoming'] or 0), db['shortMoneyTip']),
			0,1,0,
			1,1,1
		)
	end
	tip:AddLine(' ')
	tip:AddDoubleLine(L['Gesamt'],child:displayMoney(child:GetGold(db['displaySelTT'] or 2), db['shortMoneyTip']),0,1,0,1,1,1)
end
child['ldb'] = { -- https://github.com/tekkub/libdatabroker-1-1/wiki/Data-Specifications
	['type'] = 'data source', -- required: 'data source' or 'launcher'
	['text'] = childName, -- required/optional for launcher
	['icon'] = 'Interface\\Icons\\INV_Misc_Bag_10_Blue', -- optional/required for launcher
	['OnClick'] = OnClick, -- optional/required for launcher
	['OnTooltipShow'] = OnTooltipShow, -- optional
}
function child:OnText(objname, message)
	local ldbobj = zzLDB:GetLDB(objname)
	if(ldbobj['text'] ~= message) then
		ldbobj['text'] = message
	end
end
local init = {
	Setup = function(self, childName, event)
		L = child['storage']['L']
		db = child['storage']['db']
		dbr = child['storage']['dbr']
		dbp = child['storage']['dbp']
		session = child['storage']['session']
		faction = child['storage']['faction']
		realm = child['storage']['realm']
		local folder = addonName ~= childName  and format("Interface\\AddOns\\%s\\%s", addonName, childName) or format("Interface\\AddOns\\%s", childName)
		lineIn = format("|T%s:0|t %s", format("%s\\in.tga", folder), L['Einnahmen'])
		lineOut = format("|T%s:0|t %s", format("%s\\out.tga", folder), L['Ausgaben'])
	end
}
addon.RegisterCallback(init, format("Init%s", childName), 'Setup', childName)