-- ========================================================================= --
--                              SylingTracker                                --
--           https://www.curseforge.com/wow/addons/sylingtracker             --
--                                                                           --
--                               Repository:                                 --
--                   https://github.com/Skamer/SylingTracker                 --
--                                                                           --
-- ========================================================================= --
Syling                "SylingTracker.Achievements.ContentView"               ""
-- ========================================================================= --
namespace                          "SLT"
-- ========================================================================= --
-- Iterator helper for ignoring the children are used for backdrop, and avoiding
-- they are taken as account for their parent height
IterateFrameChildren = Utils.IterateFrameChildren
ResetStyles           = Utils.ResetStyles
ShowContextMenu       = API.ShowContextMenu
ValidateFlags         = System.Toolset.validateflags

__Recyclable__ "SylingTracker_AchievementsContentView%d"
class "AchievementsContentView" (function(_ENV)
  inherit "ContentView"

  __Flags__()
  enum "Flags" {
    NONE = 0,
    HAS_ACHIEVEMENTS = 1
  }

  function OnViewUpdate(self, data)
    local achievementsData = data.achievements

    -- Determines the flags 
    local flags = Flags.NONE 
    if achievementsData then 
      flags = Flags.HAS_ACHIEVEMENTS
    end 

    if flags ~= self.Flags then 
      ResetStyles(self)

      -- are there achievements 
      if ValidateFlags(Flags.HAS_ACHIEVEMENTS, flags) then 
        self:AcquireAchievements()
      else
        self:ReleaseAchievements()
      end

      -- Styling stuff 
      if flags ~= Flags.NONE then 
        local styles = self.FlagsStyles and self.FlagsStyles[flags]
        if styles then 
          Style[self] = styles 
        end 
      end
    end

    -- Update 
    if achievementsData then 
      local achievementsView = self:AcquireAchievements()
      achievementsView:UpdateView(achievementsData)
    end

    self.Flags = flags 
  end


  function AcquireAchievements(self)
    local content = self:GetChild("Content")
    local achievements = content:GetChild("Achievements")
    if not achievements then 
      achievements = AchievementListView.Acquire()

      -- We need to keep the old name when we'll release it 
      self.__PreviousAchievementsName = achievements:GetName()

      achievements:SetParent(content)
      achievements:SetName("Achievements")
      achievements:InstantApplyStyle()

      -- Register the events 
      achievements.OnSizeChanged = achievements.OnSizeChanged + self.OnAchievementsSizeChanged

      self:AdjustHeight(true)
    end
    
    return achievements
  end

  function ReleaseAchievements(self)
    local content = self:GetChild("Content")
    local achievements = content:GetChild("Achievements")
    if achievements then 
      -- Give its old name (generated by the recycle system)
      achievements:SetName(self.__PreviousAchievementsName)
      self.__PreviousAchievementsName = nil

      -- Unregister the events
      achievements.OnSizeChanged = achievements.OnSizeChanged - self.OnAchievementsSizeChanged

      -- It's better to release it after the event has been unregistered for avoiding
      -- useless call
      achievements:Release()

      self:AdjustHeight(true)
    end
  end

  function OnRelease(self)
    -- First, release the children 
    self:ReleaseAchievements()

    -- We call the "Parent" OnRelease (see, ContentView)
    super.OnRelease(self)

    -- Reset property
    self.Flags = nil 
  end
  -----------------------------------------------------------------------------
  --                               Properties                                --
  -----------------------------------------------------------------------------
  property "FlagsStyles" {
    type = Table
  }

  property "Flags" {
    type = AchievementsContentView.Flags,
    default = AchievementsContentView.Flags.NONE
  }
  -----------------------------------------------------------------------------
  --                            Constructors                                 --
  -----------------------------------------------------------------------------
  __Template__{}
  function __ctor(self)
    self.OnAchievementsSizeChanged = function() self:AdjustHeight(true) end
  end
end)

Style.UpdateSkin("Default", {
  [AchievementsContentView] = {
    Header = {
      IconBadge = {
        backdropColor = { r = 0, g = 0, b = 0, a = 0},
        Icon = {
          file = [[Interface\ACHIEVEMENTFRAME\UI-ACHIEVEMENT-SHIELDS]],
          texCoords = RectType(0, 64/128, 0, 64/128)
        }
      },

      Label = {
        text = "Achievements"
      }
    },

    Content = {
      location = {
        Anchor("TOP", 0, -5, "Header", "BOTTOM"),
        Anchor("LEFT", 5, 0),
        Anchor("RIGHT", -5, 0)
      }
    },

    FlagsStyles = {
      [AchievementsContentView.Flags.HAS_ACHIEVEMENTS] = {
        Content = {
          Achievements = {
            location = {
              Anchor("TOP"),
              Anchor("LEFT"),
              Anchor("RIGHT")
            }
          }
        }
      }
    }
  }
})