'*************************************************************
'** Roku Xtream-ALL for XoceUnder                            *
'** Copyright (c)2024 XoceUnder.  All rights reserved.       *
'*************************************************************
sub Init()
    ' set the name of the function in the Task node component to be executed when the state field changes to RUN
    ' in our case this method executed after the following cmd: m.contentTask.control = "run"(see Init method in MainScene)
    m.top.functionName = "GetContent"
end sub

sub GetContent()
    ' request the content feed from the API
    http = NewHttp( m.global.config.serverURL + "/player_api.php?username=" + m.global.user +"&password=" + m.global.pass + "&action=" + m.global.items +"&category_id=" + m.top.category_id )
    rsp = http.GetToStringWithRetry()
	rootChildren = []
	
    json = ParseJson(rsp)
	if json <> invalid and json.Count() > 0 then
        ' Aplicar ordenamiento según configuración
        if m.global.sort <> invalid and m.global.sort <> "auto" then
            if json[0].stream_id <> invalid then
                if m.global.sort = "id" then
                    json.SortBy("stream_id", "r")
                else
                    json.SortBy("name")
                end if
            else
                if m.global.sort = "id" then
                    json.SortBy("series_id", "r")
                else
                    json.SortBy("name")
                end if
            end if
        else
            ' Ordenamiento por defecto (más recientes primero)
            if json[0].stream_id <> invalid then
                json.SortBy("stream_id", "r")
            else
                json.SortBy("series_id", "r")
            end if
        end if
        
        row = {}
        row.title = m.top.category_title
        row.children = []
        for each item in json ' parse items and push them to row
            itemData = GetItemData(item)
            rootChildren.Push(itemData)
        end for
	end if  
    ' set up a root ContentNode to represent rowList on the GridScreen
    contentNode = CreateObject("roSGNode", "ContentNode")
    contentNode.Update({children: rootChildren}, true)
	m.top.content = contentNode
end sub

function GetItemData(video as Object) as Object
    item = {}
	item.title = video.name
	item.contentType = m.global.contentType
	item.mediaType = m.global.contentType
    item.rating = AnyToString(video.rating)
    item.description = ""
	item.categories = ""
	item.actors = ""
	item.cast = ""
    item.releasedate = ""
	item.directors = ""
    item.length = ""
    item.duration = ""
	
	if m.global.contentType = "movie" then
		item.id = video.stream_id
		item.hdPosterURL = video.stream_icon
        ' Verificar si container_extension existe antes de usarlo
        if video.container_extension <> invalid then
            item.url = m.global.config.serverURL + m.global.play + tostr(video.stream_id) + "." + video.container_extension
        else
            ' Usar formato por defecto basado en streamFormat
            extension = ".m3u8"
            if m.global.streamFormat <> invalid and m.global.streamFormat <> "hls" then extension = ".ts"
            item.url = m.global.config.serverURL + m.global.play + tostr(video.stream_id) + extension
        end if
        'item.streamFormat = video.container_extension
		item.fhdposterurl = ""
	else
		item.id = video.series_id
		item.hdPosterURL = video.cover
		item.fhdposterurl = backdrop_path(video.backdrop_path)
		item.children = []
	end if
	
    return item
end function