/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.LicenseException
 *  com.l7tech.gateway.common.audit.Audit
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.composite.ForEachLoopAssertion
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.policy.variable.Syntax
 *  org.jboss.util.collection.ArrayIterator
 *  org.springframework.beans.factory.BeanFactory
 */
package com.l7tech.server.policy.assertion.composite;

import com.l7tech.gateway.common.LicenseException;
import com.l7tech.gateway.common.audit.Audit;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.composite.ForEachLoopAssertion;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.composite.ServerCompositeAssertion;
import com.l7tech.server.policy.assertion.composite.c;
import com.l7tech.server.policy.variable.ExpandVariables;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import org.jboss.util.collection.ArrayIterator;
import org.springframework.beans.factory.BeanFactory;

public class ServerForEachLoopAssertion
extends ServerCompositeAssertion<ForEachLoopAssertion> {
    private final String h;
    private final String i;
    private final int j;
    private final String k;
    private final String l;
    private final String m;
    private final ServerCompositeAssertion.AssertionResultListener n = new c(this);
    private static final String[] p;

    public ServerForEachLoopAssertion(ForEachLoopAssertion forEachLoopAssertion, BeanFactory beanFactory) throws PolicyAssertionException, LicenseException {
        super(forEachLoopAssertion, beanFactory);
        this.h = forEachLoopAssertion.getLoopVariableName();
        this.j = forEachLoopAssertion.getIterationLimit();
        String string = forEachLoopAssertion.getVariablePrefix();
        String[] stringArray = p;
        this.i = string + stringArray[0];
        this.k = string + stringArray[2];
        this.l = string + stringArray[1];
        this.m = string + ForEachLoopAssertion.BREAK;
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        AssertionStatus assertionStatus;
        Object object = this.b(policyEnforcementContext);
        Iterator iterator = ServerForEachLoopAssertion.b(object);
        int n2 = 0;
        boolean bl2 = false;
        boolean bl3 = false;
        try {
            if (!this.a(policyEnforcementContext, this.m)) {
                policyEnforcementContext.setVariable(this.m, Boolean.FALSE);
            }
        }
        catch (IOException iOException) {
            throw ServerForEachLoopAssertion.a(iOException);
        }
        policyEnforcementContext.setVariable(this.l, bl2);
        while (true) {
            block14: {
                try {
                    try {
                        if (!iterator.hasNext()) break;
                        if (this.j <= 0) break block14;
                    }
                    catch (IOException iOException) {
                        throw ServerForEachLoopAssertion.a(iOException);
                    }
                    if (n2 < this.j) break block14;
                }
                catch (IOException iOException) {
                    throw ServerForEachLoopAssertion.a(iOException);
                }
                bl2 = true;
                break;
            }
            Object e10 = iterator.next();
            policyEnforcementContext.setVariable(this.i, e10);
            policyEnforcementContext.setVariable(this.k, n2);
            AssertionStatus assertionStatus2 = this.iterateChildren(policyEnforcementContext, this.n);
            if (!AssertionStatus.NONE.equals(assertionStatus2)) {
                bl3 = true;
                break;
            }
            try {
                if (this.a(policyEnforcementContext)) {
                    break;
                }
            }
            catch (IOException iOException) {
                throw ServerForEachLoopAssertion.a(iOException);
            }
            ++n2;
        }
        try {
            policyEnforcementContext.setVariable(this.k, n2);
            policyEnforcementContext.setVariable(this.l, bl2);
            assertionStatus = bl3 ? AssertionStatus.FAILED : AssertionStatus.NONE;
        }
        catch (IOException iOException) {
            throw ServerForEachLoopAssertion.a(iOException);
        }
        return assertionStatus;
    }

    private boolean a(PolicyEnforcementContext policyEnforcementContext, String string) {
        try {
            policyEnforcementContext.getVariable(string);
            return true;
        }
        catch (NoSuchVariableException noSuchVariableException) {
            return false;
        }
    }

    private boolean a(PolicyEnforcementContext policyEnforcementContext) {
        boolean bl2;
        try {
            Object object = policyEnforcementContext.getVariable(this.m);
            bl2 = this.a(object);
        }
        catch (NoSuchVariableException noSuchVariableException) {
            return false;
        }
        return bl2;
    }

    private boolean a(Object object) {
        if (object instanceof Boolean) {
            return (Boolean)object;
        }
        if (object instanceof String) {
            String string = (String)object;
            return Boolean.parseBoolean(string);
        }
        if (object instanceof Number) {
            Number number = (Number)object;
            return 0L != number.longValue();
        }
        return false;
    }

    private Object b(PolicyEnforcementContext policyEnforcementContext) {
        Audit audit = this.getAudit();
        Map<String, Object> map = policyEnforcementContext.getVariableMap(new String[]{this.h}, audit);
        Object object = ExpandVariables.processSingleVariableAsObject(Syntax.SYNTAX_PREFIX + this.h + "}", map, audit);
        if (object == null) {
            return Collections.emptyList();
        }
        return object;
    }

    private static Iterator b(Object object) {
        Object object2 = object instanceof Object[] ? new ArrayIterator((Object[])object) : (object instanceof Collection ? ((Collection)object).iterator() : Collections.singletonList(object).iterator());
        return object2;
    }

    private static IOException a(IOException iOException) {
        return iOException;
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = "wU$Cy\u0019J-\u000ewS)Rn\u0019@<R=Xf\u0015P\u000bw_%Ty\u001dP0Y?B";
        int n3 = "wU$Cy\u0019J-\u000ewS)Rn\u0019@<R=Xf\u0015P\u000bw_%Ty\u001dP0Y?B".length();
        int n4 = 8;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServerForEachLoopAssertion.c(64, ServerForEachLoopAssertion.b(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                p = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x64);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 25;
                    case 1 -> 118;
                    case 2 -> 17;
                    case 3 -> 113;
                    case 4 -> 75;
                    case 5 -> 60;
                    default -> 100;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

