content: sub init()
    m.top.backgroundURI = "pkg:/images/background.png" ' Placeholder background
    m.port = CreateObject("roMessagePort")
    m.top.observeField("visible", m.port) ' Observe a field to ensure port is active for later async events
  
    m.categoryList = m.top.findNode("categoryList")
  
    fetchContent()
  end sub
  
  sub fetchContent()
    print "Fetching content from local feed.json..."
    contentFile = ReadAsciiFile("pkg:/source/feed.json")
    if contentFile = ""
      print "Error: Could not read feed.json or it is empty."
      ' TODO: Show an error message on screen
      return
    end if
  
    parsedContent = ParseJson(contentFile)
    if parsedContent = invalid
      print "Error: Invalid JSON format in feed.json."
      ' TODO: Show an error message on screen
      return
    end if
  
    print "Successfully parsed content."
    m.top.content = parsedContent ' This will trigger onContentChange
  end sub
  
  sub onContentChange()
    content = m.top.content
    if content <> invalid AND content.categories <> invalid
      categoryData = []
      for each category in content.categories
        categoryData.push(category)
      end for
      m.categoryList.content = categoryData
      m.categoryList.setFocus(true) ' Set focus to the list
    else
      print "No categories found in content or content is invalid."
    end if
  end sub
  
  sub playVideo(videoContent as object)
    if videoContent = invalid OR videoContent.stream_url = invalid OR videoContent.stream_url = ""
      print "Error: Cannot play video, stream_url is missing or invalid."
      if videoContent.stream_url = "YOUR_LIVE_STREAM_URL_HERE_1"
        print "Hint: You need to replace the placeholder live stream URL in feed.json"
      end if
      return
    end if
  
    print "Attempting to play video: " + videoContent.title + " URL: " + videoContent.stream_url
  
    video = CreateObject("roSGNode", "VideoScene")
    video.video = videoContent ' Pass the whole video content AA
    video.control = "play"    ' Start playback immediately
  
    ' Observe video playback events
    video.observeField("state", m.port)
    video.observeField("visible", m.port) ' To know when it's closed
  
    m.top.getScene().showDialog(video) ' Show VideoScene as a dialog (common practice)
                                     ' Alternatively, you can manage it as a child of the main scene
                                     ' or use screen.PushScene() / screen.PopScene() if not using SceneGraph dialogs.
  
    m.currentPlayer = video ' Store reference to current player
  end sub
  
  ' Need to handle events from the VideoScene in the main event loop (source/main.brs)
  ' or here if we want to manage the player lifecycle within Scene.brs
  ' For simplicity with dialog, we'll assume main.brs handles screen closed.
  ' If VideoScene was a child, we'd need more complex handling here.
  ' Let's add a handler for when the video player is closed or has an error.
  
  sub handleVideoPlayerEvents(msg as object)
      if m.currentPlayer = invalid then return
  
      if msg.isRemoteClosed() OR (msg.getField() = "visible" AND NOT msg.getData())
          print "Video player closed by user or programmatically."
          if m.currentPlayer <> invalid
              m.currentPlayer.setFocus(false) ' Release focus
              m.currentPlayer.close = true    ' Ensure it's closed
              m.currentPlayer = invalid
          end if
          m.categoryList.setFocus(true) ' Return focus to the list
      else if msg.getField() = "state"
          state = msg.getData()
          print "Video player state: " + state
          if state = "error"
              print "Video player error."
              ' You could get more error info from m.currentPlayer.error / m.currentPlayer.errorCode
              if m.currentPlayer <> invalid
                  m.currentPlayer.setFocus(false)
                  m.currentPlayer.close = true
                  m.currentPlayer = invalid
              end if
              m.categoryList.setFocus(true)
          else if state = "finished"
              print "Video playback finished."
               if m.currentPlayer <> invalid
                  m.currentPlayer.setFocus(false)
                  m.currentPlayer.close = true
                  m.currentPlayer = invalid
              end if
              m.categoryList.setFocus(true)
          end if
      end if
  end sub
  