/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.Component
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.AuditRecord
 *  com.l7tech.gateway.common.audit.SystemAuditRecord
 *  com.l7tech.gateway.common.audit.SystemMessages
 *  com.l7tech.identity.User
 *  com.l7tech.message.Message
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.polback.BackgroundTask
 *  com.l7tech.security.token.OpaqueSecurityToken
 *  com.l7tech.security.token.SecurityToken
 *  jakarta.inject.Inject
 *  org.jetbrains.annotations.NotNull
 *  org.jetbrains.annotations.Nullable
 */
package com.l7tech.server.task;

import com.l7tech.gateway.common.Component;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.AuditRecord;
import com.l7tech.gateway.common.audit.SystemAuditRecord;
import com.l7tech.gateway.common.audit.SystemMessages;
import com.l7tech.identity.User;
import com.l7tech.message.Message;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.polback.BackgroundTask;
import com.l7tech.security.token.OpaqueSecurityToken;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.identity.AuthenticationResult;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.message.PolicyEnforcementContextFactory;
import com.l7tech.server.polback.PolicyBackedServiceRegistry;
import com.l7tech.server.task.ScheduledTaskJobManager;
import com.l7tech.server.task.a;
import jakarta.inject.Inject;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/*
 * Exception performing whole class analysis ignored.
 */
public class ScheduledPolicyRunner {
    private static Logger a;
    @Inject
    protected PolicyBackedServiceRegistry pbsReg;
    private static ScheduledPolicyRunner b;
    @NotNull
    private ScheduledTaskJobManager c;
    private Auditor d;
    private static final String[] e;

    public static void setInstance(ScheduledPolicyRunner scheduledPolicyRunner) {
        b = scheduledPolicyRunner;
    }

    @NotNull
    public static ScheduledPolicyRunner getInstance(@NotNull ScheduledTaskJobManager scheduledTaskJobManager) {
        ScheduledPolicyRunner scheduledPolicyRunner;
        try {
            if (scheduledTaskJobManager == null) {
                ScheduledPolicyRunner.a(0);
            }
        }
        catch (RuntimeException runtimeException) {
            throw ScheduledPolicyRunner.a(runtimeException);
        }
        try {
            if (b == null) {
                b = new ScheduledPolicyRunner(scheduledTaskJobManager);
            }
        }
        catch (RuntimeException runtimeException) {
            throw ScheduledPolicyRunner.a(runtimeException);
        }
        try {
            scheduledPolicyRunner = b;
            if (scheduledPolicyRunner == null) {
                ScheduledPolicyRunner.a(1);
            }
        }
        catch (RuntimeException runtimeException) {
            throw ScheduledPolicyRunner.a(runtimeException);
        }
        return scheduledPolicyRunner;
    }

    @Nullable
    public static ScheduledPolicyRunner getInstance() {
        return b;
    }

    private ScheduledPolicyRunner(ScheduledTaskJobManager scheduledTaskJobManager) {
        this.c = scheduledTaskJobManager;
    }

    public boolean isClusterMaster() {
        return this.c.clusterMaster.isMaster();
    }

    public void runBackgroundTask(String string, Goid goid, Goid goid2, String string2) {
        User user;
        BackgroundTask backgroundTask;
        PolicyEnforcementContext policyEnforcementContext;
        Method method;
        String[] stringArray;
        block7: {
            try {
                stringArray = e;
                method = BackgroundTask.class.getMethod(stringArray[3], new Class[0]);
            }
            catch (NoSuchMethodException noSuchMethodException) {
                throw new RuntimeException(noSuchMethodException);
            }
            policyEnforcementContext = PolicyEnforcementContextFactory.createPolicyEnforcementContext(new Message(), new Message());
            try {
                if (goid2 == null || string2 == null) break block7;
            }
            catch (NoSuchMethodException noSuchMethodException) {
                throw ScheduledPolicyRunner.a(noSuchMethodException);
            }
            try {
                backgroundTask = this.c.identityProviderFactory.getProvider(goid2);
                if (backgroundTask == null) {
                    String[] stringArray2 = new String[1];
                    stringArray = e;
                    stringArray2[0] = stringArray[4];
                    this.a().logAndAudit((AuditDetailMessage)SystemMessages.SCHEDULER_POLICY_ERROR, stringArray2);
                    return;
                }
                user = backgroundTask.getUserManager().findByPrimaryKey(string2);
                AuthenticationResult authenticationResult = new AuthenticationResult(user, (SecurityToken)new OpaqueSecurityToken(user.getLogin(), null));
                policyEnforcementContext.getDefaultAuthenticationContext().addAuthenticationResult(authenticationResult);
            }
            catch (FindException findException) {
                String[] stringArray3 = new String[1];
                stringArray = e;
                stringArray3[0] = stringArray[6];
                this.a().logAndAudit((AuditDetailMessage)SystemMessages.SCHEDULER_POLICY_ERROR, stringArray3);
            }
        }
        backgroundTask = (BackgroundTask)this.c.pbsReg.getImplementationProxyForSingleMethod(method, goid, policyEnforcementContext);
        stringArray = e;
        user = new SystemAuditRecord(Level.INFO, this.c.nodeId, Component.GW_SCHEDULED_TASK, stringArray[11] + string, false, null, null, null, stringArray[0], this.c.clusterInfoManager.getSelfNodeInf().getAddress());
        this.c.auditContextFactory.doWithNewAuditContext((AuditRecord)user, new a(this, backgroundTask, string, policyEnforcementContext));
    }

