-- Copyright © 2023 @Groick (Twitch). All rights reserved.
local function L(key, ...) return EasyOptions:Localize(key, ...) end
function EasyOptions:CreateButton(parent, customControlName, text, tooltipTitleText, tooltipText, positionInfo, customSize, onClickHandler)
  local controlName = EasyOptions:GetControlName("Button", customControlName)
  local control = CreateFrame("Button", controlName, parent, "UIPanelButtonTemplate")
  control.Name = controlName
  control.Type = "Button"
  control.BaseType = "Button"
  control:GetFontString():SetPoint("CENTER", -0.5, -0.5)
  control:SetMotionScriptsWhileDisabled(true)
  -- control:RegisterForClicks("AnyUp")
  
  control:SetText(text)
  EasyOptions:SetTooltip(control, control, tooltipTitleText, tooltipText)

  local label = control:GetFontString()
  local stringWidth = EasyOptions:GetLabelStringWidth(label, true)
  local defaultWidth = math.max(stringWidth + 24, 110)
  local defaultHeight = EasyOptions:GetDefaultButtonHeight()
  local widthMaxAllowed = EasyOptions.Fields.ControlSizeMaxAllowed_Large
  local ignoreCustomHeight = false
  local width, height, hasWidth, hasHeight, shouldFillWidth = EasyOptions:GetSizeFromCustomSize(customSize, ignoreCustomHeight, defaultWidth, defaultHeight, widthMaxAllowed, EasyOptions.Fields.ControlSizeMaxAllowed_Large, true, false)
  control:SetSize(width, height)
  
  local offsetX = 2
  local offsetY = -4
  control:ClearAllPoints()
  control:SetPoint(positionInfo[1], parent, positionInfo[2], positionInfo[3] + offsetX, positionInfo[4] + offsetY)
  if (shouldFillWidth) then
    EasyOptions:SetControlPointToFillWidth(control, parent, positionInfo, offsetX, 2)
  end
  
  control:SetScript("OnClick", onClickHandler)
  -- control:SetScript("OnEnter", onEnterHandler)
  -- control:SetScript("OnLeave", onLeaveHandler)
  control.OnEnabledChangedHandler = function(self)
    -- Reserved.
  end
  EasyOptions:AddSupportForEnabledState(control)

  local controlRegionWidth = width
  local controlRegionHeight = height -- NOTE: We don't use control:GetHeight() because some controls don't implement that method and most controls that support that method return a value that is larger than desired.
  control.ControlRegionWidth = controlRegionWidth
  control.ControlRegionHeight = controlRegionHeight
  return control, controlRegionWidth, controlRegionHeight, shouldFillWidth
end
function EasyOptions:CreateOptionButton(parent, positionInfo, optionItem)
  local optionName = optionItem.Name
  local text = EasyOptions:GetControlText(optionItem)
  local tooltipTitleText = optionItem.TooltipTitleText or text
  local tooltipText = optionItem.TooltipText
  local customSize = optionItem.Size
  local onClickHandler = optionItem.OnClickHandler
  
  local control, controlRegionWidth, controlRegionHeight, shouldFillWidth = EasyOptions:CreateButton(parent, nil, text, tooltipTitleText, tooltipText, positionInfo, customSize, onClickHandler)
  -- EasyOptions:AddRightButtonDownHandlerForOptionControl(control)
  
  optionItem.Control = control
  control.OptionItem = optionItem
  if (optionName ~= nil) and (optionName ~= "") then
    EasyOptions.Current.OptionControls[optionName] = control
  end
  
  controlRegionWidth = controlRegionWidth - 2
  controlRegionHeight = controlRegionHeight + 0

  return control, controlRegionWidth, controlRegionHeight
end
function EasyOptions:CreateOptionReloadUIButton(parent, positionInfo, optionItem)
  optionItem.Text = optionItem.Text or L("Reload UI")
  optionItem.TooltipText = optionItem.TooltipText or L("Reload the user interface.")
  local onClickHandlerCustom = optionItem.OnClickHandler
  local onClickHandler = function(self, button)
    ReloadUI()
    if (onClickHandlerCustom ~= nil) then
      onClickHandlerCustom(self, button)
    end
  end
  optionItem.OnClickHandler = onClickHandler

  return EasyOptions:CreateOptionButton(parent, positionInfo, optionItem)
end
function EasyOptions:SetButtonIconTexture(control, iconPadding, iconTextureFilePath)
  local name
  if (control.Name == nil) then
    name = nil
  else
    name = control.Name..".Icon"
  end

  local offsetX = 0 -- 2 + 0.5 * (control:GetWidth() - control:GetHeight())
  local offsetY = 0 -- 3
  local iconTexture = control:CreateTexture(name, "ARTWORK")
  iconTexture:SetTexture(iconTextureFilePath)
  iconTexture:SetPoint("CENTER", control, "CENTER", offsetX, -offsetY) -- "LEFT", "RIGHT", "TOP", "BOTTOM", "TOPLEFT", "TOPRIGHT", "BOTTOMLEFT", "BOTTOMRIGHT", "CENTER"
  -- iconTexture:SetPoint("TOPLEFT", control, "TOPLEFT", offsetX, -offsetY) -- "LEFT", "RIGHT", "TOP", "BOTTOM", "TOPLEFT", "TOPRIGHT", "BOTTOMLEFT", "BOTTOMRIGHT", "CENTER"
  -- iconTexture:SetPoint("BOTTOMRIGHT", control, "BOTTOMRIGHT", -offsetX, offsetY) -- "LEFT", "RIGHT", "TOP", "BOTTOM", "TOPLEFT", "TOPRIGHT", "BOTTOMLEFT", "BOTTOMRIGHT", "CENTER"
  local textureSize = math.max(math.min(control:GetWidth(), control:GetHeight()) - 2 * iconPadding, 0)
  iconTexture:SetSize(textureSize, textureSize)
  control.IconTexture = iconTexture
  EasyOptions:UpdateIconTextureColor(control) -- IMPORTANT: This must be called after control.IconTexture is set.
end
function EasyOptions:UpdateIconTextureColor(control)
  if (control == nil) then
    return
  end
  if (control.IconTexture == nil) then
    return
  end

  local color
  if (control.IsEnabled == nil) or (control:IsEnabled()) then
    color = EasyOptions.Fields.DefaultHighlightColor
  else
    color = {0.5, 0.5, 0.5}
  end
  control.IconTexture:SetVertexColor(color[1], color[2], color[3], 1.0)
end
function EasyOptions:GetDefaultButtonHeight()
  return 22
end
