components_tasks_BrandingConfigTask.bs

import "pkg:/source/api/ApiClient.bs"
import "pkg:/source/api/apiPool.bs"
import "pkg:/source/roku_modules/log/LogMixin.brs"
import "pkg:/source/utils/misc.bs"

sub init()
  m.log = new log.Logger("BrandingConfigTask")
  m.top.functionName = "fetchBrandingConfig"
end sub

' Orchestrator Task: submits to ApiTask pool and waits off the render thread.
' Fetches branding configuration from server and updates m.global.server.isSplashscreenEnabled.
' Sets responseCode to 200 on success, -1 on failure.
' IMPORTANT: Non-blocking — never throws errors that stop app flow.
sub fetchBrandingConfig()
  m.log.info("Fetching branding configuration from server")

  res = fetchRes(GetApi().BuildGetBrandingConfigurationRequest(), "brandingConfig")
  configData = invalid
  if isValid(res) and res.ok
    configData = res.json
  end if

  if isValid(configData)
    m.log.debug("Branding config response received", formatJson(configData))

    ' Update global server node with splashscreen status
    serverNode = m.global.server
    if isValid(configData.SplashscreenEnabled)
      serverNode.isSplashscreenEnabled = configData.SplashscreenEnabled
      m.log.info("Splashscreen status cached", "enabled:", configData.SplashscreenEnabled)
    else
      ' API returned data but no SplashscreenEnabled field — default to false
      serverNode.isSplashscreenEnabled = false
      m.log.warn("SplashscreenEnabled field not found in response - defaulting to false")
    end if

    m.top.responseCode = 200
  else
    ' API call failed — default to false, log warning, continue normally
    m.log.warn("Failed to fetch branding configuration - defaulting to disabled")
    m.global.server.isSplashscreenEnabled = false
    m.top.responseCode = -1
    m.top.failureReason = "API call failed or returned invalid"
  end if

  m.log.debug("BrandingConfigTask completed", "responseCode:", m.top.responseCode)
end sub

' Reset task to default state
sub empty()
  m.top.responseCode = invalid
  m.top.failureReason = ""
end sub