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 = 1, 1, 1
		if(db[pref[#pref]]) then
			r,g,b = Hex2RGB(db[pref[#pref]])
--		print(Hex2RGB(db[pref[#pref]]))
		end
		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,3)

		config:AddConfigEntry(menu3,'fpsheader4',"header",L['fpsheader'],nil,4)
		config:AddConfigEntry(menu3,"latencyhigh","range",L['Latency High'],L['Latency High Desc'],5,0,500,1)
		config:AddConfigEntry(menu3,"latencyhighcolor","color",L['Latency High Color'],nil,6)

		config:AddConfigEntry(menu3,'fpsheader5',"header",L['fpsheader'],nil,7)
		config:AddConfigEntry(menu3,"latencymedium","range",L['Latency Medium'],L['Latency Medium Desc'],8,0,500,1)
		config:AddConfigEntry(menu3,"latencymediumcolor","color",L['Latency Medium Color'],nil,9)

		config:AddConfigEntry(menu3,'fpsheader6',"header",L['fpsheader'],nil,10)
		config:AddConfigEntry(menu3,"latencylow","range",L['Latency Low'],L['Latency Low Desc'],11,0,500,1)
		config:AddConfigEntry(menu3,"latencylowcolor","color",L['Latency Low Color'],nil,12)

		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,3)

		config:AddConfigEntry(menu4,'fpsheader',"header",L['fpsheader'],nil,4)
		config:AddConfigEntry(menu4,"fpshigh","range",L['FPS High'],L['FPS High Desc'],5,0,500,1)
		config:AddConfigEntry(menu4,"fpshighcolor","color",L['FPS High Color'],nil,6)

		config:AddConfigEntry(menu4,'fpsheader2',"header",L['fpsheader'],nil,7)
		config:AddConfigEntry(menu4,"fpsmedium","range",L['FPS Medium'],L['FPS Medium Desc'],8,0,500,1)
		config:AddConfigEntry(menu4,"fpsmediumcolor","color",L['FPS Medium Color'],nil,9)

		config:AddConfigEntry(menu4,'fpsheader3',"header",L['fpsheader'],nil,10)
		config:AddConfigEntry(menu4,"fpslow","range",L['FPS Low'],L['FPS Low Desc'],11,0,500,1)
		config:AddConfigEntry(menu4,"fpslowcolor","color",L['FPS Low Color'],nil,12)

		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')