-- Copyright © 2024 @Groick (Twitch). All rights reserved.
local function L(key, ...) return EasyOptions:Localize(key, ...) end
function EasyOptions:CreateOptionFontFilePath(parent, positionInfo, optionItem)
  if (optionItem.IncludeDefaultItemInfos) then
    optionItem.DropDownItemInfos = EasyOptions:CombineLists(optionItem.DropDownItemInfos, EasyOptions.Fields.DefaultFontFileInfos)
  end

  local onOptionValueChangedHandlerCustom = optionItem.OnOptionValueChangedHandler
  optionItem.OnOptionValueChangedHandler = function(self)
    local control = self.Control
    control:UpdateSampleLabel()
  end
  
  local textDefault = L("Font")
  local tooltipTextDefault_ValueDropDown = nil
  local tooltipTextDefault_ValueTextBox = nil
  local textDefault_TestButton = nil
  local tooltipTextDefault_TestButton = nil
  local dialogTextBoxInfo = {
    TextBoxCount = nil, -- Use the default value. -- 0, 1, or 2
    TextBox1LabelText = nil, -- Use the default value.
    TextBox1Text = nil, -- Use the default value.
    TextBox1Tooltip = nil, -- Use the default value.
    TextBox1Width = nil, -- Use the default value.
    TextBox2LabelText = L("Font Path"), 
    TextBox2Text = nil, -- Use the default value.
    TextBox2Tooltip = L("Enter the file path to the desired font.\n\nSupported font formats include TrueType (.ttf) or OpenType Format (.otf).\n\nYou can use a custom font by doing the following:\n1. Exit the game.\n2. Create a folder in your World of Warcraft addons folder (e.g., Interface\\AddOns\\MyFiles\\).\n3. Copy your custom font to that folder.\n4. Launch the game.\n5. Add an item to the drop down that contains the file path of your custom font.\n\nExample:\nInterface\\AddOns\\MyFiles\\MyFont.otf"), 
    TextBox2Width = 500, -- EasyOptions:GetDefaultTextBoxWidth()
  }
  local control, controlRegionWidth, controlRegionHeight = EasyOptions:CreateOptionEditableDropDownBase(parent, positionInfo, optionItem, textDefault, tooltipTextDefault_ValueDropDown, tooltipTextDefault_ValueTextBox, textDefault_TestButton, tooltipTextDefault_TestButton, dialogTextBoxInfo)

  local labelControl, labelControlRegionWidth, labelControlRegionHeight = EasyOptions:CreateOptionFontFilePath_CreateSampleLabel(parent, control)
  controlRegionWidth = controlRegionWidth + labelControlRegionWidth
  controlRegionHeight = math.max(controlRegionHeight, labelControlRegionHeight)

  control.UpdateSampleLabel = function(self)
    local control = self
    local optionItem = control.OptionItem
    local optionName = optionItem.Name
    local value = optionItem:GetOptionValue() or EasyOptions.Fields.DefaultFontFilePath
    local label = control.SampleLabel
    local customFontInfo = value
    local customTextColor = nil
    EasyOptions:SetFontFromCustomFontInfo(label, customFontInfo, customTextColor)
  end
  control:UpdateSampleLabel()
  
  return control, controlRegionWidth, controlRegionHeight
