/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.ca.datasources.cassandra.CassandraUtil
 *  com.datastax.oss.driver.api.core.CqlSession
 *  com.datastax.oss.driver.api.core.CqlSessionBuilder
 *  com.datastax.oss.driver.api.core.config.DefaultDriverOption
 *  com.datastax.oss.driver.api.core.config.DriverOption
 *  com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder
 *  com.datastax.oss.driver.api.core.ssl.SslEngineFactory
 *  com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder
 *  com.l7tech.gateway.common.cassandra.CassandraConnection
 *  com.l7tech.gateway.common.cassandra.CassandraConstants
 *  com.l7tech.util.Config
 *  com.typesafe.config.Config
 *  com.typesafe.config.ConfigFactory
 *  com.typesafe.config.ConfigMergeable
 *  org.apache.commons.lang3.StringUtils
 */
package com.l7tech.server.cassandra;

import com.ca.datasources.cassandra.CassandraUtil;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.config.DriverOption;
import com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder;
import com.datastax.oss.driver.api.core.ssl.SslEngineFactory;
import com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder;
import com.l7tech.gateway.common.cassandra.CassandraConnection;
import com.l7tech.gateway.common.cassandra.CassandraConstants;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigMergeable;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

public class CassandraSessionBuilder {
    private static final String a;
    private static final String b;
    private final CassandraConnection c;
    private final com.l7tech.util.Config d;
    private final Map<String, String> e;
    private Function<CassandraConnection, String> f;
    private Function<CassandraConnection, SslEngineFactory> g;
    private static final String[] h;

    public CassandraSessionBuilder(CassandraConnection cassandraConnection, com.l7tech.util.Config config) {
        this.c = cassandraConnection;
        this.d = config;
        this.e = cassandraConnection.getProperties();
    }

    public CassandraSessionBuilder passwordSupplier(Function<CassandraConnection, String> function) {
        this.f = function;
        return this;
    }

    public CassandraSessionBuilder sslEngineFactorySupplier(Function<CassandraConnection, SslEngineFactory> function) {
        this.g = function;
        return this;
    }

    public CqlSession build() {
        ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder = this.b();
        this.configureDriverConfigLoader(programmaticDriverConfigLoaderBuilder);
        CqlSessionBuilder cqlSessionBuilder = this.a();
        this.configureSession(cqlSessionBuilder, programmaticDriverConfigLoaderBuilder);
        return (CqlSession)cqlSessionBuilder.build();
    }

    protected void configureDriverConfigLoader(ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder) {
        this.a(programmaticDriverConfigLoaderBuilder);
        this.b(programmaticDriverConfigLoaderBuilder);
        this.c(programmaticDriverConfigLoaderBuilder);
        this.d(programmaticDriverConfigLoaderBuilder);
        this.e(programmaticDriverConfigLoaderBuilder);
    }

    protected void configureSession(CqlSessionBuilder cqlSessionBuilder, ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder) {
        cqlSessionBuilder.withConfigLoader(programmaticDriverConfigLoaderBuilder.build());
        if (this.g != null && this.c.isSsl()) {
            cqlSessionBuilder.withSslEngineFactory(this.g.apply(this.c));
        }
    }

    CqlSessionBuilder a() {
        return CqlSession.builder();
    }

    ProgrammaticDriverConfigLoaderBuilder b() {
        String[] stringArray = h;
        String string = this.e.get(stringArray[4]);
        if (StringUtils.isNotEmpty((CharSequence)string)) {
            this.e.clear();
            return new DefaultProgrammaticDriverConfigLoaderBuilder(() -> ConfigFactory.defaultApplication().withFallback((ConfigMergeable)ConfigFactory.parseString((String)string)).withFallback((ConfigMergeable)this.c()).withFallback((ConfigMergeable)ConfigFactory.defaultReference((ClassLoader)CqlSession.class.getClassLoader())).resolve(), stringArray[7]);
        }
        return new DefaultProgrammaticDriverConfigLoaderBuilder(() -> ConfigFactory.defaultApplication().withFallback((ConfigMergeable)this.c()).withFallback((ConfigMergeable)ConfigFactory.defaultReference((ClassLoader)CqlSession.class.getClassLoader())).resolve(), h[10]);
    }

    private String a(DefaultDriverOption defaultDriverOption) {
        return h[11] + defaultDriverOption.getPath();
    }

