-------------------------
-- Configuration Panel --
-------------------------
local AddOnName, nsVars = ...
local ss = nsVars.SS

local CreateFrame, tonumber = CreateFrame, tonumber

ss["Config"] = ss["Config"] or {
};

function ss:InitializeConfigPanel()
  -- Create the config frame home
  ss.Config = ss.ConfigTools.createInterfaceOption{}

  -- Add the title
  ss.Config.widgetFrame = ss.ConfigTools.getWidgetContainer(ss.Config, "ConfigWidgetFrame")

  -- Add the title
  ss.Config.widgetFrame.Title = ss.ConfigTools.createTitle("Config", ss.Config.widgetFrame)

  -- Add the button container
  ss.Config.buttonFrame = CreateFrame("Frame",ss._f.."cfg_button_frame",ss.Config)
  ss.Config.buttonFrame:SetAllPoints(ss.Config)

  -- Add the global scale slider
  ss.Config.buttonFrame.SSScaleSlider, ss.Config.buttonFrame.SSScaleSliderEditBox = ss.ConfigTools.createSliderField({
    name = "SS32ScaleSlider",
    parent = ss.Config.buttonFrame,
    len    = 220,
    width  = 16,
    anchor = "BOTTOM",
    x      = 0,
    y      = 100,
    values = {
      label = "Global Scale",
      high  = 150,
      low   = 50,
      step  = 1,
      value = ss.global.general.scale,
    },
    onChanged = function(self)
      ss.global.general.scale = self:GetValue()
      ss.Layout:UpdateScale(self:GetValue())

      if ss.Config.buttonFrame.SSScaleSliderEditBox then
        ss.Config.buttonFrame.SSScaleSliderEditBox:SetText(self:GetValue())
      end
    end,
  },{
    maxLetters = 3,
    onEnterPressed = function(self)
      local val = self:GetText()
      if tonumber(val) then
        self:GetParent():SetValue(val)
        self:SetText(self:GetParent():GetValue())
        self:ClearFocus()
      else
        self:SetText(ss.global.general.scale)
      end
    end,
  });

  -- Add the track combat check button and click event
  ss.ConfigTools.getCheckbox("TrackCombatCheckButton", "Track Combat", ss.Config.buttonFrame, ss.global.track.combat, function(self)
    if not self:GetChecked() and ss.Layout:InCombat() then
      ss.Layout:EndCombat()
    end

    ss.global.track.combat = self:GetChecked()

    if self:GetChecked() and ss.Layout:InCombat() then
      ss.Layout:StartCombat()
    end
  end)

  -- Add the show artwork check button and click event
  ss.ConfigTools.getCheckbox("ShowArtworkCheckButton", "Show Artwork", ss.Config.buttonFrame, ss.global.show.artwork, function(self)
    ss.Layout:ShowArtwork(self:GetChecked())
    ss.global.show.artwork = self:GetChecked()
  end)

  -- Add the show player frame check button and click event
--  ss.ConfigTools.getCheckbox("ShowBlizzPlayerFrameCheckButton", "Use Player Frame", ss.Config.buttonFrame, ss.global.show.playerFrame, function(self)
--    ss.global.show.playerFrame = self:GetChecked()
--    ReloadUI()
--  end)

  -- Add the map marker visibility checkbox
  ss.ConfigTools.getCheckbox("ShowBlizzPlayerFrameCheckButton", "Show Map Marker", ss.Config.buttonFrame, ss.global.general.MapMarker.show, function(self)
    ss.global.general.MapMarker.show = self:GetChecked()
    ss.MapMarker:Show(ss.global.general.MapMarker.show)
  end)

  -- Add the smooth animate check button and click event
  ss.ConfigTools.getCheckbox("AnimateSphereValuesCheckButton", "Smooth Animations", ss.Config.buttonFrame, ss.global.general.smoothing, function(self)
    ss.global.general.smoothing = self:GetChecked()
  end)

  -- Add the use pet sphere check button and click event
  ss.ConfigTools.getCheckbox("UsePetSphereCheckButton", "Track Pet", ss.Config.buttonFrame, ss.db.track["pet"], function(self)
    ss.db.track["pet"] = self:GetChecked()
    ss.Layout:TrackPet(self:GetChecked() and ss.petAvail)
  end)

  -- Add the use power tracker check button and click event
  ss.ConfigTools.getCheckbox("UsePowerTrackerCheckButton", "Track Additional Resource", ss.Config.buttonFrame, ss.db.track["additionalResource"], function(self)
    ss.db.track["additionalResource"] = self:GetChecked()
    ss.Layout:TrackAdditionalResource(self:GetChecked() and ss.ClassResources.hasSecondaryResource())
  end)

  self:InitializeConfigPreviewPanel()
  self:InitializeConfigMicroSpheresPanel()
  self:InitializeConfigProfilePanel()
end
