components_ItemGrid_MusicArtistGridItem.bs

import "pkg:/source/utils/config.bs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.itemPoster = m.top.findNode("itemPoster")
  m.postTextBackground = m.top.findNode("postTextBackground")
  m.posterText = m.top.findNode("posterText")
  m.backdrop = m.top.findNode("backdrop")
  m.backdrop.blendColor = m.global.constants.colorBackgroundSecondary

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

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

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

  m.posterText.visible = false
  m.postTextBackground.visible = false
end sub

sub itemContentChanged()
  m.posterText.visible = false
  m.postTextBackground.visible = false

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

  itemData = m.top.itemContent

  if not isValid(itemData) then return

  ' Check if we need to show a fallback icon (only when no PosterUrl)
  showFallbackIcon = false
  fallbackIconUri = ""

  if not isValid(itemData.PosterUrl) or itemData.PosterUrl = ""
    if LCase(itemData.type) = "musicalbum"
      fallbackIconUri = "pkg:/images/icons/album.png"
      showFallbackIcon = true
    else if LCase(itemData.type) = "musicartist"
      fallbackIconUri = "pkg:/images/icons/missingArtist.png"
      showFallbackIcon = true
    else if LCase(itemData.json.type) = "musicgenre"
      fallbackIconUri = "pkg:/images/icons/musicFolder.png"
      showFallbackIcon = true
    end if
  end if

  m.itemPoster.uri = itemData.PosterUrl
  m.posterText.text = itemData.title

  'If Poster not loaded, show colored backdrop
  if m.itemPoster.loadStatus <> "ready"
    m.backdrop.visible = true
    ' If we need a fallback icon, prepare to show it on top of backdrop
    if showFallbackIcon
      m.itemPoster.uri = fallbackIconUri
      m.itemPoster.blendColor = m.global.constants.colorTextSecondary
      m.itemPoster.loadDisplayMode = "scaleToFit"
    end if
  end if
  if m.top.itemHasFocus then focusChanged()
end sub

'Display or hide title Visibility on focus change
sub focusChanged()
  if m.top.itemHasFocus = true
    m.posterText.repeatCount = -1
  else
    m.posterText.repeatCount = 0
  end if

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

'Hide backdrop when real poster loaded (not fallback icon)
sub onPosterLoadStatusChanged()
  if m.itemPoster.loadStatus = "ready"
    ' Only hide backdrop if we loaded an actual poster image (has PosterUrl)
    itemData = m.top.itemContent
    if isValid(itemData) and isValid(itemData.PosterUrl) and itemData.PosterUrl <> ""
      m.backdrop.visible = false
      m.itemPoster.blendColor = "0xFFFFFFFF" ' Reset to white
      ' Restore original display mode if parent specifies one
      if isValid(m.topParent.imageDisplayMode)
        m.itemPoster.loadDisplayMode = m.topParent.imageDisplayMode
      end if
    end if
  end if
end sub