    private Config c() {
        Integer n2;
        HashMap<String, Comparable<Integer>> hashMap = new HashMap<String, Comparable<Integer>>();
        String[] stringArray = h;
        hashMap.put(this.a(DefaultDriverOption.CONNECTION_MAX_REQUESTS), Integer.valueOf(this.d.getIntProperty(stringArray[5], 1024)));
        hashMap.put(this.a(DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT), Duration.ofMillis(this.d.getIntProperty(stringArray[16], 5000)));
        hashMap.put(this.a(DefaultDriverOption.REQUEST_TIMEOUT), Duration.ofMillis(this.d.getIntProperty(stringArray[14], 12000)));
        hashMap.put(this.a(DefaultDriverOption.SOCKET_KEEP_ALIVE), Boolean.valueOf(this.d.getBooleanProperty(stringArray[1], true)));
        Integer n3 = CassandraUtil.getInteger((Object)this.d.getProperty(stringArray[8]));
        if (n3 != null) {
            hashMap.put(this.a(DefaultDriverOption.SOCKET_RECEIVE_BUFFER_SIZE), n3);
        }
        stringArray = h;
        CassandraUtil.getBoolean((Object)this.d.getProperty(stringArray[13])).ifPresent(bl2 -> hashMap.put(this.a(DefaultDriverOption.SOCKET_REUSE_ADDRESS), (Comparable<Integer>)bl2));
        Integer n4 = CassandraUtil.getInteger((Object)this.d.getProperty(stringArray[12]));
        if (n4 != null) {
            hashMap.put(this.a(DefaultDriverOption.SOCKET_SEND_BUFFER_SIZE), n4);
        }
        if ((n2 = CassandraUtil.getInteger((Object)this.d.getProperty((stringArray = h)[6]))) != null) {
            hashMap.put(this.a(DefaultDriverOption.SOCKET_LINGER_INTERVAL), n2);
        }
        stringArray = h;
        hashMap.put(this.a(DefaultDriverOption.SOCKET_TCP_NODELAY), Boolean.valueOf(this.d.getBooleanProperty(stringArray[9], false)));
        hashMap.put(this.a(DefaultDriverOption.REQUEST_PAGE_SIZE), Integer.valueOf(this.d.getIntProperty(stringArray[3], 5000)));
        return ConfigFactory.parseMap(hashMap);
    }

    private void a(ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder) {
        String string;
        if (StringUtils.isNotEmpty((CharSequence)this.c.getKeyspaceName())) {
            programmaticDriverConfigLoaderBuilder.withString((DriverOption)DefaultDriverOption.SESSION_KEYSPACE, this.c.getKeyspaceName());
        }
        if (StringUtils.isNotEmpty((CharSequence)(string = this.e.get(CassandraConstants.LOCAL_DC_NAME)))) {
            programmaticDriverConfigLoaderBuilder.withString((DriverOption)DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, string);
        }
        if (StringUtils.isNotEmpty((CharSequence)this.c.getUsername())) {
            programmaticDriverConfigLoaderBuilder.withString((DriverOption)DefaultDriverOption.AUTH_PROVIDER_CLASS, h[0]);
            programmaticDriverConfigLoaderBuilder.withString((DriverOption)DefaultDriverOption.AUTH_PROVIDER_USER_NAME, this.c.getUsername());
            if (this.f != null) {
                programmaticDriverConfigLoaderBuilder.withString((DriverOption)DefaultDriverOption.AUTH_PROVIDER_PASSWORD, this.f.apply(this.c));
            }
        }
        String string3 = this.c.getPort();
        programmaticDriverConfigLoaderBuilder.withStringList((DriverOption)DefaultDriverOption.CONTACT_POINTS, Arrays.stream(this.c.getContactPointsAsArray()).map(string2 -> String.format(h[2], string2, string3)).collect(Collectors.toList()));
        if (StringUtils.isNotEmpty((CharSequence)this.c.getCompression())) {
            programmaticDriverConfigLoaderBuilder.withString((DriverOption)DefaultDriverOption.PROTOCOL_COMPRESSION, this.c.getCompression());
        }
    }

