-------------------------
-- 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 version.
  ss.Config.widgetFrame.version = ss.Config.widgetFrame:CreateFontString(ss._f.."Version")
  ss.Config.widgetFrame.version:SetFont     (ss.private.font["general"], 14)
  ss.Config.widgetFrame.version:SetPoint    ("BOTTOMRIGHT", -15, 15)
  ss.Config.widgetFrame.version:SetSize     (150, 25)
  ss.Config.widgetFrame.version:SetText     ("Version: ".. ss.version)
  ss.Config.widgetFrame.version:SetJustifyV ("BOTTOM")
  ss.Config.widgetFrame.version:SetJustifyH ("RIGHT")

  -- 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)
    if self:GetChecked() == true then
      ss:RemovePendingTask("hideArtwork")
      ss:AddPendingTask("showArtwork", function()
        ss.Layout:ShowArtwork(true)
        ss.global.show.artwork = true
      end)
    else
      ss:RemovePendingTask("showArtwork")
      ss:AddPendingTask("hideArtwork", function()
        ss.Layout:ShowArtwork(false)
        ss.global.show.artwork = false
      end)
    end
  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("ShowMapMarkerCheckButton", "Show Map Marker", ss.Config.buttonFrame, ss.global.general.MapMarker.show, function(self)
    if self:GetChecked() == true then
      ss:RemovePendingTask("hideMapMarker")
      ss:AddPendingTask("showMapMarker", function()
        ss.global.general.MapMarker.show = true
        ss.MapMarker:Show(ss.global.general.MapMarker.show)
      end)
    else
      ss:RemovePendingTask("showMapMarker")
      ss:AddPendingTask("hideMapMarker", function()
        ss.global.general.MapMarker.show = false
        ss.MapMarker:Show(ss.global.general.MapMarker.show)
      end)
    end
  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.global.track.pet, function(self)
    if self:GetChecked() == true then
      ss:RemovePendingTask("hidePetSphere")
      ss:AddPendingTask("usePetSphere", function()
        ss.global.track.pet = true
        ss.Layout:TrackPet(true and ss.petAvail)
      end)
    else
      ss:RemovePendingTask("usePetSphere")
      ss:AddPendingTask("hidePetSphere", function()
        ss.global.track.pet = false
        ss.Layout:TrackPet(false and ss.petAvail)
      end)
    end
  end)

  -- Add the use power tracker check button and click event
  ss.ConfigTools.getCheckbox("UsePowerTrackerCheckButton", "Track Additional Resource", ss.Config.buttonFrame, ss.global.track.additionalResource, function(self)
    if self:GetChecked() == true then
      ss:RemovePendingTask("hidePowerTracker")
      ss:AddPendingTask("usePowerTracker", function()
        ss.global.track.additionalResource = true
        ss.Layout:TrackAdditionalResource(true and ss.ClassResources.hasSecondaryResource())
      end)
    else
      ss:RemovePendingTask("usePowerTracker")
      ss:AddPendingTask("hidePowerTracker", function()
        ss.global.track.additionalResource = false
        ss.Layout:TrackAdditionalResource(false and ss.ClassResources.hasSecondaryResource())
      end)
    end
  end)

  -- Add the use power tracker check button and click event
  ss.ConfigTools.getCheckbox("ShowResourceGradientsCheckButton", "Show Resource Gradients", ss.Config.buttonFrame, ss.global.show.gradient, function(self)
    -- Add as a pending task to avoid issues manipualting frames during combat.
    if self:GetChecked() == true then
      ss:RemovePendingTask("hideGradients")
      ss:AddPendingTask("showGradients", function()
        ss.Layout:ShowGradients(true)
      end)
    else
      ss:RemovePendingTask("showGradients")
      ss:AddPendingTask("hideGradients", function()
        ss.Layout:ShowGradients(nil)
      end)
    end
  end)

  -- Load additional config panels.
  self:InitializeConfigPreviewPanel()
  self:InitializeConfigMicroSpheresPanel()
  self:InitializeConfigProfilePanel()
end
