components_JRScene.bs

import "pkg:/source/roku_modules/log/LogMixin.brs"
import "pkg:/source/utils/backdrop.bs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.log = log.Logger("JRScene")
  m.top.backgroundColor = m.global.constants.colorBackgroundPrimary
  m.top.backgroundURI = ""
  m.loadingText = m.top.findNode("loadingText")
  m.spinner = m.top.findNode("spinner")
  m.imageFader = m.top.findNode("imageFader")
  localDevice = m.global.device
  m.imageFader.width = localDevice.uiResolution[0]
  m.imageFader.height = localDevice.uiResolution[1]

  ' Hide backdrop until setting is resolved on first backdrop request (lazy initialization)
  m.imageFader.visible = false

  ' set text manually to AVOID translation
  defaultFont = m.top.findNode("defaultFont")
  fallbackFont = m.top.findNode("fallbackFont")
  defaultFont.text = "Ag"
  fallbackFont.text = "Ag"
end sub

sub loadingTextChanged()
  m.loadingText.text = m.top.loadingText
end sub

sub backgroundImageUriChanged()
  if not isValid(m.imageFader) then return

  ' Resolve backdrop setting on first use (lazy initialization)
  if m.top.showBackdrop = invalid
    localUser = m.global.user
    if isValid(localUser)
      m.top.showBackdrop = resolveShowBackdrop(localUser.settings, localUser.config)
    else
      m.top.showBackdrop = false ' Don't show backdrops if called before login
    end if
    m.imageFader.visible = m.top.showBackdrop
  end if

  ' Only update URI if backdrops are enabled (performance optimization)
  if m.top.showBackdrop
    m.imageFader.isAnimated = true
    m.imageFader.uri = m.top.backgroundImageUri
  end if
end sub

' Set the background image with animation control
' @param {string} uri - The image URI to display
' @param {boolean} isAnimated - Whether to animate the transition
' @param {boolean} forceBackdrop - Force show backdrop regardless of user setting (used for login splashscreen)
sub setBackgroundImage(uri as string, isAnimated = true as boolean, forceBackdrop = false as boolean)
  if not isValid(m.imageFader) then return

  ' Force backdrop mode bypasses user settings (used for login splashscreen)
  if forceBackdrop
    ' For empty URI in force mode, clear URI and reset state for next screen
    if uri = invalid or uri = ""
      m.imageFader.uri = ""
      ' Reset showBackdrop to invalid so next screen can properly initialize
      m.top.showBackdrop = invalid
      ' Explicitly hide imageFader to ensure a clean state if next screen does not reinitialize backdrop visibility
      m.imageFader.visible = false
    else
      m.imageFader.isAnimated = isAnimated
      m.imageFader.uri = uri
      m.imageFader.visible = true
    end if
    return
  end if

  ' Resolve backdrop setting on first use (lazy initialization)
  if m.top.showBackdrop = invalid
    localUser = m.global.user
    if isValid(localUser)
      m.top.showBackdrop = resolveShowBackdrop(localUser.settings, localUser.config)
    else
      m.top.showBackdrop = false ' Don't show backdrops if called before login
    end if
    m.imageFader.visible = m.top.showBackdrop
  end if

  ' Only update URI if backdrops are enabled (performance optimization)
  if m.top.showBackdrop
    m.imageFader.isAnimated = isAnimated
    m.imageFader.uri = uri
  end if
end sub

' Re-evaluate and apply the backdrop visibility setting
' Useful when settings change at runtime (e.g., from Settings screen)
sub refreshBackdropSetting()
  if not isValid(m.imageFader) then return

  localUser = m.global.user
  if isValid(localUser)
    m.top.showBackdrop = resolveShowBackdrop(localUser.settings, localUser.config)
    m.imageFader.visible = m.top.showBackdrop

    ' Clear backdrop URI if hiding to free memory
    if not m.top.showBackdrop
      m.top.backgroundImageUri = ""
    end if
  end if
end sub

' Triggered when the isLoading boolean component field is changed
sub isLoadingChanged()
  ' toggle visibility of active view/group
  group = m.global.sceneManager.callFunc("getActiveScene")
  if isValid(group)
    group.visible = not m.top.disableRemote
  end if

  ' toggle visibility of loading spinner
  m.spinner.visible = m.top.isLoading
  ' toggle visibility of loading text
  m.loadingText.visible = m.top.isLoading
end sub

function onKeyEvent(key as string, press as boolean) as boolean
  if not press then return false
  if m.top.disableRemote then return true

  if key = "back"
    m.global.sceneManager.callFunc("popScene")
    return true
  else if key = "options"
    group = m.global.sceneManager.callFunc("getActiveScene")
    if isValid(group) and isValid(group.optionsAvailable) and group.optionsAvailable
      group.lastFocus = group.focusedChild
      panel = group.findNode("options")
      panel.visible = true
      panel.findNode("panelList").setFocus(true)
    end if
    return true
  end if

  return false
end function