--function to make any frame object movable
local AddOnName, nsVars = ...
local ss = nsVars.SS

local CreateFrame, CreateTexture, getglobal, tonumber, table, math = CreateFrame, CreateTexture, getglobal, tonumber, table, math
local strlower, pairs, select, string = strlower, pairs, select, string
local UIDropDownMenu_SetSelectedID, UIDropDownMenu_GetText, UIDropDownMenu_AddButton = UIDropDownMenu_SetSelectedID, UIDropDownMenu_GetText, UIDropDownMenu_AddButton
local UIDropDownMenu_Initialize, UIDropDownMenu_JustifyText = UIDropDownMenu_Initialize, UIDropDownMenu_JustifyText
local UIDropDownMenu_SetText, CloseDropDownMenus = UIDropDownMenu_SetText, CloseDropDownMenus
local ColorPickerFrame, OpacitySliderFrame = ColorPickerFrame, OpacitySliderFrame

local function tryNewColors(restore)
  local r,g,b = ColorPickerFrame:GetColorRGB()
  local a = OpacitySliderFrame:GetValue()
  local fillColor = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getFillColor()

  if restore then
    r,g,b,a = ss:GetRGBAValues(restore)
  end

  if ColorPickerFrame.elementToChange == "Filling" then
    fillColor[1] = {r=r,g=g,b=b,a=a}
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setFillColor(fillColor)
  elseif ColorPickerFrame.elementToChange == "Second Filling" then
    fillColor[2] = {r=r,g=g,b=b,a=a}
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setFillColor(fillColor)
  elseif ColorPickerFrame.elementToChange == "SwirlColor" then
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setSwirlColor({r=r,g=g,b=b,a=a})
  elseif ColorPickerFrame.elementToChange == "SwirlColor1" then
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setSwirlColor({r=r,g=g,b=b,a=a}, 1)
  elseif ColorPickerFrame.elementToChange == "SwirlColor2" then
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setSwirlColor({r=r,g=g,b=b,a=a}, 2)
  elseif ColorPickerFrame.elementToChange == "SwirlColor3" then
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setSwirlColor({r=r,g=g,b=b,a=a}, 3)
  elseif ColorPickerFrame.elementToChange == "Font1" then
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setTextColor({r=r,g=g,b=b,a=a})
  elseif ColorPickerFrame.elementToChange == "Font2" then
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setTextColor2({r=r,g=g,b=b,a=a})
  end
end

local function showColorPicker(xmlElement)
  local r,g,b,a

  if xmlElement == "Filling" then
    local fillColor = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getFillColor()
    r,g,b,a = ss:GetRGBAValues(fillColor[1]) -- returns white for w/e reason

    a = a or 1
  elseif xmlElement == "SwirlColor" then
    r,g,b,a = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getSwirlColor()
  elseif xmlElement == "SwirlColor1" then
    r,g,b,a = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getSwirlColor(1)
  elseif xmlElement == "SwirlColor2" then
    r,g,b,a = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getSwirlColor(2)
  elseif xmlElement == "SwirlColor3" then
    r,g,b,a = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getSwirlColor(3)
  elseif xmlElement == "Font1" then
    r,g,b,a = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getTextColor()
  elseif xmlElement == "Font2" then
    r,g,b,a = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getTextColor2()
  elseif xmlElement == "Second Filling" then
    local fillColor = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getFillColor()
    if fillColor[2] then
      r,g,b,a = ss:GetRGBAValues(fillColor[2])
    else
      r,g,b,a = ss:GetRGBAValues(fillColor[1])
    end

    a = a or 1
  end

  --    r,g,b,a = r*255,g*255,b*255,a
  --ss.Debug.describe({r,g,b,a})


  -- on DH fill for health sphere has these values
  -- r = 0.6392142        = 162.999 (show in colorpicker)
  -- g = 0.1882348        =  47.999 (show in colorpicker)
  -- b = 0.7882335        = 200.999 (COLOR PICKER HAS 0 WTF)
  -- a = 0.999999

  ColorPickerFrame.elementToChange = xmlElement
  ColorPickerFrame.func, ColorPickerFrame.opacityFunc, ColorPickerFrame.cancelFunc = tryNewColors, tryNewColors, tryNewColors
  ColorPickerFrame:SetColorRGB(r,g,b)

  ColorPickerFrame.hasOpacity, ColorPickerFrame.opacity = (a ~= nil), a
  ColorPickerFrame.opacity = a

  ColorPickerFrame.previousValues = {r=r,g=g,b=b,a=a}
  ss:MakeFrameMovable(ColorPickerFrame)
  ColorPickerFrame:EnableKeyboard(false)
  ColorPickerFrame:Hide()
  ColorPickerFrame:Show()
