/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityManager$UniqueType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.Functions$Unary
 *  com.l7tech.util.HexUtils
 *  com.l7tech.util.MasterPasswordManager
 *  jakarta.inject.Inject
 *  jakarta.inject.Named
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.l7tech.server.secureconversation;

import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityManager;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.secureconversation.StoredSecureConversationSession;
import com.l7tech.server.secureconversation.StoredSecureConversationSessionManager;
import com.l7tech.server.secureconversation.a;
import com.l7tech.server.secureconversation.b;
import com.l7tech.util.Functions;
import com.l7tech.util.HexUtils;
import com.l7tech.util.MasterPasswordManager;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.text.ParseException;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class})
public class StoredSecureConversationSessionManagerImpl
extends HibernateEntityManager<StoredSecureConversationSession, EntityHeader>
implements StoredSecureConversationSessionManager {
    @Inject
    @Named(value="dbPasswordEncryption")
    private MasterPasswordManager clusterEncryptionManager;
    private static final String[] m;

    @Override
    public StoredSecureConversationSession findInboundSessionByIdentifier(String string) throws FindException {
        return this.a(StoredSecureConversationSession.generateSessionKey(string), this.b(string));
    }

    @Override
    public StoredSecureConversationSession findOutboundSessionByUserAndService(Goid goid, String string, String string2) throws FindException {
        return this.a(StoredSecureConversationSession.generateSessionKey(goid, string, string2), this.a(goid, string, string2));
    }

    @Override
    public void deleteInboundSessionByIdentifer(String string) throws DeleteException {
        this.b(StoredSecureConversationSession.generateSessionKey(string), this.b(string));
    }

    @Override
    public void deleteOutboundSessionByUserAndService(Goid goid, String string, String string2) throws DeleteException {
        this.b(StoredSecureConversationSession.generateSessionKey(goid, string, string2), this.a(goid, string, string2));
    }

    @Override
    public void deleteStale(long l2) throws DeleteException {
        this.getHibernateTemplate().bulkUpdate(m[1], new Object[]{l2});
    }

    @Override
    public void deleteAll() throws DeleteException {
        this.getHibernateTemplate().bulkUpdate(m[4], new Object[0]);
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public String encryptSessionKey(byte[] byArray) {
        return this.clusterEncryptionManager.encryptPassword(HexUtils.encodeBase64((byte[])byArray).toCharArray());
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public byte[] decryptSessionKey(String string) throws FindException {
        try {
            return HexUtils.decodeBase64((String)new String(this.clusterEncryptionManager.decryptPassword(string)));
        }
        catch (ParseException parseException) {
            throw new FindException(m[3], (Throwable)parseException);
        }
    }

    @Transactional(propagation=Propagation.SUPPORTS)
    public Class<StoredSecureConversationSession> getImpClass() {
        return StoredSecureConversationSession.class;
    }

    @Override
    protected EntityManager.UniqueType getUniqueType() {
        return EntityManager.UniqueType.OTHER;
    }

    @Override
    protected Collection<Map<String, Object>> getUniqueConstraints(StoredSecureConversationSession storedSecureConversationSession) {
        return Collections.singleton(this.a(storedSecureConversationSession.getSessionKey()));
    }

    private Map<String, Object> a(String string) {
        return Collections.singletonMap(m[0], string);
    }

    private StoredSecureConversationSession a(String string, Functions.Unary<Boolean, StoredSecureConversationSession> unary) throws FindException {
        StoredSecureConversationSession storedSecureConversationSession = (StoredSecureConversationSession)((Object)this.findUnique(this.a(string)));
        if (!((Boolean)unary.call((Object)storedSecureConversationSession)).booleanValue()) {
            storedSecureConversationSession = null;
        }
        return storedSecureConversationSession;
    }

    private void b(String string, Functions.Unary<Boolean, StoredSecureConversationSession> unary) throws DeleteException {
        try {
            StoredSecureConversationSession storedSecureConversationSession = this.a(string, unary);
            try {
                if (storedSecureConversationSession != null) {
                    this.delete(storedSecureConversationSession);
                }
            }
            catch (FindException findException) {
                throw StoredSecureConversationSessionManagerImpl.a(findException);
            }
        }
        catch (FindException findException) {
            throw new DeleteException(m[2], (Throwable)findException);
        }
    }

    private Functions.Unary<Boolean, StoredSecureConversationSession> b(String string) {
        return new a(this, string);
    }

    private Functions.Unary<Boolean, StoredSecureConversationSession> a(Goid goid, String string, String string2) {
        return new b(this, goid, string, string2);
    }

    private static FindException a(FindException findException) {
        return findException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "\u001eO\u0014+\u0019\u001dN&O\u001e<\tO\u000b=\u0004\u0017\u0000>^\b*\u0015\u0016s\bI\u0012*\u00151O\u0003\\\u0002*\u0003\u0013T\u0004E\t\u000b\u0015\u0001S\u0004E\tx\u0003RW\u0005O\u0015=P\u0001\u000e\bR\u00171\u0002\u0017SM\u0016Gg\"(X\u00157\u0002RF\u0004D\u00031\u001e\u0015\u0000\u001eO\u0014+\u0019\u001dNML\b*P\u0016E\u0001O\u00131\u001f\u001c";
                var4_3 = "\u001eO\u0014+\u0019\u001dN&O\u001e<\tO\u000b=\u0004\u0017\u0000>^\b*\u0015\u0016s\bI\u0012*\u00151O\u0003\\\u0002*\u0003\u0013T\u0004E\t\u000b\u0015\u0001S\u0004E\tx\u0003RW\u0005O\u0015=P\u0001\u000e\bR\u00171\u0002\u0017SM\u0016Gg\"(X\u00157\u0002RF\u0004D\u00031\u001e\u0015\u0000\u001eO\u0014+\u0019\u001dNML\b*P\u0016E\u0001O\u00131\u001f\u001c".length();
                var1_4 = 10;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = StoredSecureConversationSessionManagerImpl.b(v0, StoredSecureConversationSessionManagerImpl.d(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = ":F\u00048\u001e\u0015\u0002\u001bGE>\u0017\u0013P\u0016X\u0011z\u0001\u0015Q\u001cA\n4R\u001bG\u0016&\u000bM\t?\u0006\u0015\u0002<\\\n(\u0017\u0014q\nK\u0010(\u00173M\u0001^\u0000(\u0001\u0011V\u0006G\u000b\t\u0017\u0003Q\u0006G\u000b";
                    var4_3 = ":F\u00048\u001e\u0015\u0002\u001bGE>\u0017\u0013P\u0016X\u0011z\u0001\u0015Q\u001cA\n4R\u001bG\u0016&\u000bM\t?\u0006\u0015\u0002<\\\n(\u0017\u0014q\nK\u0010(\u00173M\u0001^\u0000(\u0001\u0011V\u0006G\u000b\t\u0017\u0003Q\u0006G\u000b".length();
                    var1_4 = 29;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 19;
                        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++] = StoredSecureConversationSessionManagerImpl.b(v0, StoredSecureConversationSessionManagerImpl.d(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;
            }
        }
        StoredSecureConversationSessionManagerImpl.m = var5;
    }

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(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 -> 124;
                    case 1 -> 59;
                    case 2 -> 118;
                    case 3 -> 73;
                    case 4 -> 97;
                    case 5 -> 99;
                    default -> 49;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

