-- ========================================================================= --
--                              SylingTracker                                --
--           https://www.curseforge.com/wow/addons/sylingtracker             --
--                                                                           --
--                               Repository:                                 --
--                   https://github.com/Skamer/SylingTracker                 --
--                                                                           --
-- ========================================================================= --
Syling                "SylingTracker.AutoQuests.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
-- ========================================================================= --
-- NOTE: We don't inherit from "ContentView" because we don't need the header 
__Recyclable__ "SylingTracker_AutoQuestsContentView%d"
class "AutoQuestsContentView" (function(_ENV)
  inherit "Frame" extend "IView"
  function OnViewUpdate(self, data)
    if data.quests then 
      local autoQuests = self:AcquireAutoQuests()
      autoQuests:UpdateView(data.quests)
    else 
      self:ReleaseAutoQuests()
    end
  end

  function OnAdjustHeight(self, useAnimation)
    local maxOuterBottom 

    for childName, child in IterateFrameChildren(self) do 
      local outerBottom = child:GetBottom() 
      if outerBottom then 
        if not maxOuterBottom or maxOuterBottom > outerBottom then 
          maxOuterBottom = outerBottom
        end 
      end 
    end
    
    if maxOuterBottom then 
      local computeHeight = self:GetTop() - maxOuterBottom + self.PaddingBottom
      if useAnimation then 
        self:SetAnimatedHeight(computeHeight)
      else 
        self:SetHeight(computeHeight)
      end
    end
  end

  function AcquireAutoQuests(self)
    local autoQuests = self:GetChild("AutoQuests")
    if not autoQuests then 
      autoQuests = AutoQuestListView.Acquire()

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

      autoQuests:SetParent(self)
      autoQuests:SetName("AutoQuests")

      -- It's important to only style it once we have set its parent and its new
      -- name
      if self.AutoQuests then 
        Style[autoQuests] = self.AutoQuests
      end

      -- Register the events 
      autoQuests.OnSizeChanged = autoQuests.OnSizeChanged + self.OnAutoQuestsSizeChanged

      self:AdjustHeight(true)
    end

    return autoQuests
  end

  function ReleaseAutoQuests(self)
    local autoQuests = self:GetChild("AutoQuests")
    if autoQuests then 
      -- Give its old name (generated by the recycle system)
      autoQuests:SetName(self.__PreviousAutoQuestsName)
      self.__PreviousAutoQuestsName = nil 

      -- Unregister the events
      autoQuests.OnSizeChanged = autoQuests.OnSizeChanged - self.OnAutoQuestsSizeChanged

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

      self:AdjustHeight()
    end
  end

  function OnRelease(self)
    self:ReleaseAutoQuests()

    self:ClearAllPoints()
    self:SetParent()
    self:Hide()
    self:CancelAdjustHeight()
    self:CancelAnimatingHeight()

    self:SetHeight(1)

    ResetStyles(self)
  end

  function OnAcquire(self)
    -- Important ! We need the frame is instantly styled as this may affect 
    -- its height.
    self:InstantApplyStyle()

    self:Show()
    self:AdjustHeight(true)
  end

  -----------------------------------------------------------------------------
  --                               Properties                                --
  -----------------------------------------------------------------------------
  property "PaddingBottom" {
    type    = Number,
    default = 0
  }

  property "AutoQuests" {
    type = Table
  }
  -----------------------------------------------------------------------------
  --                            Constructors                                 --
  -----------------------------------------------------------------------------
  function __ctor(self)
    -- Important! As the frame ajusts its height depending of its children height
    -- we need to set its height when contructed for the event "OnSizechanged" of
    -- its children is triggered.
    self:SetHeight(1) -- !important

    self:SetClipsChildren(true)

    self.OnAutoQuestsSizeChanged = function() self:AdjustHeight(true) end
  end
end)
-------------------------------------------------------------------------------
--                                Styles                                     --
-------------------------------------------------------------------------------
Style.UpdateSkin("Default", {
  [AutoQuestsContentView] = {
    AutoQuests = {
      location = {
        Anchor("TOP"),
        Anchor("LEFT"),
        Anchor("RIGHT")
      }
    }
  }
})
