-- ========================================================================= --
--                              SylingTracker                                --
--           https://www.curseforge.com/wow/addons/sylingtracker             --
--                                                                           --
--                               Repository:                                 --
--                   https://github.com/Skamer/SylingTracker                 --
--                                                                           --
-- ========================================================================= --
Syling                "SylingTracker.WorldQuests.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_WorldQuestsContentView%d"
class "WorldQuestsContentView" (function(_ENV)
  inherit "ContentView"

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

  function OnViewUpdate(self, data)
    local questsData = data.quests

    -- Determines the flags 
    local flags = Flags.NONE
    if questsData then 
      flags = Flags.HAS_WORLD_QUESTS
    end

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

      -- are there world quests 
      if ValidateFlags(Flags.HAS_WORLD_QUESTS, flags) then 
        self:AcquireWorldQuests()
      else
        self:ReleaseWorldQuests()
      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 questsData then 
      local worldQuests = self:AcquireWorldQuests()
      worldQuests:UpdateView(questsData)
    end
    
    self.Flags = flags
  end

  function AcquireWorldQuests(self)
    local content = self:GetChild("Content")
    local worldQuests = content:GetChild("WorldQuests")
    if not worldQuests then 
      worldQuests = WorldQuestListView.Acquire()

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

      worldQuests:SetParent(content)
      worldQuests:SetName("WorldQuests")
      worldQuests:InstantApplyStyle()

      -- Register the events
      worldQuests.OnSizeChanged = worldQuests.OnSizeChanged + self.OnChildrenSizeChanged

      self:AdjustHeight(true)
    end
    
    return worldQuests
  end

  function ReleaseWorldQuests(self)
    local content = self:GetChild("Content")
    local worldQuests = content:GetChild("WorldQuests")
    if worldQuests then 
      -- Give its old name (generated by the recycle system)
      worldQuests:SetName(self.__PreviousWorldQuestsName)
      self.__PreviousWorldQuestsName = nil

      -- Unregister the events
      worldQuests.OnSizeChanged = worldQuests.OnSizeChanged - self.OnChildrenSizeChanged

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

      self:AdjustHeight(true)
    end
  end

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

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

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

  property "Flags" {
    type = WorldQuestsContentView.Flags,
    default = WorldQuestsContentView.Flags.NONE
  }
  -----------------------------------------------------------------------------
  --                            Constructors                                 --
  -----------------------------------------------------------------------------
  __Template__{}
  function __ctor(self)
    self.OnChildrenSizeChanged = function() self:AdjustHeight(true) end
  end
end)
-------------------------------------------------------------------------------
--                                Styles                                     --
-------------------------------------------------------------------------------
Style.UpdateSkin("Default", {
  [WorldQuestsContentView] = {
    Header = {
      IconBadge = {
        backdropColor = { r = 0, g = 0, b = 0, a = 0},
        Icon = {
          atlas = AtlasType("QuestDaily")
        }
      },

      Label = {
        text = "World Quests"
      }
    },

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

    FlagsStyles = {
      [WorldQuestsContentView.Flags.HAS_WORLD_QUESTS] =  {
        Content = {
          WorldQuests = {
            location = {
              Anchor("TOP"),
              Anchor("LEFT"),
              Anchor("RIGHT")
            }
          }
        }
      }
    }
  }
})