    private void b(ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder) {
        Integer n2;
        if (this.e.containsKey(CassandraConstants.MAX_CONNECTIONS_PER_LOCAL_HOST)) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE, CassandraUtil.getIntOrDefault((String)this.e.get(CassandraConstants.MAX_CONNECTIONS_PER_LOCAL_HOST), (int)1));
        }
        if (this.e.containsKey(CassandraConstants.MAX_CONNECTIONS_PER_REMOTE_HOST)) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE, CassandraUtil.getIntOrDefault((String)this.e.get(CassandraConstants.MAX_CONNECTIONS_PER_REMOTE_HOST), (int)1));
        }
        if ((n2 = CassandraUtil.getInteger((Object)this.e.get(CassandraConstants.MAX_SIMULTANEOUS_REQUESTS_PER_HOST_THRESHOLD))) != null) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.CONNECTION_MAX_REQUESTS, n2.intValue());
        }
    }

    private void c(ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder) {
        Integer n2;
        Integer n3;
        Integer n4 = CassandraUtil.getInteger((Object)this.e.get(CassandraConstants.CONNECTION_TIMEOUT_MILLIS));
        if (n4 != null) {
            programmaticDriverConfigLoaderBuilder.withDuration((DriverOption)DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT, Duration.ofMillis(n4.intValue()));
        }
        if ((n3 = CassandraUtil.getInteger((Object)this.e.get(CassandraConstants.READ_TIMEOUT_MILLIS))) != null) {
            programmaticDriverConfigLoaderBuilder.withDuration((DriverOption)DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(n3.intValue()));
        }
        CassandraUtil.getBoolean((Object)this.e.get(CassandraConstants.KEEP_ALIVE)).ifPresent(bl2 -> programmaticDriverConfigLoaderBuilder.withBoolean((DriverOption)DefaultDriverOption.SOCKET_KEEP_ALIVE, bl2.booleanValue()));
        Integer n5 = CassandraUtil.getInteger((Object)this.e.get(CassandraConstants.RECEIVE_BUFFER_SIZE));
        if (n5 != null) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.SOCKET_RECEIVE_BUFFER_SIZE, n5.intValue());
        }
        CassandraUtil.getBoolean((Object)this.e.get(CassandraConstants.REUSE_ADDRESS)).ifPresent(bl2 -> programmaticDriverConfigLoaderBuilder.withBoolean((DriverOption)DefaultDriverOption.SOCKET_REUSE_ADDRESS, bl2.booleanValue()));
        Integer n6 = CassandraUtil.getInteger((Object)this.e.get(CassandraConstants.SEND_BUFFER_SIZE));
        if (n6 != null) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.SOCKET_SEND_BUFFER_SIZE, n6.intValue());
        }
        if ((n2 = CassandraUtil.getInteger((Object)this.e.get(CassandraConstants.SO_LINGER))) != null) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.SOCKET_LINGER_INTERVAL, n2.intValue());
        }
        CassandraUtil.getBoolean((Object)this.e.get(CassandraConstants.TCP_NO_DELAY)).ifPresent(bl2 -> programmaticDriverConfigLoaderBuilder.withBoolean((DriverOption)DefaultDriverOption.SOCKET_TCP_NODELAY, bl2.booleanValue()));
    }

    private void d(ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder) {
        if (this.e.containsKey(CassandraConstants.NUM_OF_USED_HOSTS_PER_REMOTE_DC)) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_MAX_NODES_PER_REMOTE_DC, CassandraUtil.getIntOrDefault((String)this.e.get(CassandraConstants.NUM_OF_USED_HOSTS_PER_REMOTE_DC), (int)0));
        }
        if (this.e.containsKey(CassandraConstants.ALLOW_REMOTE_DCS_FOR_LOCAL_CONSISTENCY_LEVEL)) {
            programmaticDriverConfigLoaderBuilder.withBoolean((DriverOption)DefaultDriverOption.LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS, CassandraUtil.getBooleanOrDefault((String)this.e.get(CassandraConstants.ALLOW_REMOTE_DCS_FOR_LOCAL_CONSISTENCY_LEVEL), (boolean)false));
        }
    }

    private void e(ProgrammaticDriverConfigLoaderBuilder programmaticDriverConfigLoaderBuilder) {
        Integer n2 = CassandraUtil.getInteger((Object)this.e.get(CassandraConstants.QUERY_FETCH_SIZE));
        if (n2 != null) {
            programmaticDriverConfigLoaderBuilder.withInt((DriverOption)DefaultDriverOption.REQUEST_PAGE_SIZE, n2.intValue());
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[17];
                var3_1 = 0;
                var2_2 = "U\u00125>\u0001\u0002p}\n\u0015\"\u001b>Ew\u0011\">\u000b3g\u0013f\u001f'$\u000e8qw\u001fz<\n3eD\u0012=!\n\u0005 \rnr\u001c f\u001f'$\u000e8qw\u001fz1\n\"vm-=-\nxq`\u00185\"\u0003\"Cd\u0012!2\fa\f=!\n$Vj\u00102>\b1f\u001f'$\u000e8qw\u001fz:\u000e.Fl\u0013!;\u001b7{`\u0011!$=3dp\u001b'#\u001c\u0006pw6;$\u001b\u0002}w\u001b'?\u0000:q\u0012f\u001f'$\u000e8qw\u001fz$\u0000\u001a|k\u00191%\u0014a\u001f 6\u001c\"t}S>6\u001978a\f=!\n$\u001bf\u001f'$\u000e8qw\u001fz%\n5pl\b1\u0015\u001a0s`\f\u0007>\u00153\u0014f\u001f'$\u000e8qw\u001fz#\f&[j:1;\u000e/\u0014a\u001f 6\u001c\"t}S>6\u001978a\f=!\n$\u0015a\u001f 6\u001c\"t}S>6\u001978a\f=!\n$;\u0018f\u001f'$\u000e8qw\u001fz$\n8qG\u000b21\n$Fl\u00041\u0016f\u001f'$\u000e8qw\u001fz%\n#f`?03\u001d3fv\u001bf\u001f'$\u000e8qw\u001fz%\n7qQ\u001792\u0000#aH\u00178;\u0006%";
                var4_3 = "U\u00125>\u0001\u0002p}\n\u0015\"\u001b>Ew\u0011\">\u000b3g\u0013f\u001f'$\u000e8qw\u001fz<\n3eD\u0012=!\n\u0005 \rnr\u001c f\u001f'$\u000e8qw\u001fz1\n\"vm-=-\nxq`\u00185\"\u0003\"Cd\u0012!2\fa\f=!\n$Vj\u00102>\b1f\u001f'$\u000e8qw\u001fz:\u000e.Fl\u0013!;\u001b7{`\u0011!$=3dp\u001b'#\u001c\u0006pw6;$\u001b\u0002}w\u001b'?\u0000:q\u0012f\u001f'$\u000e8qw\u001fz$\u0000\u001a|k\u00191%\u0014a\u001f 6\u001c\"t}S>6\u001978a\f=!\n$\u001bf\u001f'$\u000e8qw\u001fz%\n5pl\b1\u0015\u001a0s`\f\u0007>\u00153\u0014f\u001f'$\u000e8qw\u001fz#\f&[j:1;\u000e/\u0014a\u001f 6\u001c\"t}S>6\u001978a\f=!\n$\u0015a\u001f 6\u001c\"t}S>6\u001978a\f=!\n$;\u0018f\u001f'$\u000e8qw\u001fz$\n8qG\u000b21\n$Fl\u00041\u0016f\u001f'$\u000e8qw\u001fz%\n#f`?03\u001d3fv\u001bf\u001f'$\u000e8qw\u001fz%\n7qQ\u001792\u0000#aH\u00178;\u0006%".length();
                var1_4 = 21;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = CassandraSessionBuilder.a(v0, CassandraSessionBuilder.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "9TeyR|\u000e2HjfP\u001e>G\u007f|V`)/G\"lX`#8Ex[^c(2SxB^b!4U";
                    var4_3 = "9TeyR|\u000e2HjfP\u001e>G\u007f|V`)/G\"lX`#8Ex[^c(2SxB^b!4U".length();
                    var1_4 = 12;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 47;
                        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++] = CassandraSessionBuilder.a(v0, CassandraSessionBuilder.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;
            }
        }
        CassandraSessionBuilder.h = var5;
        CassandraSessionBuilder.a = CassandraSessionBuilder.h[10];
        CassandraSessionBuilder.b = CassandraSessionBuilder.h[15];
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x62);
        }
        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 -> 9;
                    case 2 -> 35;
                    case 3 -> 32;
                    case 4 -> 24;
                    case 5 -> 33;
                    default -> 98;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

