-- Copyright © 2024 @Groick (Twitch). All rights reserved.
local function L(key, ...) return EasyOptions:Localize(key, ...) end
function EasyOptions:CreateOptionCustomControl(parent, positionInfo, optionItem)
  local optionName = optionItem.Name
  local text = EasyOptions:GetControlText(optionItem)
  local tooltipTitleText = optionItem.TooltipTitleText or text
  local tooltipText = optionItem.TooltipText
  
  -- Validate optionItem.GetCustomControl.
  if (optionItem.GetCustomControl == nil) then
    EasyOptions:PrintErrorMessageWithOptionItemInfo(L("cannot get its CustomControl because the item's GetCustomControl method is nil."))
    return nil, 0, 0
  end

  -- Attempt to get the custom control from the external addon.
  local control, controlRegionWidth, controlRegionHeight = optionItem.GetCustomControl(parent, optionItem)
  if (control == nil) then
    return nil, controlRegionWidth or 0, controlRegionHeight or 0
  elseif (not EasyOptions:IsFrame(control, false)) then
    EasyOptions:PrintErrorMessageWithOptionItemInfo(L("has a GetCustomControl method that returned a value of {1} for its control which is not a valid type.", EasyOptions:Format(control)))
    return nil, controlRegionWidth or 0, controlRegionHeight or 0
  end
  if (controlRegionWidth == nil) then
    if (control.GetWidth ~= nil) then
      controlRegionWidth = control:GetWidth()
    else
      controlRegionWidth = 0
    end
  end
  if (controlRegionHeight == nil) then
    if (control.GetHeight ~= nil) then
      controlRegionHeight = control:GetHeight()
    else
      controlRegionHeight = 0
    end
  end
  
  if (control:GetParent() ~= parent) and (parent ~= nil) then
    control:SetParent(parent)
  end
  control:ClearAllPoints()
  control:SetPoint(positionInfo[1], parent, positionInfo[2], positionInfo[3], positionInfo[4])
  
  optionItem.Control = control
  control.OptionItem = optionItem
  if (optionName ~= nil) and (optionName ~= "") then
    EasyOptions.Current.OptionControls[optionName] = control
  end
  
  if (optionItem.TooltipTitleText ~= nil) or (tooltipText ~= nil) then
    EasyOptions:SetTooltip(control, control, tooltipTitleText, tooltipText)
  end

  EasyOptions:AddRightButtonDownHandlerForOptionControl(control)
    
  if (control.OnEnabledChangedHandler == nil) then
    control.OnEnabledChangedHandler = function(self)
      -- Reserved.
    end
  end
  EasyOptions:AddSupportForEnabledState(control) -- Passing your control to the EasyOptions:AddSupportForEnabledState method will implement the methods and properties that allow EasyOptions to automatically disable your control when a parent CheckBox is unchecked.

  -- Override the SetValue method if the method exists.
  if (control.SetValue ~= nil) then
    local setValueBase = control.SetValue
    control.SetValue = function(self, value)
      local control = self
      local optionItem = control.OptionItem
      setValueBase(self, value) -- Call the base method.
      if (optionItem ~= nil) and (not EasyOptions.Current.IsBuildingOptionFrame) then
        local allowUpdateControl = false
        EasyOptions:SetOptionValueFromControlInfo(control, value, allowUpdateControl)
      end
    end
    local value = EasyOptions:GetOptionValueOrDefaultValue(optionName, optionItem.DefaultValue) -- Get the current value associated with the control.
    control:SetValue(value) -- Set the control's value to the current value in the addon's database.
  end

  return control, controlRegionWidth, controlRegionHeight
end
