'*************************************************************
'** Roku Xtream-ALL for XoceUnder
'*************************************************************

sub Init()

    mBind(["homeGrid",
           "itemLabelMain1",
           "itemLabelMain2",
           "itemHeader",
           "welcomeLabel",
           "welcomeContainer"])

    m.top.observeField("focusedChild","onFocusedChildChange")

    ' Mensagem de boas-vindas dinâmica
    SetWelcomeMessage()
    
    ' Criar logo
    CreateDynamicLogo()

    ' =========================
    ' BOTÕES DO MENU
    ' =========================
    buttons = []

    buttons.Push({id:"1", title:tr("Search"), shortdescriptionline1:tr("PESQUISAR"), HDLISTITEMICONURL:"pkg:/images/menu/search.png", shortdescriptionline2:tr("Buscar conteúdo")})
    buttons.Push({id:"2", title:tr("Canais"), shortdescriptionline1:tr("CANAIS"), HDLISTITEMICONURL:"pkg:/images/menu/tv.png", shortdescriptionline2:tr("Canais Ao vivo")})
    buttons.Push({id:"3", title:tr("Filmes"), shortdescriptionline1:tr("FILMES"), HDLISTITEMICONURL:"pkg:/images/menu/movie.png", shortdescriptionline2:tr("Todos os Filmes")})
    buttons.Push({id:"4", title:tr("Séries"), shortdescriptionline1:tr("SÉRIES"), HDLISTITEMICONURL:"pkg:/images/menu/series.png", shortdescriptionline2:tr("Todas As Séries")})
    buttons.Push({id:"5", title:tr("Usuário"), shortdescriptionline1:tr("USUÁRIO"), HDLISTITEMICONURL:"pkg:/images/menu/account.png", shortdescriptionline2:tr("Informações do Usuário")})
    buttons.Push({id:"6", title:tr("Favoritos"), shortdescriptionline1:tr("FAVORITOS"), HDLISTITEMICONURL:"pkg:/images/menu/plus.png", shortdescriptionline2:tr("Meus Favoritos")})
    buttons.Push({id:"7", title:tr("Configurações"), shortdescriptionline1:tr("CONFIGURAÇÕES"), HDLISTITEMICONURL:"pkg:/images/menu/settings.png", shortdescriptionline2:tr("Menu de Configurações")})

    m.homeGrid.content = List2ContentNode(buttons)

    ' Centraliza grid horizontalmente
    markupRect = m.homeGrid.boundingRect()
    screenWidth = getScreenSize().width
    centerx = (screenWidth - markupRect.width) / 2

    m.homeGrid.translation = [centerx + 15, 650]

    ' Item padrão
    m.homeGrid.jumpToItem = 2
    m.homeGrid.focusColumn = 2

end sub


'==================================================
' BEM VINDO DINÂMICO (SEM FUNDO)
'==================================================
sub SetWelcomeMessage()

    username = ""
    if m.global <> invalid and m.global.user <> invalid
        username = m.global.user
    end if

    welcomeText = "Usuário: " + username
    m.welcomeLabel.text = welcomeText

    textRect = m.welcomeLabel.boundingRect()

    screenWidth = getScreenSize().width
    textWidth = textRect.width

    containerX = (screenWidth - textWidth) / 2

    m.welcomeContainer.translation = [containerX, 40]

end sub


'==================================================
' CRIAR LOGO DINÂMICA
'==================================================
sub CreateDynamicLogo()

    m.logoMenu = createObject("roSGNode", "Poster")
    m.logoMenu.id = "logoMenu"
    m.logoMenu.uri = m.global.logomenu

    m.logoMenu.observeField("loadStatus", "onLogoLoaded")

    m.top.appendChild(m.logoMenu)

end sub


'==================================================
' POSICIONA LOGO 20% ABAIXO DO TEXTO
'==================================================
sub onLogoLoaded()

    if m.logoMenu.loadStatus = "ready"

        screenWidth = getScreenSize().width

        logoRect = m.logoMenu.boundingRect()
        logoWidth = logoRect.width
        logoHeight = logoRect.height

        ' Centraliza horizontalmente
        centerX = (screenWidth - logoWidth) / 2

        ' Posição do texto
        welcomeY = m.welcomeContainer.translation[1]
        welcomeHeight = m.welcomeLabel.boundingRect().height

        ' 20% abaixo do texto
        spaceBelow = welcomeHeight * 0.2
        newY = welcomeY + welcomeHeight + spaceBelow

        m.logoMenu.translation = [centerX, newY]

    end if

end sub


'==================================================
' CONTROLE DE FOCO
'==================================================
sub onFocusedChildChange()

    if m.top.hasFocus()
        m.top.visible = true
        m.homeGrid.setFocus(true)
    end if

end sub