-- Copyright © 2024 @Groick (Twitch). All rights reserved.
local function L(key, ...) return EasyOptions:Localize(key, ...) end
function EasyOptions:CreateCheckBox(parent, customControlName, value, text, tooltipTitleText, tooltipText, positionInfo, customFontInfo, customTextColor, onClickHandler)
  local controlName = EasyOptions:GetControlName("CheckBox", customControlName)
  local control = CreateFrame("CheckButton", controlName, parent, "InterfaceOptionsCheckButtonTemplate")
  control.Name = controlName
  control.Type = "CheckBox"
  control.BaseType = "CheckBox"
  control:SetMotionScriptsWhileDisabled(true)
  -- control:EnableMouse(true)

  control.GetValue = function(self)
    return EasyOptions:GetCheckBoxValue(self)
  end
  control.SetValue = function(self, value)
    EasyOptions:SetCheckBoxValue(self, value)
  end

  local height = 16 -- IMPORTANT: We don't use control:GetHeight() because that function returns an invalid value for this control type.

  local label = control.Text
  local labelOffsetX = 0
  local labelOffsetY = 0
  label:SetJustifyH("LEFT") -- "LEFT", "RIGHT", "CENTER"
  label:SetJustifyV("MIDDLE") -- "TOP", "BOTTOM", "MIDDLE"
  label:SetPoint("LEFT", control, "RIGHT", labelOffsetX, labelOffsetY)
  EasyOptions:SetFontFromCustomFontInfo(label, EasyOptions.Fields.DefaultFontInfo, EasyOptions.Fields.DefaultFontColor) -- We must initialize the font of this control to the default font.
  EasyOptions:SetFontFromCustomFontInfo(label, customFontInfo, customTextColor)
  control.Label = label

  control:SetValue(value)
  control.Label:SetText(text)
  local stringWidth = 0
  local stringHeight = 0
  local hasLabelText = (text ~= nil) and (text ~= "")
  if (hasLabelText) then
    stringWidth = EasyOptions:GetLabelStringWidth(label, true)
    stringHeight = EasyOptions:GetLabelStringHeight(label)
    local hitRectHeightOffset = 0.5 * (8 - math.max(stringHeight - height, 0))
    control:SetHitRectInsets(0, -1 * math.max(stringWidth + labelOffsetX, 0), hitRectHeightOffset, hitRectHeightOffset) -- Left, Right, Top, Bottom. Negative values increase the size of the hit box and positive values decrease the size of the hit box. -- IMPORTANT: This is required in order to allow the user to click the label region of the CheckBox.
    -- control.Label:SetSize(stringWidth, stringHeight) -- Set the size in order to help prevent text from wrapping.
  end
  EasyOptions:SetTooltip(control, control, tooltipTitleText, tooltipText)
  -- control.Label:SetNonSpaceWrap(false)
  
  local offsetX = 0
  local offsetY = 0
  control:ClearAllPoints()
  control:SetPoint(positionInfo[1], parent, positionInfo[2], positionInfo[3] + offsetX, positionInfo[4] + offsetY)
  
  control:SetScript("OnClick", onClickHandler)
  control.OnEnabledChangedHandler = function(self)
    -- Reserved.
  end
  EasyOptions:AddSupportForEnabledState(control)

  local controlRegionWidth = (control:GetWidth() - 7) + offsetX + EasyOptions:IIF(hasLabelText, math.max(stringWidth + labelOffsetX + 1, 0), 0)
  local controlRegionHeight = math.max(height, stringHeight)
  control.ControlRegionWidth = controlRegionWidth
  control.ControlRegionHeight = controlRegionHeight
  return control, controlRegionWidth, controlRegionHeight
end
function EasyOptions:CreateOptionCheckBox(parent, positionInfo, optionItem)
  local optionName = optionItem.Name
  local value = EasyOptions:GetOptionValueOrDefaultValue(optionName, optionItem.DefaultValue)
  local text = EasyOptions:GetControlText(optionItem)
  local tooltipTitleText = optionItem.TooltipTitleText or text
  local tooltipText = optionItem.TooltipText
  local customFontInfo = optionItem.FontInfo
  local customTextColor = optionItem.TextColor
  local onClickHandlerCustom = optionItem.OnClickHandler
  local onClickHandler = function(self, button)
    EasyOptions:OptionCheckBox_OnClickHandler(self)
    if (onClickHandlerCustom ~= nil) then
      onClickHandlerCustom(self, button)
    end
  end
  
  local control, controlRegionWidth, controlRegionHeight = EasyOptions:CreateCheckBox(parent, nil, value, text, tooltipTitleText, tooltipText, positionInfo, customFontInfo, customTextColor, onClickHandler)
  EasyOptions:AddRightButtonDownHandlerForOptionControl(control)
  
  optionItem.Control = control
  control.OptionItem = optionItem
  if (optionName ~= nil) and (optionName ~= "") then
    EasyOptions.Current.OptionControls[optionName] = control
  end
  
  return control, controlRegionWidth, controlRegionHeight
end
function EasyOptions:OptionCheckBox_OnClickHandler(control)
  control:SetValue(control:GetValue())
end
function EasyOptions:IsCheckBoxType(control)
  if (control == nil) then
    return false
  end
  if (control.BaseType == "CheckBox") or (control.BaseType == "RadioButton") then
    return true
  end
  return false
end
function EasyOptions:GetCheckBoxValue(control)
  return control:GetChecked()
end
function EasyOptions:SetCheckBoxValue(control, isChecked)
  -- Update the control.
  control:SetChecked(isChecked)

  -- Update Options (if required).
  if (not EasyOptions.Current.IsBuildingOptionFrame) then
    local allowUpdateControl = false
    EasyOptions:SetOptionValueFromControlInfo(control, isChecked, allowUpdateControl)
  end

  -- Update the enabled state of controls (if required).
  if (not EasyOptions.Current.IsBuildingOptionFrame) then -- NOTE: We don't update the enabled state of controls if EasyOptions.Current.IsBuildingOptionFrame is true because we assume that the enabled state of controls will be set to the appropriate value when each Option frame is built.
    local includeChildControls = false -- Don't update grandchild controls because we assume that each child control will update the enabled state of its children if appropriate.
    EasyOptions:UpdateEnabledStateOfChildControlsFromEnabledStateOfOwners(control, includeChildControls)
  end
end