end
function EasyOptions:CreateOptionFontFilePath_CreateSampleLabel(parent, control)
  -- Determine parameters.
  local controlName = control.Name
  local controlLabelText = control.ValueDropDown.Label:GetText()
  local hasControlLabelText = (controlLabelText ~= nil) and (controlLabelText ~= "")
  local text_Sample = L("Sample")
  local text_SampleLabelLabel = EasyOptions:IIF(hasControlLabelText, text_Sample, nil) -- NOTE: We don't set the text of SampleLabel's label if control's label does not have text because doing so can cause SampleLabel's label to extend above control's region which could cause SampleLabel's label to overlap a previous control.
  local text_SampleLabel = L("Abc123")
  local tooltipTitleText = text_Sample
  local tooltipText = L("Displays sample text using the selected font.")
  --
  local sampleLabelOffsetX = 0
  local sampleLabelOffsetY = -2
  local sampleLabelLabelOffsetX = -4 + sampleLabelOffsetX
  local sampleLabelLabelOffsetY = 5 - sampleLabelOffsetY
  local positionInfo_SampleLabelLabel = {"BOTTOMLEFT", "TOPLEFT", sampleLabelLabelOffsetX, sampleLabelLabelOffsetY} -- "LEFT", "RIGHT", "TOP", "BOTTOM", "TOPLEFT", "TOPRIGHT", "BOTTOMLEFT", "BOTTOMRIGHT", "CENTER"
  local positionInfo_SampleLabel = {"TOPLEFT", "TOPRIGHT", sampleLabelOffsetX, sampleLabelOffsetY} -- "LEFT", "RIGHT", "TOP", "BOTTOM", "TOPLEFT", "TOPRIGHT", "BOTTOMLEFT", "BOTTOMRIGHT", "CENTER"

  -- Create the SampleLabel.
  local labelAnchorControl = EasyOptions:IIF(control.TestButton ~= nil, control.TestButton, control.RestoreDefaultItemsButton)
  local controlTypeName = "Label"
  local customControlName = EasyOptions:IIF(controlName ~= nil, controlName.."."..controlTypeName, nil)
  local fontObjectName = EasyOptions.Fields.DefaultLabelFontObjectName
  local customSize = nil
  local wordWrap = nil
  local customFontInfo = nil -- NOTE: The font for the SampleLabel is set elsewhere.
  local customTextColor = nil
  local includeUnderline = false
  local customUnderlineWidth = nil
  local frameTemplate = nil
  local labelControl, labelControlRegionWidth, labelControlRegionHeight = EasyOptions:CreateLabel(parent, labelAnchorControl, controlTypeName, customControlName, text_SampleLabel, tooltipTitleText, tooltipText, fontObjectName, positionInfo_SampleLabel, customSize, wordWrap, customFontInfo, customTextColor, includeUnderline, customUnderlineWidth, frameTemplate)
  labelControlRegionWidth = 1.0 * labelControlRegionWidth + sampleLabelOffsetX -- Account for possible changes in the width of the label's text when the label's font is changed.
  
  -- Create the label for the SampleLabel.
  local ownerControl = nil -- IMPORTANT: We must not set ownerControl = labelControl because doing so will cause labelControl.Label to be changed.
  local labelControlLabel, labelLabel, labelControlLabelRegionWidth, labelControlLabelRegionHeight = EasyOptions:CreateLabelForControl(parent, ownerControl, labelControl, labelControl.Name, text_SampleLabelLabel, tooltipTitleText, tooltipText, positionInfo_SampleLabelLabel, fontObjectName, customFontInfo, customTextColor)
  local hasLabelText = (text_SampleLabelLabel ~= nil) and (text_SampleLabelLabel ~= "")
  if (hasLabelText) then
    labelControlRegionWidth = math.max(labelControlRegionWidth + 0, labelControlLabelRegionWidth + 0)
    labelControlRegionHeight = (labelControlRegionHeight - sampleLabelOffsetY) + (labelControlLabelRegionHeight + (sampleLabelLabelOffsetY + sampleLabelOffsetY)) + 0
  end
  
  control.SampleLabel = labelControl.Label
  control.SampleLabelControl = labelControl
  EasyOptions:AddControlsToOwner({labelControlLabel, labelControl}, control)

  return labelControl, labelControlRegionWidth, labelControlRegionHeight
end

function EasyOptions:CreateOptionFontSize(parent, positionInfo, optionItem)
  optionItem.Text = optionItem.Text or L("Font Size")
  optionItem.TooltipText = optionItem.TooltipText or L("Specifies the font size.")
  optionItem.MinValue = optionItem.MinValue or 4
  optionItem.MaxValue = optionItem.MaxValue or EasyOptions.Fields.FontSizeMaxAllowed
  optionItem.ValueStep = optionItem.ValueStep or 1
  optionItem.FormatString = optionItem.FormatString or "%i"
  return EasyOptions:CreateOptionSlider(parent, positionInfo, optionItem)
end
