content: sub init()
      m.headerTitle = m.top.findNode("headerTitle")
      m.userInfo = m.top.findNode("userInfo")
      m.filesButton = m.top.findNode("filesButton")
      m.transfersButton = m.top.findNode("transfersButton")
      m.logoutButton = m.top.findNode("logoutButton")
      m.fileGrid = m.top.findNode("fileGrid")
      m.loadingSpinner = m.top.findNode("loadingSpinner")
      
      ' Set up observers
      m.filesButton.observeField("buttonSelected", "onFilesPressed")
      m.transfersButton.observeField("buttonSelected", "onTransfersPressed")
      m.logoutButton.observeField("buttonSelected", "onLogoutPressed")
      m.fileGrid.observeField("itemSelected", "onFileSelected")
      
      ' Observe access token changes
      m.top.observeField("accessToken", "onAccessTokenChanged")
      m.top.observeField("playContentId", "onPlayContentId")
      
      ' Current folder ID
      m.currentFolderId = 0 ' Root folder
  end sub
  
  sub onAccessTokenChanged(event as Object)
      accessToken = event.getData()
      if accessToken <> invalid and accessToken <> ""
          m.fileGrid.accessToken = accessToken
          loadFiles()
      end if
  end sub
  
  sub onPlayContentId(event as Object)
      contentId = event.getData()
      if contentId <> invalid and contentId <> ""
          ' Find and play the specific content
          playFileById(contentId)
      end if
  end sub
  
  sub loadFiles()
      m.loadingSpinner.visible = true
      m.fileGrid.loadFolder(m.currentFolderId)
  end sub
  
  sub onFilesPressed(event as Object)
      m.currentFolderId = 0
      loadFiles()
  end sub
  
  sub onTransfersPressed(event as Object)
      ' Load transfers list
      ' Implementation would show active transfers
      print "Transfers not implemented yet"
  end sub
  
  sub onLogoutPressed(event as Object)
      m.top.logout = true
  end sub
  
  sub onFileSelected(event as Object)
      fileData = event.getData()
      if fileData <> invalid
          if fileData.content_type <> invalid and left(fileData.content_type, 5) = "video"
              ' It's a video file, play it
              playVideo(fileData)
          else if fileData.file_type = "FOLDER"
              ' It's a folder, navigate into it
              m.currentFolderId = fileData.id
              loadFiles()
          else
              ' Other file types - could download or show info
              print "File type not supported for playback: "; fileData.content_type
          end if
      end if
  end sub
  
  sub playVideo(fileData as Object)
      ' Create video content object
      videoContent = CreateObject("roSGNode", "ContentNode")
      videoContent.url = "https://api.put.io/v2/files/" + fileData.id.toStr() + "/download?oauth_token=" + m.top.accessToken
      videoContent.title = fileData.name
      videoContent.description = fileData.name
      
      ' Check for subtitles
      if fileData.subtitle <> invalid and fileData.subtitle.count() > 0
          subtitleTracks = []
          for each subtitle in fileData.subtitle
              track = {
                  url: subtitle.url + "?oauth_token=" + m.top.accessToken,
                  language: subtitle.language,
                  description: subtitle.name
              }
              subtitleTracks.push(track)
          end for
          videoContent.subtitleTracks = subtitleTracks
      end if
      
      ' Emit play video event
      m.top.playVideo = {
          content: videoContent,
          fileData: fileData
      }
  end sub
  
  sub playFileById(fileId as String)
      ' Make API call to get file info and play it
      request = CreateObject("roUrlTransfer")
      request.setUrl("https://api.put.io/v2/files/" + fileId)
      request.addHeader("Authorization", "Bearer " + m.top.accessToken)
      
      port = CreateObject("roMessagePort")
      request.setMessagePort(port)
      
      if request.asyncGetToString()
          msg = wait(5000, port)
          if msg <> invalid and type(msg) = "roUrlEvent"
              responseCode = msg.getResponseCode()
              responseString = msg.getString()
              
              if responseCode = 200
                  response = ParseJson(responseString)
                  if response <> invalid and response.file <> invalid
                      playVideo(response.file)
                  end if
              end if
          end if
      end if
  end sub
  
  function onKeyEvent(key as String, press as Boolean) as Boolean
      handled = false
      
      if press
          if key = "back"
              if m.currentFolderId <> 0
                  ' Go back to parent folder
                  ' Would need to implement parent folder tracking
                  m.currentFolderId = 0
                  loadFiles()
                  handled = true
              end if
          end if
      end if
      
      return handled
  end function
  