'*************************************************************
'** Roku Xtream-ALL for XoceUnder
'** Copyright (c)2024 XoceUnder.  All rights reserved.
sub Init()
    m.top.functionName = "GetContent"
end sub

sub GetContent()
    contentNode = CreateObject("roSGNode", "ContentNode")
    rootChildren = []

    if true
        if not isArray(m.global.cachesearch)
            tipoid = 0
            if m.top.action = "get_live_streams"
                tipoid = 1
            else if m.top.action = "get_vod_streams"
                tipoid = 2
            else if m.top.action = "get_series"
                tipoid = 3
            end if

            query = m.top.searchTerm
            http = NewHttp("http://rokupro.contentmastersrebrand.com/buscador.php?username=" + m.global.user + "&password=" + m.global.pass + "&search=" + query + "&tipoid=" + tipoid.toStr() + "&baseurl=" + m.global.config.serverURL)
            rsp = http.GetToStringWithRetry()

            ' Validar JSON
            parsed = ParseJson(rsp)
            if parsed = invalid or type(parsed) <> "roArray"
                print "Error en respuesta JSON o no es un array"
                m.top.content = contentNode
                return
            end if

            m.global.cachesearch = parsed
            json = parsed
        else
            json = m.global.cachesearch
        end if

        if json <> invalid and json.Count() > 0
            if m.global.sort <> "auto"
                if type(json[0]) = "roAssociativeArray" and json[0].lookup("stream_id") <> invalid
                    if m.global.sort = "id"
                        json.SortBy("stream_id", "r")
                    else
                        json.SortBy("name")
                    end if
                else
                    if m.global.sort = "id"
                        json.SortBy("series_id", "r")
                    else
                        json.SortBy("name")
                    end if
                end if
            end if

            row = {title: tr("Result:"), children: []}
            for each item in json
                itemData = GetItemData(item)
                if LCase(itemData.title).Instr(LCase(m.top.searchTerm)) > -1
                    row.children.Push(itemData)
                end if
            end for
            rootChildren.Push(row)
        end if
    else
        ?"Error al buscar contenido"
    end if

    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 = "live"
		item.id = video.stream_id
		item.hdPosterURL = video.stream_icon
		m3u8 = ".m3u8"
		if m.global.streamFormat <> "hls" m3u8 = ".ts"
        item.url = m.global.config.serverURL + m.global.play + tostr(video.stream_id) + m3u8
		item.live = true
		item.fhdposterurl = ""
	else if m.global.contentType = "movie"
		item.id = video.stream_id
		item.hdPosterURL = video.stream_icon
		ext = ""
		if video.container_extension <> invalid and video.container_extension <> ""
			ext = "." + video.container_extension
		end if
		item.url = m.global.config.serverURL + m.global.play + tostr(video.stream_id) + ext
		item.fhdposterurl = ""
	else if m.global.contentType = "series"
		item.id = video.series_id
		item.hdPosterURL = video.cover
		item.fhdposterurl = backdrop_path(video.backdrop_path)
		item.children = []
	end if

    return item
end function