components_ItemGrid_GridItemSmall.bs

import "pkg:/source/constants/imageSize.bs"
import "pkg:/source/utils/itemImageUrl.bs"
import "pkg:/source/utils/misc.bs"
import "pkg:/source/utils/placeholderImage.bs"

sub init()
  m.itemPoster = m.top.findNode("itemPoster")
  m.placeholder = m.top.findNode("placeholder")
  m.genreBackdrop = m.top.findNode("genreBackdrop")
  m.genreLabel = m.top.findNode("genreLabel")
  initTitle()

  m.itemPoster.observeField("loadStatus", "onPosterLoadStatusChanged")

  'Parent is MarkupGrid and it's parent is the ItemGrid
  m.topParent = m.top.GetParent().GetParent()

  m.title.visible = false

  'Get the imageDisplayMode for these grid items
  if isValid(m.topParent.imageDisplayMode)
    m.itemPoster.loadDisplayMode = m.topParent.imageDisplayMode
  end if
end sub

sub initTitle()
  m.title = m.top.findNode("title")
end sub

sub onItemContentChanged()
  m.title.visible = false

  if isValid(m.topParent.showItemTitles)
    if LCase(m.topParent.showItemTitles) = "showalways"
      m.title.visible = true
    end if
  end if

  itemData = m.top.itemContent

  if not isValid(itemData) then return
  if not isValidAndNotEmpty(itemData.id) then return

  userSettings = m.global.user.settings

  if not userSettings.uiTvShowsDisableUnwatchedCount
    if itemData.isWatched
      m.itemPoster.isWatched = true
    else if itemData.unplayedItemCount > 0
      m.itemPoster.unplayedCount = itemData.unplayedItemCount
    end if
  end if

  if LCase(itemData.type) = "genre" or LCase(itemData.folderType) = "genre"
    ' "View All [Genre]" items are action tiles, not media — they get a dedicated themed
    ' backdrop + in-cell label rather than going through JRPlaceholder, so they don't
    ' read as failed-to-load placeholders. Check both type and folderType: if Jellyfin
    ' returns IsFolder=false/absent the transformer leaves type="Genre" and folderType="" so
    ' we must match on type directly.
    m.itemPoster.uri = ""
    m.placeholder.visible = false
    m.genreBackdrop.visible = true
    m.genreLabel.text = itemData.title
    m.genreLabel.visible = true
  else
    m.genreBackdrop.visible = false
    m.genreLabel.visible = false

    posterUrl = getItemPosterUrl(itemData)
    if posterUrl = ""
      ' Known-missing image — eagerly surface the typed glyph. Load-status never fires
      ' "failed" for an empty URI, so we must set the placeholder state here rather
      ' than waiting for onPosterLoadStatusChanged.
      m.itemPoster.uri = ""
      showPlaceholder(resolveItemPlaceholderType(itemData))
    else
      ' Image expected — start in loading state. The load-status observer flips to
      ' the typed-glyph failure state on "failed", or hides on "ready".
      m.placeholder.itemType = ""
      m.placeholder.visible = true
      m.itemPoster.uri = posterUrl
    end if
  end if

  m.title.text = itemData.title
end sub

sub onFocusChanged()
  if not isValid(m.title) then initTitle()

  if m.top.itemHasFocus = true
    m.title.repeatCount = -1
  else
    m.title.repeatCount = 0
  end if

  if isValid(m.topParent.showItemTitles)
    if LCase(m.topParent.showItemTitles) = "showonhover"
      m.title.visible = m.top.itemHasFocus
    end if
  end if
end sub

' Toggle the JRPlaceholder fallback based on the real poster's load state.
' Mirrors the canonical state machine from JRRowItem.bs onPosterLoadStatusChanged.
sub onPosterLoadStatusChanged()
  if m.itemPoster.loadStatus = "ready" and m.itemPoster.uri <> ""
    m.placeholder.visible = false
  else if m.itemPoster.loadStatus = "failed" and m.itemPoster.uri <> ""
    showPlaceholder(resolveItemPlaceholderType(m.top.itemContent))
  else
    m.placeholder.visible = true
  end if
end sub

' Flip the placeholder into failure state with a type-appropriate glyph, then
' clear the poster URI so the glyph isn't covered by a stale broken image.
' itemType="" puts JRPlaceholder in loading state (backdrop only, no glyph);
' resolveItemPlaceholderType returns "" only for invalid/typeless items, so
' valid items always end up here with a typed glyph or the Folder fallback.
sub showPlaceholder(itemType as string)
  m.placeholder.itemType = itemType
  m.placeholder.visible = true
  m.itemPoster.uri = ""
end sub