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

local CreateFrame, UIDropDownMenu_SetText, tonumber = CreateFrame, UIDropDownMenu_SetText, tonumber

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

function ss:InitializeConfigPanel()
  -- Create the config panel
  local tabNav = SJTabPanel:NewInterfaceOption(AddOnName, nil, "FontExo2SemiBold", "FontExo2SemiBold", ss.SharedMedia.miscTextures.gradientCircleLg, ss.SharedMedia.miscTextures.gradientCircleLg)
  
  ss.Config = tabNav:AddTab("General", function()
    UIDropDownMenu_SetText(ss.Config.buttonFrame.SmoothAnimationsDropdown, ss.global.general.smoothAnimations)
  end)

  ss.Config._tabNav = tabNav

  -- 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"], 20)
  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 track combat check button and click event
  ss.ConfigTools.getWidget(ss.Config.buttonFrame, {
    type = "Checkbox",
    name = "TrackCombatCheckButton",
    text = "Track Combat",
    checked = ss.global.track.combat,
    onClick = 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.getWidget(ss.Config.buttonFrame, {
    type = "Checkbox",
    name = "ShowArtworkCheckButton",
    text = "Show Artwork",
    checked = ss.global.show.artwork,
    onClick = 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,
  })
  
  --TODO: How to roll this out without confusing people, some users already hide it and others have it shown, a change might leave them wondering what's going on....
  --- Could use the popup window?
  
  --  -- Add the show player frame check button and click event
  --  ss.ConfigTools.getCheckbox("ShowBlizzPlayerFrameCheckButton", "Show Default Player Frame", ss.Config.buttonFrame, ss.global.show.playerFrame, function(self)
  --    if self:GetChecked() == true then
  --      ss.PendingTask.remove("hidePlayerFrame")
  --      ss.PendingTask.add   ("showPlayerFrame", function()
  --        ss.global.show.playerFrame = true
  --        ss.Layout:UpdatePlayerFrame()
  --      end)
  --    else
  --      ss.PendingTask.remove("showPlayerFrame")
  --      ss.PendingTask.add   ("hidePlayerFrame", function()
  --        ss.global.show.playerFrame = false
  --        ss.Layout:UpdatePlayerFrame()
  --      end)
  --    end
  --  end)
  
  -- Add the map marker visibility checkbox
  ss.ConfigTools.getWidget(ss.Config.buttonFrame, {
    type = "Checkbox",
    name = "ShowMapMarkerCheckButton",
    text = "Show Map Marker",
    checked = ss.global.general.MapMarker.show,
    onClick = 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
  local smoothOptions = {
    "off",
    "fast",
    "med",
    "slow",
  }
  
  ss.ConfigTools.getWidget(ss.Config.buttonFrame, {
    type = "Dropdown",
    name = "SmoothAnimationsDropdown",
    text = "Smooth Animations",
    defaultText = ss.global.general.smoothAnimations or "",
    init = function(self, level)
      local stuff = {}
      
      for k, v in ss.pairsByKeys(smoothOptions) do
        stuff         = {}
        stuff.text    = v
        stuff.value   = v
        stuff.checked = (v == ss.global.general.smoothAnimations)
        stuff.func    = function(self)
          UIDropDownMenu_SetText(ss.Config.buttonFrame.SmoothAnimationsDropdown, self:GetText())
          ss.global.general.smoothAnimations = v
          CloseDropDownMenus()
        end
        
        UIDropDownMenu_AddButton(stuff, level)
      end
    end,
  })
  
  -- Add the use pet sphere check button and click event
  ss.ConfigTools.getWidget(ss.Config.buttonFrame, {
    type = "Checkbox",
    name = "UsePetSphereCheckButton",
    text = "Track Pet",
    checked = ss.global.track.pet,
    onClick = 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.getWidget(ss.Config.buttonFrame, {
    type = "Checkbox",
    name = "UsePowerTrackerCheckButton",
    text = "Track Additional Resource",
    checked = ss.global.track.additionalResource,
    onClick = 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 show resource gradients checkbox.
  ss.ConfigTools.getWidget(ss.Config.buttonFrame, {
    type = "Checkbox",
    name = "ShowResourceGradientsCheckButton",
    text = "Show Resource Gradients",
    checked = ss.global.show.gradient,
    onClick = 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.getWidget(ss.Config.buttonFrame, {
    type = "Checkbox",
    name = "TrackPetBattleCheckButton",
    text = "Hide Spheres During Pet Battles",
    checked = ss.global.track.petBattles,
    onClick = 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()
  
  -- Highlight the profile tab if necessary.
  ss.Config._tabNav:SetHotTab(4, (not ss.db.Initialized))
end
