'*************************************************************
'** Roku Xtream-ALL for XoceUnder
'** Copyright (c)2024 XoceUnder.  All rights reserved.
sub Init()
   'Dbg("MenuScreen Init")
    mBind(["homeGrid", "itemLabelMain1", "itemLabelMain2","itemHeader", "welcomeLabel"])
    m.top.observeField("focusedChild","onFocusedChildChange")
  
    ' Configurar mensaje de bienvenida
    SetWelcomeMessage()
    
    ' Crear logo dinámicamente
    CreateDynamicLogo()
  
    'Populate grid content
    buttons = []
	buttons.Push({id: "1", x: "300", y: "300", title: tr("Search"), shortdescriptionline1: tr("Search"), HDLISTITEMICONURL: "pkg:/images/menu/search.png", shortdescriptionline2: tr("Search content by title")})
    buttons.Push({id: "2", x: "300", y: "300", title: tr("Live"), shortdescriptionline1: tr("Live"), HDLISTITEMICONURL: "pkg:/images/menu/tv.png", shortdescriptionline2: tr("Live channels to watch")})
    buttons.Push({id: "3", x: "300", y: "300", title: tr("Movies"), shortdescriptionline1: tr("Movies"), HDLISTITEMICONURL: "pkg:/images/menu/movie.png", shortdescriptionline2: tr("Movies to watch by category")})
    buttons.Push({id: "4", x: "300", y: "300", title: tr("TV Shows"), shortdescriptionline1: tr("TV Shows"), HDLISTITEMICONURL: "pkg:/images/menu/series.png", shortdescriptionline2: tr("TV Shows to see by category")})
    buttons.Push({id: "5", x: "300", y: "300", title: tr("Account"), shortdescriptionline1: tr("Account"), HDLISTITEMICONURL: "pkg:/images/menu/account.png", shortdescriptionline2: tr("Account Information")})
	buttons.Push({id: "6", x: "300", y: "300", title: tr("My Favorites"), shortdescriptionline1: tr("My Favorites"), HDLISTITEMICONURL: "pkg:/images/menu/plus.png", shortdescriptionline2: tr("My selected list")})
    buttons.Push({id: "7", x: "300", y: "300", title: tr("Settings"), shortdescriptionline1: tr("Settings"), HDLISTITEMICONURL: "pkg:/images/menu/settings.png", shortdescriptionline2: tr("System Settings")})
	
    'Populate grid content
    m.homeGrid.content = List2ContentNode(buttons)

    'Center the MarkUp Box - Posición original del menú
    markupRect = m.homeGrid.boundingRect()
    centerx = (getScreenSize().width - markupRect.width ) / 2
    m.homeGrid.translation = [ centerx + 15, 650 ]
    
    'Posicionar logo alineado con el primer ícono del menú
    PositionDynamicLogo(centerx + 600, 150)
    
    'Select default item
    m.homeGrid.jumpToItem = 2
    m.homeGrid.focusColumn = 2
	
end sub

sub CreateDynamicLogo()
    ' Crear el logo dinámicamente como un Poster
    m.logoMenu = createObject("roSGNode", "Poster")
    m.logoMenu.id = "logoMenu"
    m.logoMenu.uri = m.global.logomenu
    
    ' Agregar el logo al componente
    m.top.appendChild(m.logoMenu)
end sub

sub PositionDynamicLogo(x as Float, y as Float)
    ' Posicionar el logo dinámico
    if m.logoMenu <> invalid then
        m.logoMenu.translation = [x, y]
    end if
end sub

sub SetWelcomeMessage()
    ' Obtener el nombre de usuario desde m.global.user
    username = ""
    if m.global <> invalid and m.global.user <> invalid then
        username = m.global.user
    end if
    
    ' Configurar el mensaje de bienvenida
    welcomeText = "BIENVENIDO: " + username
    m.welcomeLabel.text = welcomeText
    
    ' Centrar el label horizontalmente
    screenWidth = getScreenSize().width
    m.welcomeLabel.translation = [screenWidth / 2 - m.welcomeLabel.boundingRect().width / 2, 50]
end sub

sub onFocusedChildChange()
    'print "top parent: " m.top.GetParent()
    'print "top scene: " m.top.GetScene()
  if m.top.hasFocus()
     m.top.visible = true
	 m.homeGrid.setFocus(true)
  end if
end sub