--[[widgetData = {
	type = "doublecolor",
	name = "My Control",
	tooltip = "Control's tooltip",
	getFuncA = function() end,
	getFuncB = function() end,
	setFuncA = function() end,
	setFuncB = function() end,
	width = "full", --or "half" (optional)
	disabled = function() return boolean end, --(optional) boolean or function which returns boolean
	warning = "Some warning text", --(optional)
	default = { [1] = { r = val1, g = val2, b = val3, a = val4 }, [2] = ZO_ColorDef:New(r, g, b, a)}, -- (optional) table with color tables or ZO_ColorDef objects
	reference = "MyControlReference", --(optional) unique global reference
}
]]

local widgetVersion = 2
local LAM = LibStub("LibAddonMenu-2.0", true)
if (not LAM) then return end
if not LAM:RegisterWidget("doublecolor", widgetVersion) then return end

local wm = WINDOW_MANAGER
local cm = CALLBACK_MANAGER
local tinsert = table.insert

local function UpdateDisabled(control)
	local disable
	if type(control.data.disabled) == "function" then
		disable = control.data.disabled()
	else
		disable = control.data.disabled
	end

	if disable then
		control.label:SetColor(ZO_DEFAULT_DISABLED_COLOR:UnpackRGBA())
	else
		control.label:SetColor(ZO_DEFAULT_ENABLED_COLOR:UnpackRGBA())
	end

	control.isDisabled = disable
end

local function UpdateColor(control, valueR, valueG, valueB, valueA)
	control.setFunc(valueR, valueG, valueB, valueA or 1)

	--after setting this value, let's refresh the others to see if any should be disabled or have their settings changed
	local widget = control:GetParent()
	if widget.panel.data.registerForRefresh then
		cm:FireCallbacks("LAM-RefreshPanel", widget)
	end

	control.thumb:SetColor(valueR, valueG, valueB, valueA or 1)
end

local function UpdateValue(control, forceDefault)
	local aR, aG, aB, aA, bR, bG, bB, bA
	if forceDefault then	 --if we are forcing defaults
		local colorA = control.data.default[1]
		aR, aG, aB, aA = colorA.r, colorA.g, colorA.b, colorA.a
		control.colorA.setFunc(aR, aG, aB, aA)
		local colorB = control.data.default[2]
		bR, bG, bB, bA = colorB.r, colorB.g, colorB.b, colorB.a
		control.colorB.setFunc(bR, bG, bB, bA)
	else
		aR, aG, aB, aA = control.data.getFuncA()
		bR, bG, bB, bA = control.data.getFuncB()
	end

	control.colorA.thumb:SetColor(aR, aG, aB, aA or 1)
	control.colorB.thumb:SetColor(bR, bG, bB, bA or 1)
end


