local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local db, realm
local zzLDB = LibStub:GetLibrary("zzLDB")
local folder = addonName ~= childName  and format("Interface\\AddOns\\%s\\%s", addonName, childName) or format("Interface\\AddOns\\%s", childName)
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
local function OnClick(self, button)
	zzLDB:DefaultOnClick(self, button, addon, childName)
end
local function OnTooltipShow(tip)
	tip:AddLine(childName)
	local statusItem = child['statusItem']
	for k,v in pairs(statusItem) do
		local stat = statusItem[k]
		if(db['listall'] or stat < 100) then
			stat = child:colorize(stat,child:pickColor(stat))
			tip:AddDoubleLine(L[k], stat.." %")
		end
	end
	tip:AddLine(" ")
	tip:AddLine(L["Repair Charts"])
	local tmp = {}
	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
				if(m == realm) then
					local player = format("%s-%s", n, m)
					if(db['repairstats'] and db['repairstats'][player] and db['repairstats'][player] > 0) then
						tmp[player] = db['repairstats'][player]
					end
				end
			end
		end
	end
	for _,v in pairs(child:sortArray(tmp,false)) do
		tip:AddDoubleLine(child:classcolor(v,db['classes'][v]), child:MoneyString(db['repairstats'][v] or 0))
	end
	tip:AddLine(" ")
	tip:AddLine(L['HINT3'])
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'] = format("%s\\icon2.tga", folder), -- optional/required for launcher
	['OnClick'] = OnClick, -- optional/required for launcher
	['OnTooltipShow'] = OnTooltipShow, -- optional
}
local init = {
	Setup = function(self, childName, event)
--		print("setup ldb", childName, event)
		db = child['db']
		realm = GetRealmName()
		local player = UnitName('player')
		local _, class = UnitClass('player')
		db['classes'] = db['classes'] or {}
		db['classes'][player] = class
		child.OnText = zzLDB.DefaultOnText
	end
}
addon.RegisterCallback(init, format("Init%s", childName), 'Setup', childName)