-------------------------
-- 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.PendingTask.remove("hideArtwork")
      ss.PendingTask.add   ("showArtwork", function()
        ss.Layout:ShowArtwork(true)
        ss.global.show.artwork = true
      end)
    else
      ss.PendingTask.remove("showArtwork")
      ss.PendingTask.add   ("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.PendingTask.remove("hideMapMarker")
      ss.PendingTask.add   ("showMapMarker", function()
        ss.global.general.MapMarker.show = true
        ss.MapMarker:Show(ss.global.general.MapMarker.show)
      end)
    else
      ss.PendingTask.remove("showMapMarker")
      ss.PendingTask.add   ("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.PendingTask.remove("hidePetSphere")
      ss.PendingTask.add   ("usePetSphere", function()
        ss.global.track.pet = true
        ss.Layout:TrackPet(true and ss.petAvail)
      end)
    else
      ss.PendingTask.remove("usePetSphere")
      ss.PendingTask.add   ("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.PendingTask.remove("hidePowerTracker")
      ss.PendingTask.add   ("usePowerTracker", function()
        ss.global.track.additionalResource = true
        ss.Layout:TrackAdditionalResource(true and ss.ClassResources.hasSecondaryResource())
      end)
    else
      ss.PendingTask.remove("usePowerTracker")
      ss.PendingTask.add   ("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.PendingTask.remove("hideGradients")
      ss.PendingTask.add   ("showGradients", function()
        ss.Layout:ShowGradients(true)
      end)
    else
      ss.PendingTask.remove("showGradients")
      ss.PendingTask.add   ("hideGradients", function()
        ss.Layout:ShowGradients(nil)
      end)
    end
  end)
  
  -- Add the track pet battle check button and click event.
  ss.ConfigTools.getCheckbox("TrackPetBattleCheckButton", "Hide Spheres During Pet Battles", ss.Config.buttonFrame, ss.global.track.petBattles, function(self)
    if self:GetChecked() == true then
      ss.PendingTask.remove("dontTrackPetBattle")
      ss.PendingTask.add   ("trackPetBattle", function()
        ss.global.track.petBattles = true
      
        if ss.isInPetBattle then
          ss:OnPetBattleStart()
        end
      end)
    else
      ss.PendingTask.remove("trackPetBattle")
      ss.PendingTask.add   ("dontTrackPetBattle", function()
        if ss.isInPetBattle then
          ss:OnPetBattleEnd()
          ss.isInPetBattle = true
        end
        
        ss.global.track.petBattles = false
      end)
    end
  end)

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