<?xml version="1.0" encoding="UTF-8"?>
<component name="ValidateCodeTask" extends="Task">

    <interface>
        <field id="codeToValidate" type="string"/>
        <field id="validated" type="boolean" value="false"/>
        <field id="username" type="string"/>
        <field id="password" type="string"/>
        <field id="errorMsg" type="string"/>
    </interface>

    <script type="text/brightscript">
    <![CDATA[
        sub init()
            m.top.functionName = "ValidateActivationCode"
        end sub
        
        sub ValidateActivationCode()
            code = m.top.codeToValidate
            if code = "" or code = invalid
                return
            end if
            
            maxAttempts = 120
            attemptCount = 0
            
            while attemptCount < maxAttempts
                attemptCount = attemptCount + 1
                
                request = CreateObject("roUrlTransfer")
                url = "http://rokupro.contentmastersrebrand.com/checking.php?code=" + code
                print "ValidateCodeTask - Checking code: "; code
                request.SetUrl(url)
                
                response = request.GetToString()
                
                if response = invalid or response = ""
                    sleep(5000)
                    continue while
                end if
                
                ' CRÍTICO: Limpiar respuesta antes de parsear
                data = response.Trim()
                json = ParseJSON(data)
                
                if json = invalid
                    print "ValidateCodeTask - Invalid JSON"
                    sleep(5000)
                    continue while
                end if
                
                print "ValidateCodeTask - Response status: "; json.status
                
                if json.status = "success"
                    print "ValidateCodeTask - SUCCESS!"
                    print "ValidateCodeTask - Username: "; json.name
                    
                    ' Guardar en fields para LoginScreen
                    m.top.username = json.name
                    m.top.password = json.passw
                    m.top.validated = true
                    exit while
                    
                else if json.status = "pending"
                    print "ValidateCodeTask - Still pending..."
                    sleep(5000)
                    
                else if json.status = "expired"
                    print "ValidateCodeTask - Code expired"
                    m.top.errorMsg = "Code expired"
                    exit while
                    
                else
                    print "ValidateCodeTask - Unknown status: "; json.status
                    sleep(5000)
                end if
            end while
            
            if not m.top.validated
                print "ValidateCodeTask - Timeout"
                m.top.errorMsg = "Validation timeout"
            end if
        end sub
    ]]>
    </script>

</component>