<?xml version="1.0" encoding="UTF-8"?>
<component name="LoginTask" extends="Task">
    <script type="text/brightscript" uri="pkg:/source/utils/nodeUtils.brs" />
    <script type="text/brightscript" uri="pkg:/source/utils/generalUtils.brs" />
    <script type="text/brightscript" uri="pkg:/source/utils/urlUtils.brs" />
    <script type="text/brightscript">
        <![CDATA[
        sub init()
            m.top.functionName = "ValidateLogin"
        end sub

sub ValidateLogin()
    user = m.top.user
    pass = m.top.pass

    ' ⚠️ Validar que m.global.config_activa sea un array antes de recorrer
    if m.global.config_activa <> invalid and type(m.global.config_activa) = "roArray" and m.global.config_activa.Count() > 0
        for each cfg in m.global.config_activa
            url = cfg.serverURL + "/player_api.php?username=" + user.Escape() + "&password=" + pass.Escape()
            http = NewHttp(url)
            rsp = http.GetToStringWithRetry()
            json = ParseJson(rsp)

            if json <> invalid and json.user_info <> invalid and json.user_info.auth = 1
                ' ✅ Convertir config a assocarray con el servidor exitoso
                m.global.AddField("config", "assocarray", true)
                m.global.setField("config", {
                    serverURL: cfg.serverURL,
                    appName: cfg.appName,
                    api_key: cfg.api_key,
                    version: cfg.version
                })

                appName = cfg.appName

                ' ✅ Guardar en registro
                regWrite("lock", "0000", appName)
                regWrite("channelCategory", FormatJSON([]), appName)
                regWrite("moviesCategory", FormatJSON([]), appName)
                regWrite("seriesCategory", FormatJSON([]), appName)
                regWrite("live", FormatJSON([]), appName)
                regWrite("movie", FormatJSON([]), appName)
                regWrite("series", FormatJSON([]), appName)
                regWrite("sort", "auto", appName)
                regWrite("streamFormat", "hls", appName)
                regWrite("userTV", json.user_info.username, appName)
                regWrite("passTV", json.user_info.password, appName)

                ' ✅ Variables globales
                m.global.user = json.user_info.username
                m.global.pass = json.user_info.password
                m.global.expire = json.user_info.exp_date
                m.global.sort = "auto"
                m.global.streamFormat = "hls"
                m.global.timeServer = json.server_info.timestamp_now

                m.top.status = json.user_info.status
                m.top.validated = true

                return ' ✅ Listo
            end if
        end for
    else
        ? "❌ m.global.config_activa no es válido o está vacío"
    end if

    ' ❌ Ningún servidor aceptó login
    m.top.validated = false
end sub

        ]]>
    </script>
    <interface>
        <field id="user" type="string"/>
        <field id="pass" type="string"/>
        <field id="status" type="string"/>
        <field id="validated" type="boolean" value="false"/>
    </interface>
</component>
