components_search_SearchResults.bs

import "pkg:/source/api/baserequest.bs"
import "pkg:/source/api/Image.bs"
import "pkg:/source/api/Items.bs"
import "pkg:/source/utils/config.bs"
import "pkg:/source/utils/deviceCapabilities.bs"
import "pkg:/source/utils/misc.bs"

sub init()
  ' Clear backdrop immediately when search screen opens
  m.global.sceneManager.callFunc("setBackgroundImage", "")

  m.top.optionsAvailable = false
  m.searchSelect = m.top.findnode("searchSelect")
  m.searchTask = CreateObject("roSGNode", "SearchTask")

  m.searchHelpText = m.top.findNode("SearchHelpText")

  ' Cache search keyboard reference and observe focus changes
  ' to adjust alphabet layout when voice button popup appears
  searchBox = m.top.findNode("SearchBox")
  m.searchAlphabox = searchBox.findNode("search_Key")
  m.searchAlphabox.observeField("focusedChild", "onKeyboardFocusChange")

  ' Observe row item focus for backdrop updates
  m.searchSelect.observeField("rowItemFocused", "onSearchItemFocused")

  ' Set initial focus for scene navigation
  m.top.lastFocus = searchBox
end sub

sub OnScreenShown()
  ' Start with transparent backdrop - will update on item focus
  m.global.sceneManager.callFunc("setBackgroundImage", "")

  ' Restore focus for scene navigation
  if isValid(m.top.lastFocus)
    m.top.lastFocus.setFocus(true)
  else
    m.top.setFocus(true)
  end if
end sub

' onKeyboardFocusChange: Adjust alphabet keyboard position when textbox gains/loses focus
' The voice button popup covers alphabet rows when textbox is focused, so we move the textbox up
sub onKeyboardFocusChange()
  if not isValid(m.searchAlphabox) then return

  ' Check if the textEditBox has focus (voice button popup is visible)
  if m.searchAlphabox.textEditBox.hasFocus()
    ' Move textbox up so voice button popup doesn't cover alphabet rows below
    m.searchAlphabox.textEditBox.translation = "[0, -150]"
  else
    ' Reset textbox to original position
    m.searchAlphabox.textEditBox.translation = "[0, 0]"
  end if
end sub

' onSearchItemFocused: Update backdrop when search result is focused
sub onSearchItemFocused()
  if not isValid(m.searchSelect.rowItemFocused) or m.searchSelect.rowItemFocused[0] = -1 or m.searchSelect.rowItemFocused[1] = -1
    return
  end if

  ' Get focused item from search results
  rowContent = m.searchSelect.content.getChild(m.searchSelect.rowItemFocused[0])
  if isValid(rowContent)
    focusedItem = rowContent.getChild(m.searchSelect.rowItemFocused[1])

    if isValid(focusedItem) and isValid(focusedItem.backdropUrl) and focusedItem.backdropUrl <> ""
      m.global.sceneManager.callFunc("setBackgroundImage", focusedItem.backdropUrl)
    else
      ' Item has no backdrop - set transparent
      m.global.sceneManager.callFunc("setBackgroundImage", "")
    end if
  end if
end sub

sub searchMedias()
  query = m.top.searchAlpha
  'if user deletes the search string hide the spinner
  if query.len() = 0
    stopLoadingSpinner()
  end if
  'if search task is running and user selectes another letter stop the search and load the next letter
  m.searchTask.control = "stop"
  if isValid(query) and query <> ""
    m.searchHelpText.visible = false
    startLoadingSpinner(false)
  end if
  m.searchTask.observeField("results", "loadResults")
  m.searchTask.query = query
  m.top.overhangTitle = tr("Search") + ": " + query
  m.searchTask.control = "RUN"

end sub

sub loadResults()
  m.searchTask.unobserveField("results")

  stopLoadingSpinner()
  m.searchSelect.itemdata = m.searchTask.results
  m.searchSelect.query = m.top.SearchAlpha

  if m.searchTask.results.TotalRecordCount = 0
    ' make sure focus is on the keyboard
    if m.searchSelect.isinFocusChain()
      m.searchAlphabox.setFocus(true)
    end if
    return
  end if
end sub

function onKeyEvent(key as string, press as boolean) as boolean
  if not press then return false

  if key = "left" and m.searchSelect.isinFocusChain()
    m.searchAlphabox.setFocus(true)
    return true
  else if key = "right" and isValid(m.searchSelect.content) and m.searchSelect.content.getChildCount() > 0
    m.searchSelect.setFocus(true)
    return true
  else if key = "play" and m.searchSelect.isinFocusChain() and m.searchSelect.rowItemFocused.count() > 0
    print "play was pressed from search results"
    if isValid(m.searchSelect.rowItemFocused)
      selectedContent = m.searchSelect.content.getChild(m.searchSelect.rowItemFocused[0])
      if isValid(selectedContent)
        selectedItem = selectedContent.getChild(m.searchSelect.rowItemFocused[1])
        if isValid(selectedItem)
          m.top.quickPlayNode = selectedItem
          return true
        end if
      end if
    end if
  end if

  return false
end function