-- ========================================================================= --
--                              SylingTracker                                --
--           https://www.curseforge.com/wow/addons/sylingtracker             --
--                                                                           --
--                               Repository:                                 --
--                   https://github.com/Skamer/SylingTracker                 --
--                                                                           --
-- ========================================================================= --
Syling                  "SylingTracker.Dungeon.ContentView"                  ""
-- ========================================================================= --
namespace                          "SLT"
-- ========================================================================= --
export {
  ValidateFlags                     = System.Toolset.validateflags,
  ResetStyles                       = Utils.ResetStyles, 
}
-- ========================================================================= --
__Recyclable__ "SylingTracker_DungeonContentView%d"
class "DungeonContentView" (function(_ENV)
  inherit "ContentView"

  __Flags__()
  enum "Flags" {
    NONE            = 0,
    HAS_OBJECTIVES  = 1
  }
  -----------------------------------------------------------------------------
  --                               Methods                                   --
  -----------------------------------------------------------------------------
  function OnViewUpdate(self, data)
    local dungeonData = data.dungeon 
    if not dungeonData then 
      return 
    end

    -- Get the elements
    local contentFrame    = self:GetChild("Content")
    local headerFrame     = self:GetChild("Header")
    local dungeonNameFS   = headerFrame:GetChild("DungeonName")
    local dungeonIcon     = contentFrame:GetChild("DungeonIcon")

    -- Determine the flags
    local flags = Flags.NONE

    local objectivesData = dungeonData.objectives 
    if objectivesData then 
      flags = flags + Flags.HAS_OBJECTIVES 
    end

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

      -- Is there objectives
      if ValidateFlags(Flags.HAS_OBJECTIVES, flags) then 
        self:AcquireObjectives()
      else 
        self:ReleaseObjectives() 
      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


    local name, texture = dungeonData.name, dungeonData.icon
    if objectivesData then 
      local objectivesFrame = self:AcquireObjectives()
      objectivesFrame:UpdateView(objectivesData)
    end

    if texture then
      Style[dungeonIcon].Icon.fileID = texture
    end

    if name then 
      Style[dungeonNameFS].text = name 
    end

    self.Flags = flags
  end

  function AcquireObjectives(self)
    local content = self:GetChild("Content")
    local objectives = content:GetChild("Objectives")

    if not objectives then 
      objectives = self.ObjectivesClass.Acquire()

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

      objectives:SetParent(content)
      objectives:SetName("Objectives")
      objectives:InstantApplyStyle()

      objectives.OnSizeChanged = objectives.OnSizeChanged + self.OnObjectivesSizeChanged

      self:AdjustHeight(true)
    end

    return objectives 
  end

  function ReleaseObjectives(self)
    local content = self:GetChild("Content")
    local objectives = content:GetChild("Objectives")

    if objectives then 
      -- Give its old name (generated by the recycle system)
      objectives:SetName(self.__PreviousObjectivesName)
      self.__PreviousObjectivesName = nil 

      -- Unregister the events
      objectives.OnSizeChanged = objectives.OnSizeChanged - self.OnObjectivesSizeChanged

      -- It's better to release after events have been unregistered for avoiding
      -- useless calls 
      objectives:Release()

      self:AdjustHeight(true)
    end 
  end 

  function OnRelease(self)
    -- First, relase the children 
    self:ReleaseObjectives()

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

    -- Reset the class properties 
    self.Flags = nil
  end
  -----------------------------------------------------------------------------
  --                               Properties                                --
  -----------------------------------------------------------------------------
  property "Flags" {
    type = DungeonContentView.Flags,
    default = DungeonContentView.Flags.NONE
  }

  property "Objectives" {
    type = Table
  }

  property "ObjectivesClass" {
    type = ClassType,
    default = ObjectiveListView
  }

  property "FlagsStyles" {
    type = Table
  }
  -----------------------------------------------------------------------------
  --                            Constructors                                 --
  -----------------------------------------------------------------------------
  __Template__ {
    {
      Header = {
        DungeonName = SLTFontString
      },
      Content = {
        DungeonIcon = IconBadge
      }
    }
  }
  function __ctor(self)
    self.OnObjectivesSizeChanged = function() self:AdjustHeight(true) end
  end
end)
-- ========================================================================= --
--                                Styles                                     --
-- ========================================================================= --
Style.UpdateSkin("Default", {
  [DungeonContentView] = {
    Header = {
      IconBadge = {
        Icon = {
          atlas = AtlasType("Dungeon")
        }
      },

      Label = {
        text            = "Dungeon",
        sharedMediaFont = FontType("PT Sans Narrow Bold", 13),
        justifyV        = "TOP"
      },

      DungeonName = {
        sharedMediaFont = FontType("PT Sans Caption Bold", 13),
        textColor       = Color(1, 233/255, 174/255),
        justifyV        = "BOTTOM",
        textTransform   = "UPPERCASE",
        location        = {
          Anchor("TOP"),
          Anchor("LEFT", 0, 0, "IconBadge", "RIGHT"),
          Anchor("RIGHT"),
          Anchor("BOTTOM", 0, 2)
        }
      }
    },

    Content = {
      backdrop = {
        bgFile = [[Interface\AddOns\SylingTracker\Media\Textures\LinearGradient]],
      },
      backdropColor = { r = 35/255, g = 40/255, b = 46/255, a = 0.73},

      DungeonIcon = {
        width = 64,
        height = 64,
        location = {
          Anchor("TOPLEFT", 5, -5),
        },
      }
    },

    FlagsStyles = {
      [DungeonContentView.Flags.HAS_OBJECTIVES] = {
        Content = {
          Objectives = {
            spacing = 10,
            location = {
              Anchor("TOP", 0, -5),
              Anchor("LEFT", 5, 0, "DungeonIcon", "RIGHT"),
              Anchor("RIGHT")
            }
          }
        }
      }
    }
  }
})