components_extras_ExtrasRowList.bs

import "pkg:/source/utils/misc.bs"

sub init()
    m.top.visible = true
    updateSize()
    m.top.rowFocusAnimationStyle = "fixedFocus"
    m.top.observeField("rowItemSelected", "onRowItemSelected")
    m.top.observeField("rowItemFocused", "onRowItemFocused")

    ' Set up all Tasks
    m.LoadPeopleTask = CreateObject("roSGNode", "LoadItemsTask")
    m.LoadPeopleTask.itemsToLoad = "people"
    m.LoadPeopleTask.observeField("content", "onPeopleLoaded")
    m.LikeThisTask = CreateObject("roSGNode", "LoadItemsTask")
    m.LikeThisTask.itemsToLoad = "likethis"
    m.LikeThisTask.observeField("content", "onLikeThisLoaded")
    m.SpecialFeaturesTask = CreateObject("roSGNode", "LoadItemsTask")
    m.SpecialFeaturesTask.itemsToLoad = "specialfeatures"
    m.SpecialFeaturesTask.observeField("content", "onSpecialFeaturesLoaded")
    m.LoadAdditionalPartsTask = CreateObject("roSGNode", "LoadItemsTask")
    m.LoadAdditionalPartsTask.itemsToLoad = "additionalparts"
    m.LoadAdditionalPartsTask.observeField("content", "onAdditionalPartsLoaded")
    m.LoadMoviesTask = CreateObject("roSGNode", "LoadItemsTask")
    m.LoadMoviesTask.itemsToLoad = "personMovies"
    m.LoadShowsTask = CreateObject("roSGNode", "LoadItemsTask")
    m.LoadShowsTask.itemsToLoad = "personTVShows"
    m.LoadSeriesTask = CreateObject("roSGNode", "LoadItemsTask")
    m.LoadSeriesTask.itemsToLoad = "personSeries"
end sub

sub updateSize()
    itemHeight = 396
    m.top.itemSize = [1710, itemHeight]
    m.top.rowItemSpacing = [36, 36]
end sub

sub loadParts(data as object)
    m.extrasGrp = m.top.getParent().findNode("extrasGrp")
    m.top.parentId = data.id
    m.people = data.People
    m.LoadAdditionalPartsTask.itemId = m.top.parentId
    m.LoadAdditionalPartsTask.control = "RUN"
end sub

sub loadPersonVideos(personId)
    m.extrasGrp = m.top.getParent().findNode("extrasGrp")
    m.personId = personId
    m.LoadMoviesTask.itemId = m.personId
    m.LoadMoviesTask.observeField("content", "onMoviesLoaded")
    m.LoadMoviesTask.control = "RUN"
end sub

sub onAdditionalPartsLoaded()
    parts = m.LoadAdditionalPartsTask.content
    m.LoadAdditionalPartsTask.unobserveField("content")

    data = CreateObject("roSGNode", "ContentNode") ' The row Node
    m.top.content = data
    if parts <> invalid and parts.count() > 0
        row = buildRow("Additional Parts", parts, 464)
        addRowSize([464, 291])
        m.top.content.appendChild(row)
        m.top.rowItemSize = [[464, 291]]
    else
        m.top.rowItemSize = [[234, 396]]
    end if

    ' Load Cast and Crew and everything else...
    m.LoadPeopleTask.peopleList = m.people
    m.LoadPeopleTask.control = "RUN"
end sub

sub onPeopleLoaded()
    people = m.LoadPeopleTask.content
    m.loadPeopleTask.unobserveField("content")
    if people <> invalid and people.count() > 0
        row = m.top.content.createChild("ContentNode")
        row.Title = tr("Cast & Crew")
        for each person in people
            if person.json.type = "Actor" and person.json.Role <> invalid and person.json.Role.ToStr().Trim() <> ""
                person.subTitle = "as " + person.json.Role
            else
                person.subTitle = person.json.Type
            end if
            person.Type = "Person"
            row.appendChild(person)
        end for
    end if
    m.LikeThisTask.itemId = m.top.parentId
    m.LikeThisTask.control = "RUN"
end sub

sub onLikeThisLoaded()
    data = m.LikeThisTask.content
    m.LikeThisTask.unobserveField("content")
    if data <> invalid and data.count() > 0
        row = m.top.content.createChild("ContentNode")
        row.Title = tr("More Like This")
        for each item in data
            item.Id = item.json.Id
            item.labelText = item.json.Name
            if item.json.ProductionYear <> invalid
                item.subTitle = stri(item.json.ProductionYear)
            else if item.json.PremiereDate <> invalid
                premierYear = CreateObject("roDateTime")
                premierYear.FromISO8601String(item.json.PremiereDate)
                item.subTitle = stri(premierYear.GetYear())
            end if
            item.Type = item.json.Type
            row.appendChild(item)
        end for
        addRowSize([234, 396])
    end if
    ' Special Features next...
    m.SpecialFeaturesTask.itemId = m.top.parentId
    m.SpecialFeaturesTask.control = "RUN"
