content: sub init()
      m.stackNavigator = CreateObject("roSGNode", "StackNavigator")
  
      m.video = m.top.findNode("video")
  
      m.top.ObserveField("focusedChild", "setFocusedChild")
      m.video.ObserveField("state", "setState")
  end sub
  
  sub setFocusedChild(event as object)
      overhang = m.global.scene.findNode("overhang")
      if m.top.hasFocus() and overhang.visible
          overhang.visible = false
      else if not(m.top.isInFocusChain()) and not(overhang.visible)
          overhang.visible = true
      end if
  end sub
  
  sub setState(event as Object)
      state = event.getData()
      if state = "finished"
          m.stackNavigator.callFunc("CloseScreen")
      end if
  end sub
  
  sub setVideoID(event = invalid as Object)
      if event<>invalid
          video_id = event.getData()
      else
          video_id = m.top.video_id
      end if
      if video_id<>""
          print("video_id")
          print(video_id)
          m.global.load = true
          streamTask = CreateObject("roSGNode", "FetchStream")
          streamTask.ObserveField("response", "setResponse")
          streamTask.setField("video_id", video_id)
          if m.video.control="play"
              streamTask.setField("getVideo", true)
          end if    
          streamTask.control = "run"
      end if
  end sub
  
  function onKeyEvent(key as String, press as Boolean)  as Boolean
      if press
          if key = "ok"
              if m.video.control = "pause"
                  m.video.control = "resume"
              else 
                  m.video.control = "pause"
              end if 
              return true
          end if 
      end if
      return false
  end function
  
  sub setResponse(event as Object)
      response = event.getData()
  
      headers = response["headers"]
      body = response["body"]
      
      error = response["error"]
      try
          if headers<>invalid and headers.DoesExist("logout")
              m.global.credentials = invalid
          end if
          
          if(body["videoDetails"]<>invalid)
              videoInfo = {
                  "type": "video",
                  "title": body["videoDetails"]["title"],
                  "videoId": body["videoDetails"]["videoId"],
                  "author": body["videoDetails"]["author"],
                  "authorId": body["videoDetails"]["channelId"],
                  "liveNow": body["videoDetails"]["isLive"],
                  "url": body["streamingData"]["hlsManifestUrl"],
                  "streamformat": "auto",
              }
  
              videocontent = createObject("RoSGNode", "ContentNode")
              videocontent.Update(videoInfo, true)
  
              m.video.content = videocontent
              m.video.setFocus(true)
              m.video.control = "play"
              setVideoID()
          end if
      catch err
          if error = invalid
              error = err
          end if
      end try
      if error<>invalid
          print("Response stream error")
          print(error)
          m.stackNavigator.callFunc("CloseScreen")
      end if
      m.global.load = false
  end sub