local addonName, addon = ...
local childName = addon['childName']
local child = addon[childName]
local db
local L = LibStub("AceLocale-3.0"):GetLocale(childName, true)
local function expert()
	return not child['db']['expert']
end
local function noexpert()
	return child['db']['expert']
end
local function RGB2Hex(r,g,b)
	r = r <= 255 and r >= 0 and r or 0
	g = g <= 255 and g >= 0 and g or 0
	b = b <= 255 and b >= 0 and b or 0
	return string.format("%02x%02x%02x", r, g, b)
end
local function Hex2RGB(hex)
	local r, g, b = string.sub(hex, 1, 2), string.sub(hex, 3, 4), string.sub(hex, 5, 6)
	return tonumber(r, 16) or 0, tonumber(g, 16) or 0, tonumber(b, 16) or 0
end
function child:GetPref(pref)
--print("get", pref[#pref], pref['type'],db[pref[#pref]])
	if(pref['type'] == 'color') then
		local r,g,b = Hex2RGB(db[pref[#pref]])
--		print(Hex2RGB(db[pref[#pref]]))
		return r/255,g/255,b/255,1
	end
	return db[pref[#pref]]
end
function child:SetPref(pref,value,g,b)
	db[pref[#pref]] = pref['type'] == 'color' and RGB2Hex(value*255,g*255,b*255) or value
	child:update()
end
local init = {
	Setup = function(self)
		db = child['db']
		local config = LibStub:GetLibrary("zzConfig")
		local options = child['options']
		local main = options['args'][childName]
--		print("setup prefs", childName)
		local menu = config:AddConfigMenu(options, L['Clock'], 3, child, { disabled = expert })
		config:AddConfigEntry(menu,"clockstring","input",L['Clock'],nil,1)
		config:AddConfigEntry(menu,"clockcolor","color",L['Clock'],nil,2)
		config:AddConfigEntry(menu,"dateinfo","description",L['dateinfo'],nil,100)
		local menu2 = config:AddConfigMenu(options, L['Date'], 4, child, { disabled = expert })

		config:AddConfigEntry(menu2,"datestring","input",L['Date'],nil,1)
		config:AddConfigEntry(menu2,"datecolor","color",L['Date'],nil,2)
		config:AddConfigEntry(menu2,"dateinfo","description",L['dateinfo'],nil,100)
		local menu3 = config:AddConfigMenu(options, L['Latency'], 5, child, { disabled = expert })
		config:AddConfigEntry(menu3,"latencystring","input",L['Latency'],nil,1)
		config:AddConfigEntry(menu3,"latencycolor","color",L['Latency'],nil,2)
		config:AddConfigEntry(menu3,"latencyinfo","description",L['Latencystring'],nil,100)
		local menu4 = config:AddConfigMenu(options, L['FPS'], 6, child, { disabled = expert })
		config:AddConfigEntry(menu4,"fpsstring","input",L['FPS'],nil,1)
		config:AddConfigEntry(menu4,"fpscolor","color",L['FPS'],nil,2)
		config:AddConfigEntry(menu4,"fpsinfo","description",L['fpsstring'],nil,100)
		local menu5 = config:AddConfigMenu(options, L['LDB'], 5, child, { disabled = expert })
		config:AddConfigEntry(menu5,"ldbstring","input",L['LDB'],nil,1)
		config:AddConfigEntry(menu5,"ldbinfo","description",L['ldbstring'],nil,100)
		local btn1 = config:AddConfigEntry(main,"showClock","toggle",L['Show Clock'],nil,2)
		btn1['disabled'] = noexpert
		local btn2 = config:AddConfigEntry(main,"showLatency","toggle",L['Show Latency'],nil,3)
		btn2['disabled'] = noexpert
		local btn3 = config:AddConfigEntry(main,"showFPS","toggle",L['Show FPS'],nil,4)
		btn3['disabled'] = noexpert
		local btn4 = config:AddConfigEntry(main,"12-Hours","toggle",L['12-Hours'],nil,5)
		btn4['disabled'] = noexpert
		config:AddConfigEntry(main,"expert","toggle",L['Expert'],nil,6)
	end
}
addon.RegisterCallback(init, format("Init%s", childName), 'Setup')