local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local db, dbr, faction
local function buildSelect()
	local sel = {}
	local n,m,_
	if(addon['db']['sv']['profileKeys']) then
		for k,v in pairs(addon['db']['sv']['profileKeys']) do
			n,_,m = strsplit(' ',k,3)
			if(child:IsTwink(n)) then
				if(dbr[n] and faction == dbr[n]['faction']) then
					sel[n] = n
				end
			end
		end
	end
	return sel
end
local function buildSelectAll()
	local sel = {}
	local n,m,_
	if(addon['db']['sv']['profileKeys']) then
		for k,v in pairs(addon['db']['sv']['profileKeys']) do
			n,_,m = strsplit(' ',k,3)
			sel[format("%s-%s",n,m)] = format("%s-%s",n,m)
		end
	end
	return sel
end
local function deleteChar()
	if(db['deleteCharOk']) then
		local charName, charRealm = strsplit('-', db['deleteChar'],2)
		if(db[charRealm] and db[charRealm][charName]) then
			db[charRealm][charName] = nil
			addon['db']['sv']['profileKeys'][format("%s - %s", charName, charRealm)] = nil
		end
	end
	db['deleteCharOk'] = nil
end


local init = {
	Setup = function(self)
		local L = child['storage']['L']
		dbr = child['storage']['dbr']
		db = child['storage']['db']
		local dbf = child['storage']['dbf']
		local dbb = dbf['banker']
		faction = child['storage']['faction']
		local config = LibStub:GetLibrary("zzConfig")
		local options = child['options']
		local main = options['args'][childName]
		local buildDisplayValue = {
			L['displayMoneyChar'], L['displayMoneyRealm'], L['displayMoneyGlobal'], L['displayMoneyGlobalFaction']
		}
		config:AddConfigEntry(main, "displaySel", "select", L['Displayvalue'], L['DisplayvalueDesc'], 1, buildDisplayValue)
		config:AddConfigEntry(main, "connectedRealms", "toggle", L['connectedRealms'], nil, 2)
		config:AddConfigEntry(main, "showLog", "toggle", L['showLog'], nil, 3)
		config:AddConfigEntry(main, "shortMoney", "toggle", L['shortMoney'], nil, 4)
		config:AddConfigEntry(main, "disableBanker", "toggle", L['nobanker'], nil, 5)
		config:AddConfigEntry(main, "abrevK", "toggle", L['abrevK'], nil, 6)
		config:AddConfigEntry(main, "abrevM", "toggle", L['abrevM'], nil, 7)
		local noicon = config:AddConfigEntry(main, "noIcon", "toggle", L['noIcon'], L['noIconDesk'], 6)
		local noiconColor = config:AddConfigEntry(main, "noIconColor", "toggle", L['Color text'], L['Color text desc'], 7)
		noiconColor['disabled'] = function(self) if(not db['noIcon']) then return true end end
		local menu = config:AddConfigMenu(options, L['Bankier'], 2, config:GetDefaultHandler(child['events']['MAIL_SHOW'], dbb), { disabled = function() return db['disableBanker']
	end})
		config:AddConfigEntry(menu, "minLimit", "range", L['Minimum Limit'], L['Desc MinL'], 1, 10, 150000, 5, false)
		config:AddConfigEntry(menu, "maxLimit", "range", L['Maximum Limit'], L['Desc MaxL'], 2, 10, 150000, 5, false)
		config:AddConfigEntry(menu, "banker","select",L['Bankier'],nil,3,buildSelect)
		local ttmenu = config:AddConfigEntry(main, "ttmenu", "group", L['ttmenu_title'], L['ttmenu_desc'], 9, config:GetDefaultHandler(child['events']['PLAYER_MONEY'], db), true)
		config:AddConfigEntry(ttmenu, "displaySelTT", "select", L['Gesamt'], L['DisplayvalueDesc'], 1, buildDisplayValue)
		config:AddConfigEntry(ttmenu, "sortDir", "toggle", L['sortDir'], nil, 2)
		config:AddConfigEntry(ttmenu, "accountMoney", "toggle", L['accountMoney'], L['accountMoneyDesc'], 3)
		local short = config:AddConfigEntry(ttmenu, "shortMoneyTip", "toggle", L['shortMoneyTip'], nil, 4)
		short['disabled'] = function(self) if(not db['abrevK'] and not db['abrevM']) then return true end end
		local delmenu = config:AddConfigEntry(main, "delmenu", "group", L['DeleteChar'], L['DeleteCharDesc'], 10, config:GetDefaultHandler(nil, db), true)
		config:AddConfigEntry(delmenu, "deleteChar", "select", L['DeleteChar'], L['DeleteCharDesc'], 1, buildSelectAll)
		config:AddConfigEntry(delmenu, "deleteCharOk", "toggle", L['DeleteCharOk'], nil , 2)
		config:AddConfigEntry(delmenu, "deleteCharExec", "execute", L['DeleteChar'], L['DeleteCharDesc'], 3, deleteChar)
		child.PrefChanged = child['events']['PLAYER_MONEY']
	end
}
addon.RegisterCallback(init, format("Init%s", childName), 'Setup')