-- Copyright © 2024 @Groick (Twitch). All rights reserved.
local function L(key, ...) return EasyOptions:Localize(key, ...) end
function EasyOptions:CreateOptionSoundChannel(parent, positionInfo, optionItem)
  optionItem.Text = optionItem.Text or L("Sound Channel")
  optionItem.TooltipText = optionItem.TooltipText or L("Specifies the sound channel that is used when playing sounds.")
  optionItem.DropDownItemInfos = optionItem.DropDownItemInfos or {{"Master", L("Master")}, {"SFX", L("SFX")}, {"Ambience", L("Ambience")}, {"Dialog", L("Dialog")}, {"Music", L("Music")}}
  
  local control, controlRegionWidth, controlRegionHeight = EasyOptions:CreateOptionDropDown(parent, positionInfo, optionItem)
  
  return control, controlRegionWidth, controlRegionHeight
end

function EasyOptions:CreateOptionSoundFilePath(parent, positionInfo, optionItem)
  if (optionItem.IncludeDefaultItemInfos) then
    optionItem.DropDownItemInfos = EasyOptions:CombineLists(optionItem.DropDownItemInfos, EasyOptions.Fields.DefaultSoundFileInfos)
  end
  local onTestHandlerCustom = optionItem.OnTestHandler
  optionItem.OnTestHandler = function(self, value)
    local optionItem = self
    local soundChannel = nil -- init
    if (optionItem.GetSoundChannel ~= nil) then
      soundChannel = optionItem:GetSoundChannel()
    end
    soundChannel = soundChannel or EasyOptions.Fields.DefaultSoundChannel
    if (value == nil) then
      EasyOptions:PrintErrorMessage(L("Unable to test the sound because no sound file has been specified."))
    elseif (type(value) ~= "string") then
      EasyOptions:PrintErrorMessage(L("Unable to test the sound because the sound file is of type \"{1}\". The sound file must be a string.", type(value)))
    else
      PlaySoundFile(value, soundChannel)
    end
    if (onTestHandlerCustom ~= nil) then
      onTestHandlerCustom(self, button)
    end
  end
  local onClickHandlerCustom = optionItem.OnClickHandler
  optionItem.OnClickHandler = function(self, info, arg1, arg2, checked)
    local value = info.value
    -- local text = info.arg1 -- NOTE: We don't use info.text because info.text is always set to nil when this method is called.
    -- local control = info.arg2
    if (EasyOptionsDB["AutoPlaySelectedSounds"]) and (value ~= nil) and (type(value) == "string") then
      optionItem:OnTestHandler(value)
    end
    if (onClickHandlerCustom ~= nil) then
      onClickHandlerCustom(self, info, arg1, arg2, checked)
    end
  end
  
  local textDefault = L("Sound File")
  local tooltipTextDefault_ValueDropDown = optionItem.TooltipText or EasyOptions:GetDefaultEditableDropDownTooltipText()..L("\n\nYou can toggle whether or not the selected sound is automatically played in EasyOptions - General options.")
  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("Sound File"), 
    TextBox2Text = nil, -- Use the default value.
    TextBox2Tooltip = L("Enter the file path to the desired sound file.\n\nSupported sound file formats include mp3 and ogg.\n\nYou can use an in-game sound by entering the ID of that sound (e.g., 569593 for the Level Up sound).\n\nYou can use a custom sound 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 sound file to that folder.\n4. Launch the game.\n5. Add an item to the drop down that contains the file path of your custom sound file.\n\nExamples:\nInterface\\AddOns\\MyFiles\\Ding.mp3\n569593"), 
    TextBox2Width = 500, -- EasyOptions:GetDefaultTextBoxWidth()
  }
  return EasyOptions:CreateOptionEditableDropDownBase(parent, positionInfo, optionItem, textDefault, tooltipTextDefault_ValueDropDown, tooltipTextDefault_ValueTextBox, textDefault_TestButton, tooltipTextDefault_TestButton, dialogTextBoxInfo)
end