function LAMCreateControl.doublecolor(parent, widgetData, controlName)
	local panel = parent.panel or parent

	local dc = wm:CreateControl(controlName or widgetData.reference, parent.scroll or parent, CT_CONTROL)
	dc:SetMouseEnabled(true)
	dc:SetHandler("OnMouseEnter", ZO_Options_OnMouseEnter)
	dc:SetHandler("OnMouseExit", ZO_Options_OnMouseExit)

	dc.label = wm:CreateControl(nil, dc, CT_LABEL)
	dc.label:SetAnchor(TOPLEFT)
	dc.label:SetFont("ZoFontWinH4")
	dc.label:SetWrapMode(TEXT_WRAP_MODE_ELLIPSIS)
	dc.label:SetText(widgetData.name)

	dc.colorA = wm:CreateControl(nil, dc, CT_CONTROL)
	dc.colorA:SetDimensions(40, 24)
	dc.colorA:SetMouseEnabled(true)

	dc.colorB = wm:CreateControl(nil, dc, CT_CONTROL)
	dc.colorB:SetDimensions(40, 24)
	dc.colorB:SetMouseEnabled(true)

	local isHalfWidth = widgetData.width == "half"
	local widgetWidth = panel:GetWidth() - 60
	if isHalfWidth then
		widgetWidth = widgetWidth / 2
		dc:SetDimensions(widgetWidth, 55)
		dc.label:SetDimensions(widgetWidth, 26)
		dc.colorA:SetAnchor(TOPLEFT, dc.label, BOTTOMLEFT, widgetWidth - 195, 0)
		dc.colorB:SetAnchor(LEFT, dc.colorA, RIGHT, 16, 0)
	else
		dc:SetDimensions(widgetWidth, 30)
		dc.label:SetDimensions(widgetWidth - 210, 26)
		dc.colorA:SetAnchor(LEFT, dc, LEFT, widgetWidth - 195, 0)
		dc.colorB:SetAnchor(LEFT, dc.colorA, RIGHT, 16, 0)
	end

	local thumbA = wm:CreateControl(nil, dc.colorA, CT_TEXTURE)
	thumbA:SetDimensions(36, 18)
	thumbA:SetAnchor(LEFT, dc.colorA, LEFT, 4, 0)
	dc.colorA.thumb = thumbA

	local borderA = wm:CreateControl(nil, dc.colorA, CT_TEXTURE)
	borderA:SetAnchor(CENTER, thumbA, CENTER, 0, 0)
	borderA:SetDimensions(40, 22)
	borderA:SetTexture("EsoUI/Art/ChatWindow/chatOptions_bgColSwatch_frame.dds")
	borderA:SetTextureCoords(0, .625, 0, .8125)

	local thumbB = wm:CreateControl(nil, dc.colorB, CT_TEXTURE)
	thumbB:SetDimensions(36, 18)
	thumbB:SetAnchor(LEFT, dc.colorB, LEFT, 4, 0)
	dc.colorB.thumb = thumbB

	local borderB = wm:CreateControl(nil, dc.colorB, CT_TEXTURE)
	borderB:SetAnchor(CENTER, thumbB, CENTER, 0, 0)
	borderB:SetDimensions(40, 22)
	borderB:SetTexture("EsoUI/Art/ChatWindow/chatOptions_bgColSwatch_frame.dds")
	borderB:SetTextureCoords(0, .625, 0, .8125)

	local function OnMouseUp(self, btn, upInside)
		if (upInside and not self:GetParent().isDisabled) then
				local r, g, b, a = self.getFunc()
				COLOR_PICKER:Show(function(r, g, b, a) UpdateColor(self, r, g, b, a or 1) end, r, g, b, a, widgetData.name)
		end
	end

	dc.colorA.getFunc = widgetData.getFuncA
	dc.colorA.setFunc = widgetData.setFuncA
	dc.colorB.getFunc = widgetData.getFuncB
	dc.colorB.setFunc = widgetData.setFuncB

	dc.colorA:SetHandler("OnMouseUp", OnMouseUp)
	dc.colorA:SetHandler("OnMouseEnter", function() ZO_Options_OnMouseEnter(dc) end)
	dc.colorA:SetHandler("OnMouseExit", function() ZO_Options_OnMouseExit(dc) end)
	dc.colorB:SetHandler("OnMouseUp", OnMouseUp)
	dc.colorB:SetHandler("OnMouseEnter", function() ZO_Options_OnMouseEnter(dc) end)
	dc.colorB:SetHandler("OnMouseExit", function() ZO_Options_OnMouseExit(dc) end)

	if widgetData.warning then
		dc.warning = wm:CreateControlFromVirtual(nil, dc, "ZO_Options_WarningIcon")
		dc.warning:SetAnchor(RIGHT, dc.colorA, LEFT, -5, 0)
		dc.warning.data = {tooltipText = widgetData.warning}
	end

	dc.panel = panel	 --if this is in a submenu, panel is its parent
	dc.data = widgetData
	dc.data.tooltipText = widgetData.tooltip

	if widgetData.disabled then
		dc.UpdateDisabled = UpdateDisabled
		dc:UpdateDisabled()
	end
	dc.UpdateValue = UpdateValue
	dc:UpdateValue()

	if dc.panel.data.registerForRefresh or dc.panel.data.registerForDefaults then	 --if our parent window wants to refresh controls, then add this to the list
		tinsert(dc.panel.controlsToRefresh, dc)
	end

	return dc
end