    private Auditor a() {
        try {
            if (this.d == null) {
                this.d = new Auditor(this, this.c.applicationContext, a);
            }
        }
        catch (RuntimeException runtimeException) {
            throw ScheduledPolicyRunner.a(runtimeException);
        }
        return this.d;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[12];
                var3_1 = 0;
                var2_2 = "Z0\u0012\u000bO \bmU!&I+\u001fA\nB*\u001eiZ<3O \u000e\u0003Z0\u0012\u001ba!\u0019JO;&Qe\fVT$;L \u000e\u0004U=&\b#\u0013QU6,K*\u0011\u000bWe&M&\u0014\u000bH7 ^ \u000e\u000bO3!Cj/GS76])\u0019@k=>A&\u0005vN<<M7\u001ba!\u0019JO;&Qe\fVT$;L \u000e\u0004U=&\b#\u0013QU6>i7\u001bQV7<\\e\u001aKIr\u0012f*\bjN>>\b5\u001dVZ?7\\ \u000e\u0004\u001cw!\u000fe\u0013B\u001bw!\u0006`\u000f\u0004V'!\\e\u0012KOr0Me\u0012QW>*h\u000b\u0013Pu'>De\u0011AO:=LeYW\u0015w!\b(\tWOr<G1\\V^&'Z+\\JN>>,K*\u0011\u000bWe&M&\u0014\u000bH7 ^ \u000e\u000bO3!Cj/GS76])\u0019@k=>A&\u0005vN<<M7";
                var4_3 = "Z0\u0012\u000bO \bmU!&I+\u001fA\nB*\u001eiZ<3O \u000e\u0003Z0\u0012\u001ba!\u0019JO;&Qe\fVT$;L \u000e\u0004U=&\b#\u0013QU6,K*\u0011\u000bWe&M&\u0014\u000bH7 ^ \u000e\u000bO3!Cj/GS76])\u0019@k=>A&\u0005vN<<M7\u001ba!\u0019JO;&Qe\fVT$;L \u000e\u0004U=&\b#\u0013QU6>i7\u001bQV7<\\e\u001aKIr\u0012f*\bjN>>\b5\u001dVZ?7\\ \u000e\u0004\u001cw!\u000fe\u0013B\u001bw!\u0006`\u000f\u0004V'!\\e\u0012KOr0Me\u0012QW>*h\u000b\u0013Pu'>De\u0011AO:=LeYW\u0015w!\b(\tWOr<G1\\V^&'Z+\\JN>>,K*\u0011\u000bWe&M&\u0014\u000bH7 ^ \u000e\u000bO3!Cj/GS76])\u0019@k=>A&\u0005vN<<M7".length();
                var1_4 = 3;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 90;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ScheduledPolicyRunner.a(v0, ScheduledPolicyRunner.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "`\u000f'Bz\u000e\tf\u00040n/B\u00126ha\t\u0014i\rsiu\u001e\u0016`\u0018<~z\u0019]w\u0005?bw\u0004]a\u0005!+g\u001e\u0015b\u000e&gq\u0019]s\u000b `4";
                    var4_3 = "`\u000f'Bz\u000e\tf\u00040n/B\u00126ha\t\u0014i\rsiu\u001e\u0016`\u0018<~z\u0019]w\u0005?bw\u0004]a\u0005!+g\u001e\u0015b\u000e&gq\u0019]s\u000b `4".length();
                    var1_4 = 11;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 117;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = ScheduledPolicyRunner.a(v0, ScheduledPolicyRunner.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        ScheduledPolicyRunner.e = var5;
        ScheduledPolicyRunner.a = Logger.getLogger(ScheduledPolicyRunner.class.getName());
    }

    /*
     * Exception decompiling
     */
    private static /* synthetic */ void a(int var0) {
        /*
         * This method has failed to decompile.  When submitting a bug report, please provide this stack trace, and (if you hold appropriate legal rights) the relevant class file.
         * 
         * org.benf.cfr.reader.util.ConfusedCFRException: Tried to end blocks [0[TRYBLOCK]], but top level block is 6[SWITCH]
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.processEndingBlocks(Op04StructuredStatement.java:435)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op04StructuredStatement.buildNestedBlocks(Op04StructuredStatement.java:484)
         *     at org.benf.cfr.reader.bytecode.analysis.opgraph.Op03SimpleStatement.createInitialStructuredBlock(Op03SimpleStatement.java:736)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisInner(CodeAnalyser.java:850)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysisOrWrapFail(CodeAnalyser.java:278)
         *     at org.benf.cfr.reader.bytecode.CodeAnalyser.getAnalysis(CodeAnalyser.java:201)
         *     at org.benf.cfr.reader.entities.attributes.AttributeCode.analyse(AttributeCode.java:94)
         *     at org.benf.cfr.reader.entities.Method.analyse(Method.java:531)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseMid(ClassFile.java:1050)
         *     at org.benf.cfr.reader.entities.ClassFile.analyseTop(ClassFile.java:942)
         *     at org.benf.cfr.reader.Driver.doJarVersionTypes(Driver.java:257)
         *     at org.benf.cfr.reader.Driver.doJar(Driver.java:139)
         *     at org.benf.cfr.reader.CfrDriverImpl.analyse(CfrDriverImpl.java:76)
         *     at org.benf.cfr.reader.Main.main(Main.java:54)
         */
        throw new IllegalStateException("Decompilation failed");
    }

    private static Exception a(Exception exception) {
        return exception;
    }

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(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 -> 114;
                    case 1 -> 31;
                    case 2 -> 38;
                    case 3 -> 126;
                    case 4 -> 97;
                    case 5 -> 8;
                    default -> 8;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