end sub

sub onSpecialFeaturesLoaded()
    data = m.SpecialFeaturesTask.content
    m.SpecialFeaturesTask.unobserveField("content")
    if data <> invalid and data.count() > 0
        row = m.top.content.createChild("ContentNode")
        row.Title = tr("Special Features")
        for each item in data
            m.top.visible = true
            item.Id = item.json.Id
            item.labelText = item.json.Name
            item.subTitle = ""
            item.Type = item.json.Type
            item.imageWidth = 450
            row.appendChild(item)
        end for
        addRowSize([462, 372])
    end if

    showOrHideMe()
end sub

sub onMoviesLoaded()
    data = m.LoadMoviesTask.content
    m.LoadMoviesTask.unobserveField("content")
    rlContent = CreateObject("roSGNode", "ContentNode")
    if data <> invalid and data.count() > 0
        row = rlContent.createChild("ContentNode")
        row.title = tr("Movies")
        for each mov in data
            mov.Id = mov.json.Id
            mov.labelText = mov.json.Name
            mov.subTitle = mov.json.ProductionYear
            mov.Type = mov.json.Type
            row.appendChild(mov)
        end for
        m.top.rowItemSize = [[234, 396]]
    end if
    m.top.content = rlContent
    m.LoadShowsTask.itemId = m.personId
    m.LoadShowsTask.observeField("content", "onShowsLoaded")
    m.LoadShowsTask.control = "RUN"
end sub

sub onShowsLoaded()
    data = m.LoadShowsTask.content
    m.LoadShowsTask.unobserveField("content")
    if data <> invalid and data.count() > 0
        row = buildRow("TV Shows", data, 502)
        addRowSize([502, 396])
        m.top.content.appendChild(row)
    end if
    m.LoadSeriesTask.itemId = m.personId
    m.LoadSeriesTask.observeField("content", "onSeriesLoaded")
    m.LoadSeriesTask.control = "RUN"
end sub

sub onSeriesLoaded()
    data = m.LoadSeriesTask.content
    m.LoadSeriesTask.unobserveField("content")
    if data <> invalid and data.count() > 0
        row = buildRow("Series", data)
        addRowSize([234, 396])
        m.top.content.appendChild(row)
    end if
    m.top.visible = true

    showOrHideMe()
end sub

function buildRow(rowTitle as string, items, imgWdth = 0)
    row = CreateObject("roSGNode", "ContentNode")
    row.Title = tr(rowTitle)
    for each mov in items
        if LCase(mov.json.type) = "episode"
            if isAllValid([mov.json.SeriesName, mov.json.ParentIndexNumber, mov.json.IndexNumber, mov.json.Name])
                mov.labelText = mov.json.SeriesName
                mov.subTitle = `S${mov.json.ParentIndexNumber}:E${mov.json.IndexNumber} - ${mov.json.Name}`
            else
                mov.labelText = mov.json.Name
                mov.subTitle = mov.json.ProductionYear
            end if
        else
            mov.labelText = mov.json.Name
            mov.subTitle = mov.json.ProductionYear
        end if

        mov.Id = mov.json.Id
        mov.Type = mov.json.Type
        if imgWdth > 0
            mov.imageWidth = imgWdth
        end if
        row.appendChild(mov)
    end for
    return row
end function

sub addRowSize(newRow)
    sizeArray = m.top.rowItemSize
    newSizeArray = []
    for each size in sizeArray
        newSizeArray.push(size)
    end for
    newSizeArray.push(newRow)
    m.top.rowItemSize = newSizeArray
end sub

' don't show popup panel if there is nothing to show
sub showOrHideMe()
    if isValid(m.top.content)
        if m.top.content.getChildCount() = 0
            m.extrasGrp.visible = false
        else
            m.extrasGrp.visible = true
        end if
    end if
end sub

sub onRowItemSelected()
    m.top.selectedItem = m.top.content.getChild(m.top.rowItemSelected[0]).getChild(m.top.rowItemSelected[1])
end sub

sub onRowItemFocused()
    m.top.focusedItem = m.top.content.getChild(m.top.rowItemFocused[0]).getChild(m.top.rowItemFocused[1])
end sub