end

ss["ConfigPreview"] = {
};

function ss:InitializeConfigPreviewPanel()
  -- Create the config preview frame
  ss.ConfigPreview = CreateFrame("Frame","SeamoreSpheresConfigPreview",ss.Config)
  ss.ConfigPreview:SetFrameStrata("HIGH")
  ss.ConfigPreview:SetSize(457,500)
  ss.ConfigPreview:SetPoint("RIGHT",450,0)

  -- Add the background
  ss.ConfigPreview:SetBackdrop({
    bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
    edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
    tile = true,
    tileSize = 32,
    edgeSize = 32,
    insets = {
      left = 11,
      right = 12,
      top = 12,
      bottom = 11
    },
  });

  ss.ConfigPreview:SetScript("OnHide", function(self)
    UIDropDownMenu_SetText(ss.Config.PreviewDropDown, "Select Sphere")
  end)

  ------------
  -- BOTTOM---
  ------------

  -- Add the labels
  ss.ConfigPreview.fontLayer1 = CreateFrame("Frame", "ConfigPreviewFontLayer1", ss.ConfigPreview)
  ss.ConfigPreview.fontLayer1:SetAllPoints(ss.ConfigPreview)

  ss.ConfigPreview.fontLayer1.FontString1 = ss.ConfigPreview.fontLayer1:CreateFontString("SphereTextureLabel")
  ss.ConfigPreview.fontLayer1.FontString1:SetFont(ss.private.font["general"], 19)
  ss.ConfigPreview.fontLayer1.FontString1:SetPoint("BOTTOM",-25,30)
  ss.ConfigPreview.fontLayer1.FontString1:SetSize(181, 39)
  ss.ConfigPreview.fontLayer1.FontString1:SetTextColor(1,0.5,0.5,1)
  ss.ConfigPreview.fontLayer1.FontString1:SetText("Animation")
  ss.ConfigPreview.fontLayer1.FontString1:SetJustifyV("TOP")

  ss.ConfigPreview.fontLayer1.FontString2 = ss.ConfigPreview.fontLayer1:CreateFontString("FillTextureLabel")
  ss.ConfigPreview.fontLayer1.FontString2:SetFont(ss.private.font["general"], 19)
  ss.ConfigPreview.fontLayer1.FontString2:SetPoint("BOTTOM",-172,30)
  ss.ConfigPreview.fontLayer1.FontString2:SetSize(181, 39)
  ss.ConfigPreview.fontLayer1.FontString2:SetTextColor(1,0.5,0.5,1)
  ss.ConfigPreview.fontLayer1.FontString2:SetText("Background")
  ss.ConfigPreview.fontLayer1.FontString2:SetJustifyV("TOP")

  -- Add the title
  ss.ConfigPreview.titleLayer = CreateFrame("Frame", "ConfigPreviewTitleLayer", ss.ConfigPreview)
  ss.ConfigPreview.titleLayer:SetAllPoints(ss.ConfigPreview)

  ss.ConfigPreview.titleLayer.Title = ss.ConfigPreview.titleLayer:CreateFontString(nil, "BossEmoteNormalHuge")
  ss.ConfigPreview.titleLayer.Title:SetFont(ss.private.font["general"], 26)
  ss.ConfigPreview.titleLayer.Title:SetPoint("TOP",0,-18)
  ss.ConfigPreview.titleLayer.Title:SetSize(181, 39)
  ss.ConfigPreview.titleLayer.Title:SetTextColor(1,0,0,1)
  ss.ConfigPreview.titleLayer.Title:SetText("")
  ss.ConfigPreview.titleLayer.Title:SetJustifyV("TOP")

  -- Add the button container
  ss.ConfigPreview.buttonFrame = CreateFrame("Frame",nil,ss.ConfigPreview)
  ss.ConfigPreview.buttonFrame:SetAllPoints(ss.ConfigPreview)

  -- Add the track combat check button and click event
  ss.ConfigPreview.buttonFrame.SSSpherePercentageCheckBox = CreateFrame("CheckButton","SS32SpherePercentageCheckBox",ss.ConfigPreview.buttonFrame, "UICheckButtonTemplate")
  ss.ConfigPreview.buttonFrame.SSSpherePercentageCheckBox:SetPoint("BOTTOMRIGHT",-175,112)
  getglobal(ss.ConfigPreview.buttonFrame.SSSpherePercentageCheckBox:GetName().."Text"):SetText("Show Percentage")

  ss.ConfigPreview.buttonFrame.SSSpherePercentageCheckBox:SetScript("OnClick", function(self)
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:showFont(2, self:GetChecked())
  end)

  -- Add the track combat check button and click event
  ss.ConfigPreview.buttonFrame.SSSphereValueCheckBox = CreateFrame("CheckButton","SS32SphereValueCheckBox",ss.ConfigPreview.buttonFrame, "UICheckButtonTemplate")
  ss.ConfigPreview.buttonFrame.SSSphereValueCheckBox:SetPoint("BOTTOMRIGHT",-175,87)
  getglobal(ss.ConfigPreview.buttonFrame.SSSphereValueCheckBox:GetName().."Text"):SetText("Show Value")

  ss.ConfigPreview.buttonFrame.SSSphereValueCheckBox:SetScript("OnClick", function(self)
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:showFont(1, self:GetChecked())
  end)

  ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2 = CreateFrame("Frame", "xmlSphereDisplayFrame2", ss.ConfigPreview.buttonFrame)
  ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2:SetPoint("Top",0,-60)
  ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2:SetSize(ss.private.general.SphereSize, ss.private.general.SphereSize)

  ss.Config:FillXMLTemplate(ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2)

  ss.ConfigPreview.ConfigPreviewCloseButton = CreateFrame("Button", "ConfigPreviewCloseButton", ss.ConfigPreview, "UIPanelCloseButton")
  ss.ConfigPreview.ConfigPreviewCloseButton:SetSize(35, 35)
  ss.ConfigPreview.ConfigPreviewCloseButton:SetPoint("TOPLEFT", 422, 0)
  ss.ConfigPreview.ConfigPreviewCloseButton:SetText("")
  ss.ConfigPreview.ConfigPreviewCloseButton:SetScript("OnClick", function()
    ss.ConfigPreview:Hide()
    ss.ConfigPreview.HealthSphereOptionsDropDown:Hide()
    ss.ConfigPreview.VehicleSphereOptionsDropDown:Hide()
    ss.ConfigPreview.VehicleResourceSphereOptionsDropDown:Hide()
    ss.ConfigPreview.MainResourceSphereOptionsDropDown:Hide()
    ss.ConfigPreview.AdditionalResourceSphereOptionsDropDown:Hide()
    ss.ConfigPreview.PetSphereOptionsDropDown:Hide()
  end)

  ss.ConfigPreview.CommitButton = CreateFrame("Button", "CommitButton", ss.ConfigPreview, "GameMenuButtonTemplate")
  ss.ConfigPreview.CommitButton:SetSize(133, 23)
  ss.ConfigPreview.CommitButton:SetPoint("BOTTOM", 138, 20)
  ss.ConfigPreview.CommitButton:SetText("Save Changes")
  ss.ConfigPreview.CommitButton:SetScript("OnClick", function()
    ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:dbUpdate()

    local configName = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Get("configName")
    local dbConfig = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Get("configName")
    configName = configName == "vehicle" and "health" or configName
    configName = configName == "vehicleResource" and "mainResource" or configName

    if ss.Layout.spheres[configName] and (UnitInVehicle("player") or (not UnitInVehicle("player") and dbConfig ~= "vehicle" and dbConfig ~= "vehicleResource")) then
      ss.Layout.spheres[configName]:setResource(ss.db[dbConfig])
    end
  end)

  ---------------------
  -- Dropdown Menus ---
  ---------------------

  -- HealthModeDropDown
  ss.ConfigPreview.HealthSphereOptionsDropDown = ss.Config.createDropdown{
    name = "HealthSphereOptionsDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "TOPLEFT",
    x = 0,
    y = -15,
    init = function(self, level)
      local stuff = {}

      for k,v in pairs(ss.db.health.modes[ss.profile.modeInfo.health.modeSpec]) do
        stuff = {}
        stuff.text = k
        stuff.value = k
        stuff.func = function(self)
          UIDropDownMenu_SetSelectedID(ss.ConfigPreview.HealthSphereOptionsDropDown, self:GetID())
          UIDropDownMenu_SetText(ss.ConfigPreview.HealthSphereOptionsDropDown, self:GetText())
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setMode(ss.db.health.modes[ss.profile.modeInfo.health.modeSpec][self:GetText()])
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Set("modeId", self:GetText())
          CloseDropDownMenus()
        end
        UIDropDownMenu_AddButton(stuff, level)
      end
    end,
  };

  -- VehicleModeDropDown
  ss.ConfigPreview.VehicleSphereOptionsDropDown = ss.Config.createDropdown{
    name = "VehicleSphereOptionsDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "TOPLEFT",
    x = 0,
    y = -15,
    init = function(self, level)
      local stuff = {}

      for k,v in pairs(ss.db.vehicle.modes[ss.profile.modeInfo.vehicle.modeSpec]) do
        stuff = {}
        stuff.text = k
        stuff.value = k
        stuff.func = function(self)
          UIDropDownMenu_SetSelectedID(ss.ConfigPreview.VehicleSphereOptionsDropDown, self:GetID())
          UIDropDownMenu_SetText(ss.ConfigPreview.VehicleSphereOptionsDropDown, self:GetText())
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setMode(ss.db.vehicle.modes[ss.profile.modeInfo.vehicle.modeSpec][self:GetText()])
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Set("modeId", self:GetText())
          CloseDropDownMenus()
        end
        UIDropDownMenu_AddButton(stuff, level)
      end
    end,
  };

  -- VehicleResourceModeDropDown
  ss.ConfigPreview.VehicleResourceSphereOptionsDropDown = ss.Config.createDropdown{
    name = "VehicleResourceSphereOptionsDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "TOPLEFT",
    x = 0,
    y = -15,
    init = function(self, level)
      local stuff = {}

      for k,v in pairs(ss.db.vehicleResource.modes[ss.profile.modeInfo.vehicleResource.modeSpec]) do
        stuff = {}
        stuff.text = k
        stuff.value = k
        stuff.func = function(self)
          UIDropDownMenu_SetSelectedID(ss.ConfigPreview.VehicleResourceSphereOptionsDropDown, self:GetID())
          UIDropDownMenu_SetText(ss.ConfigPreview.VehicleResourceSphereOptionsDropDown, self:GetText())
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setMode(ss.db.vehicleResource.modes[ss.profile.modeInfo.vehicleResource.modeSpec][self:GetText()])
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Set("modeId", self:GetText())
          CloseDropDownMenus()
        end
        UIDropDownMenu_AddButton(stuff, level)
      end
    end,
  };

  -- ManaModeDropDown
  ss.ConfigPreview.MainResourceSphereOptionsDropDown = ss.Config.createDropdown{
    name = "MainResourceSphereOptionsDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "TOPLEFT",
    x = 0,
    y = -15,
    init = function(self, level)
      local stuff = {}

      for k,v in pairs(ss.db.mainResource.modes[ss.profile.modeInfo.mainResource.modeSpec]) do
        stuff = {}
        stuff.text = k
        stuff.value = k
        stuff.func = function(self)
          UIDropDownMenu_SetSelectedID(ss.ConfigPreview.MainResourceSphereOptionsDropDown, self:GetID())
          UIDropDownMenu_SetText(ss.ConfigPreview.MainResourceSphereOptionsDropDown, self:GetText())
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setMode(ss.db.mainResource.modes[ss.profile.modeInfo.mainResource.modeSpec][self:GetText()])
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Set("modeId", self:GetText())
          CloseDropDownMenus()
        end
        UIDropDownMenu_AddButton(stuff, level)
      end
    end,
  };

  -- PowerModeDropDown
  ss.ConfigPreview.AdditionalResourceSphereOptionsDropDown = ss.Config.createDropdown{
    name = "AdditionalResourceSphereOptionsDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "TOPLEFT",
    x = 0,
    y = -15,
    init = function(self, level)
      if ss.db.additionalResource.modes[ss.profile.modeInfo.additionalResource.modeSpec] then
        local stuff = {}

        for k,v in pairs(ss.db.additionalResource.modes[ss.profile.modeInfo.additionalResource.modeSpec]) do
          stuff = {}
          stuff.text = k
          stuff.value = k
          stuff.func = function(self)
            UIDropDownMenu_SetSelectedID(ss.ConfigPreview.AdditionalResourceSphereOptionsDropDown, self:GetID())
            UIDropDownMenu_SetText(ss.ConfigPreview.AdditionalResourceSphereOptionsDropDown, self:GetText())
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setMode(ss.db.additionalResource.modes[ss.profile.modeInfo.additionalResource.modeSpec][self:GetText()])
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Set("modeId", self:GetText())
            CloseDropDownMenus()
          end
          UIDropDownMenu_AddButton(stuff, level)
        end
      end
    end,
  };

  -- PetModeDropDown
  ss.ConfigPreview.PetSphereOptionsDropDown = ss.Config.createDropdown{
    name = "PetSphereOptionsDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "TOPLEFT",
    x = 0,
    y = -15,
    init = function(self, level)
      if ss.db.pet.modes[ss.profile.modeInfo.pet.modeSpec] then
        local stuff = {}

        for k,v in pairs(ss.db.pet.modes[ss.profile.modeInfo.pet.modeSpec]) do
          stuff = {}
          stuff.text = k
          stuff.value = k
          stuff.func = function(self)
            UIDropDownMenu_SetSelectedID(ss.ConfigPreview.PetSphereOptionsDropDown, self:GetID())
            UIDropDownMenu_SetText(ss.ConfigPreview.PetSphereOptionsDropDown, self:GetText())
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setMode(ss.db.pet.modes[ss.profile.modeInfo.pet.modeSpec][self:GetText()])
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Set("modeId", self:GetText())
            CloseDropDownMenus()
          end
          UIDropDownMenu_AddButton(stuff, level)
        end
      end
    end,
  };

  -- TextFormattingDropdown
  local fillOptions = {
    "Truncate Value",
    "Comma Separate Value",
    "Raw Value"
  };

  ss.ConfigPreview.TextFormattingDropdown = ss.Config.createDropdown{
    name = "TextFormattingDropdown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "TOPRIGHT",
    x = 22,
    y = -15,
    init = function(self, level)
      local stuff = {}

      for k,v in pairs(fillOptions) do
        stuff = {}
        stuff.text = v
        stuff.value = v
        stuff.func = function(self)
          UIDropDownMenu_SetSelectedID(ss.ConfigPreview.TextFormattingDropdown, self:GetID())
          UIDropDownMenu_SetText(ss.ConfigPreview.TextFormattingDropdown, self:GetText())

          if self:GetText() == "Truncate Value" then
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setTextFormat("truncated")
          elseif self:GetText() == "Comma Separate Value" then
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setTextFormat("commaSeparated")
          elseif self:GetText() == "Raw Value" then
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setTextFormat("raw")
          end

          ss.Config:UpdateXMLText()
          CloseDropDownMenus()
        end
        UIDropDownMenu_AddButton(stuff, level)
      end
    end,
  };

  -- FillTexturesDropDown
  local textureNameTable = ss.FillTextures.list("primary")
  ss.ConfigPreview.FillTexturesDropDown = ss.Config.createDropdown{
    name = "FillTexturesDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "BOTTOM",
    x = -125,
    y = 15,
    init = function(self, level)
      local stuff = {}
      for k,v in pairs(textureNameTable) do
        stuff = {}
        stuff.text = k
        stuff.value = k
        stuff.func = function(self)
          UIDropDownMenu_SetSelectedID(ss.ConfigPreview.FillTexturesDropDown, self:GetID())
          UIDropDownMenu_SetText(ss.ConfigPreview.FillTexturesDropDown, self:GetText())
          ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setFillTexture("primary", self:GetText())
          CloseDropDownMenus()
        end
        stuff.checked = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getFillTexture() == v
        UIDropDownMenu_AddButton(stuff, level)
      end
    end,
  };

  -- Animation dropdown
  local animationNameTable = ss.Animations:List()
  ss.ConfigPreview.RotationTexturesDropDown = ss.Config.createDropdown{
    name = "RotationTexturesDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 200,
    width = 45,
    anchor = "BOTTOM",
    x = 12,
    y = 15,
    init = function(self, level, menuList)
      local stuff = {};

      if level == 1 then
        for k,v in pairs(animationNameTable) do
          stuff.text = k
          stuff.menuList = k
          stuff.hasArrow = true
          stuff.checked = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Get("animGroup") == k
          UIDropDownMenu_AddButton(stuff, level)
        end
      elseif level == 2 then
        for k,v in pairs(animationNameTable[menuList]) do
          stuff.text = k
          stuff.value = k
          stuff.checked = ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:Get("animId") == k
          stuff.func = function(self)
            UIDropDownMenu_SetText(ss.ConfigPreview.RotationTexturesDropDown, self:GetText())
            ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setAnimationDisplayInfo(ss.ConfigPreview.RotationTexturesDropDown.menuList,self:GetText())
            CloseDropDownMenus()
          end

          UIDropDownMenu_AddButton(stuff, level)
        end
      end
    end,
  };

  -- Display the "slider target" dropdown to determine which value the slider should manage
  ss.ConfigPreview.sliderTargets = {
    ["Swirl"] = {
      "Speed",
      "Color",
      "Color1",
      "Color2",
      "Color3",
    },
    ["Filling"] = {
      "Color",
    },
    ["Font1"] = {
      "Color",
    },
    ["Font2"] = {
      "Color",
    },
    ["Animation"] = {
      "Alpha",
      "Scale",
      "Strata",
      "X Position",
      "Y Position",
    },
  };

  local targetSliderValues = {
    ["Swirl"] = {
      ["Speed"] = {
        ["low"]  = 1,
        ["high"] = 10,
        ["step"] = 1,
      },
    },
    ["Animation"] = {
      ["Alpha"] = {
        ["low"]  = 0,
        ["high"] = 1,
        ["step"] = 0.001,
      },
      ["Scale"] = {
        ["low"]  = 0.001,
        ["high"] = 6,
        ["step"] = 0.001,
      },
      ["Strata"] = {
        ["low"]  = 1,
        ["high"] = 5,
        ["step"] = 0.001,
      },
      ["X Position"] = {
        ["low"]  = -2,
        ["high"] = 2,
        ["step"] = 0.001,
      },
      ["Y Position"] = {
        ["low"]  = -2,
        ["high"] = 2,
        ["step"] = 0.001,
      },
    },
  };

  local function targetSliderGetValue(target)
    if target == "Animation - Scale" then
      return ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getAnimationScale()
    elseif target == "Animation - X Position" then
      return ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getAnimationOffset()
    elseif target == "Animation - Y Position" then
      return select(2, ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getAnimationOffset())
    elseif target == "Animation - Strata" then
      return ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getAnimationStrata() - ss.private.layout.config.frameLevelAdjust
    elseif target == "Animation - Alpha" then
      return ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getAnimationAlpha()
    elseif target == "Swirl - Speed" then
      return ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:getSwirlSpeed()
    else
      return 1
    end
  end

  ss.ConfigPreview.SliderTargetDropDown = ss.Config.createDropdown{
    name = "SliderTargetDropDown",
    parent = ss.ConfigPreview,
    tAlign = "LEFT",
    len = 300,
    width = 45,
    anchor = "BOTTOMLEFT",
    x = 5,
    y = 112,
    init = function(self, level, menuList)
      local stuff = {};
      stuff.notCheckable = true

      if level == 1 then
        for k,v in pairs(ss.ConfigPreview.sliderTargets) do
          stuff.text = k
          stuff.menuList = k
          stuff.hasArrow = true

          UIDropDownMenu_AddButton(stuff, level)
        end
      elseif level == 2 then
        for k,v in pairs(ss.ConfigPreview.sliderTargets[menuList]) do
          stuff.text = v
          stuff.value = menuList
          stuff.func = function(self)
            if string.match(self:GetText(), "Color") then
              if self.value == "Filling" then
                ColorPickerFrame:Hide()
                showColorPicker((self:GetText() == "Color2") and "Second Filling" or self.value)
              elseif self.value == "Swirl" then
                ColorPickerFrame:Hide()
                showColorPicker(self.value..self:GetText())
              else
                ColorPickerFrame:Hide()
                showColorPicker(self.value)
              end
            else
              local target = self.value .. " - " .. self:GetText()
              local sliderVals = targetSliderValues[self.value][self:GetText()]

              UIDropDownMenu_SetSelectedID(ss.ConfigPreview.SliderTargetDropDown, self:GetID())
              UIDropDownMenu_SetText(ss.ConfigPreview.SliderTargetDropDown, target)

              getglobal(ss.ConfigPreview.SSTargetSlider:GetName().."High"):SetText(sliderVals.high)
              getglobal(ss.ConfigPreview.SSTargetSlider:GetName().."Low"):SetText(sliderVals.low)
              ss.ConfigPreview.SSTargetSlider:SetMinMaxValues(sliderVals.low,sliderVals.high)
              ss.ConfigPreview.SSTargetSlider:SetValueStep(sliderVals.step)
              ss.ConfigPreview.SSTargetSlider:SetValue(targetSliderGetValue(target))

              ss.ConfigPreview.SSTargetSlider:Show()
            end

            CloseDropDownMenus()
          end

          UIDropDownMenu_AddButton(stuff, level)
        end
      end
    end,
  };

  ss.ConfigPreview.SSTargetSlider, ss.ConfigPreview.SSTargetSliderEditBox = ss.Config.createSliderField({
    name = "SSTargetSlider",
    parent = ss.ConfigPreview,
    len = 200,
    width = 16,
    anchor = "BOTTOMLEFT",
    x = 25,
    y = 90,
    onChanged = function(self)
      local target = UIDropDownMenu_GetText(ss.ConfigPreview.SliderTargetDropDown)

      if target == "Animation - Scale" then
        ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setAnimationScale(self:GetValue())
      elseif target == "Animation - X Position" then
        ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setAnimationOffset(self:GetValue(), nil)
      elseif target == "Animation - Y Position" then
        ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setAnimationOffset(nil,self:GetValue())
      elseif target == "Animation - Strata" then
        ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setAnimationStrata(self:GetValue() + ss.private.layout.config.frameLevelAdjust)
      elseif target == "Animation - Alpha" then
        ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setAnimationAlpha(self:GetValue())
      elseif target == "Swirl - Speed" then
        ss.ConfigPreview.buttonFrame.xmlSphereDisplayFrame2.sphere:setSwirlSpeed(self:GetValue())
      end

      if ss.ConfigPreview.SSTargetSliderEditBox then
        ss.ConfigPreview.SSTargetSliderEditBox:SetText(self:GetValue())
      end
    end,
  },{
    maxLetters = 5,
    onEnterPressed = function(self)
      local val = self:GetText()
      if tonumber(val) then
        self:GetParent():SetValue(val)
        self:SetText(self:GetParent():GetValue())
        self:ClearFocus()
      end
    end,
  });

  ss.ConfigPreview.SSTargetSlider:Hide()
end