/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.ca.apim.gateway.extension.sharedstate.counter.CounterFieldOfInterest
 *  com.ca.apim.gateway.extension.sharedstate.counter.SharedCounterState
 *  com.ca.apim.gateway.extension.sharedstate.counter.exception.CounterLimitReachedException
 */
package com.l7tech.server.extension.provider.sharedstate.counter;

import com.ca.apim.gateway.extension.sharedstate.counter.CounterFieldOfInterest;
import com.ca.apim.gateway.extension.sharedstate.counter.SharedCounterState;
import com.ca.apim.gateway.extension.sharedstate.counter.exception.CounterLimitReachedException;
import com.l7tech.server.sla.CounterInfo;
import java.lang.invoke.LambdaMetafactory;
import java.time.Clock;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Counter {
    private String a;
    private long b;
    private final EnumMap<CounterFieldOfInterest, Long> c = new EnumMap(CounterFieldOfInterest.class);
    private static final Collection<CounterFieldOfInterest> d;
    private static final Logger e;
    private static final String[] f;

    public Counter() {
        for (CounterFieldOfInterest counterFieldOfInterest : d) {
            this.c.put(counterFieldOfInterest, 0L);
        }
        this.b = Clock.systemUTC().millis();
    }

    public Counter(String string) {
        this();
        this.a = string;
    }

    public String getName() {
        return this.a;
    }

    public void setName(String string) {
        this.a = string;
    }

    public long getLastUpdate() {
        return this.b;
    }

    public void setLastUpdate(long l2) {
        this.b = l2;
    }

    public long getCounterField(CounterFieldOfInterest counterFieldOfInterest) {
        return this.c.get(counterFieldOfInterest);
    }

    public void setCounterField(CounterFieldOfInterest counterFieldOfInterest, long l2) {
        this.c.put(counterFieldOfInterest, l2);
    }

    private boolean a(CounterFieldOfInterest counterFieldOfInterest, long l2, long l3) {
        boolean bl2;
        try {
            bl2 = this.getCounterField(counterFieldOfInterest) + l2 > l3;
        }
        catch (ArithmeticException arithmeticException) {
            throw Counter.a(arithmeticException);
        }
        return bl2;
    }

    public void updateCounter(long l2, long l3) {
        this.b(l2);
        for (CounterFieldOfInterest counterFieldOfInterest : d) {
            this.a(counterFieldOfInterest, l3);
        }
        this.b = l2;
    }

    public void updateCounterWithoutExceedingLimit(CounterFieldOfInterest counterFieldOfInterest, long l2, long l3, long l4) throws CounterLimitReachedException {
        try {
            this.b(l2);
            if (this.a(counterFieldOfInterest, l3, l4)) {
                throw new CounterLimitReachedException(f[2] + this.a);
            }
        }
        catch (CounterLimitReachedException counterLimitReachedException) {
            throw Counter.a((Exception)((Object)counterLimitReachedException));
        }
        for (CounterFieldOfInterest counterFieldOfInterest2 : d) {
            this.a(counterFieldOfInterest2, l3);
        }
        this.b = l2;
    }

    private void a(CounterFieldOfInterest counterFieldOfInterest, long l2) {
        try {
            this.setCounterField(counterFieldOfInterest, Math.addExact(this.getCounterField(counterFieldOfInterest), l2));
        }
        catch (ArithmeticException arithmeticException) {
            this.a(counterFieldOfInterest);
        }
    }

    private void a(CounterFieldOfInterest counterFieldOfInterest) {
        e.log(Level.WARNING, f[4], new Object[]{this.a, counterFieldOfInterest.getName()});
    }

    private ZonedDateTime a(long l2) {
        return ZonedDateTime.ofInstant(Instant.ofEpochMilli(l2), ZoneOffset.systemDefault());
    }

    private void b(long l2) {
        try {
            if (!this.a(this.a(l2))) {
                this.setCounterField(CounterFieldOfInterest.SEC, 0L);
            }
        }
        catch (ArithmeticException arithmeticException) {
            throw Counter.a(arithmeticException);
        }
        try {
            if (!this.b(this.a(l2))) {
                this.setCounterField(CounterFieldOfInterest.MIN, 0L);
            }
        }
        catch (ArithmeticException arithmeticException) {
            throw Counter.a(arithmeticException);
        }
        try {
            if (!this.c(this.a(l2))) {
                this.setCounterField(CounterFieldOfInterest.HOUR, 0L);
            }
        }
        catch (ArithmeticException arithmeticException) {
            throw Counter.a(arithmeticException);
        }
        try {
            if (!this.d(this.a(l2))) {
                this.setCounterField(CounterFieldOfInterest.DAY, 0L);
            }
        }
        catch (ArithmeticException arithmeticException) {
            throw Counter.a(arithmeticException);
        }
        try {
            if (!this.e(this.a(l2))) {
                this.setCounterField(CounterFieldOfInterest.MONTH, 0L);
            }
        }
        catch (ArithmeticException arithmeticException) {
            throw Counter.a(arithmeticException);
        }
    }

    private boolean a(ZonedDateTime zonedDateTime) {
        return this.a(this.b).truncatedTo(ChronoUnit.SECONDS).equals(zonedDateTime.truncatedTo(ChronoUnit.SECONDS));
    }

    private boolean b(ZonedDateTime zonedDateTime) {
        return this.a(this.b).truncatedTo(ChronoUnit.MINUTES).equals(zonedDateTime.truncatedTo(ChronoUnit.MINUTES));
    }

    private boolean c(ZonedDateTime zonedDateTime) {
        return this.a(this.b).truncatedTo(ChronoUnit.HOURS).equals(zonedDateTime.truncatedTo(ChronoUnit.HOURS));
    }

    private boolean d(ZonedDateTime zonedDateTime) {
        return this.a(this.b).truncatedTo(ChronoUnit.DAYS).equals(zonedDateTime.truncatedTo(ChronoUnit.DAYS));
    }

    private boolean e(ZonedDateTime zonedDateTime) {
        boolean bl2;
        block5: {
            block4: {
                try {
                    try {
                        if (this.a(this.b).getYear() != zonedDateTime.getYear() || this.a(this.b).getMonthValue() != zonedDateTime.getMonthValue()) break block4;
                    }
                    catch (ArithmeticException arithmeticException) {
                        throw Counter.a(arithmeticException);
                    }
                    bl2 = true;
                    break block5;
                }
                catch (ArithmeticException arithmeticException) {
                    throw Counter.a(arithmeticException);
                }
            }
            bl2 = false;
        }
        return bl2;
    }

    public Map<CounterFieldOfInterest, Long> toMap() {
        return new EnumMap<CounterFieldOfInterest, Long>(this.c);
    }

    public SharedCounterState toCounterState() {
        return new CounterInfo(this.a, this.toMap(), new Date(this.b));
    }

    public String toString() {
        String string = String.valueOf(this.c);
        long l2 = this.b;
        String string2 = this.a;
        String[] stringArray = f;
        return stringArray[3] + string2 + stringArray[0] + l2 + stringArray[1] + string + "}";
    }

    private static /* synthetic */ boolean lambda$static$0(CounterFieldOfInterest counterFieldOfInterest) {
        boolean bl2;
        try {
            bl2 = counterFieldOfInterest != CounterFieldOfInterest.NONE;
        }
        catch (ArithmeticException arithmeticException) {
            throw Counter.a(arithmeticException);
        }
        return bl2;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "H5u9BZ\u0018:i14WLQ\u0006C984S\u0014\u001a#p8<W\t\u001e\nx6=FML\tv'u@F\u0019\u0001m0'\u0003";
                var4_3 = "H5u9BZ\u0018:i14WLQ\u0006C984S\u0014\u001a#p8<W\t\u001e\nx6=FML\tv'u@F\u0019\u0001m0'\u0003".length();
                var1_4 = 14;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = Counter.a(v0, Counter.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "?e3(D_\r\u0007d'+U\u0007Xh?e3(D_\r\\qv;\u0010R\u001e\u000f*4#QY\u0017\u0019nf2X__\u001ee3(T[\r\u0005*0'\\O\u001a\\l)4\u0010N\u0017\u0019*=wM\u001a\u0019\u0015o*\"\u001e\u001a<\u0013\u007f(2UH_\nk*3U\u001a\b\u0015f*f^U\u000b\\i.'^]\u001a\\k(?\u0010\\\n\u000e~.#B\u0014";
                    var4_3 = "?e3(D_\r\u0007d'+U\u0007Xh?e3(D_\r\\qv;\u0010R\u001e\u000f*4#QY\u0017\u0019nf2X__\u001ee3(T[\r\u0005*0'\\O\u001a\\l)4\u0010N\u0017\u0019*=wM\u001a\u0019\u0015o*\"\u001e\u001a<\u0013\u007f(2UH_\nk*3U\u001a\b\u0015f*f^U\u000b\\i.'^]\u001a\\k(?\u0010\\\n\u000e~.#B\u0014".length();
                    var1_4 = 14;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 40;
                        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++] = Counter.a(v0, Counter.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;
            }
        }
        Counter.f = var5;
        Counter.d = Collections.unmodifiableList(Stream.of(CounterFieldOfInterest.values()).filter((Predicate<CounterFieldOfInterest>)LambdaMetafactory.metafactory(null, null, null, (Ljava/lang/Object;)Z, lambda$static$0(com.ca.apim.gateway.extension.sharedstate.counter.CounterFieldOfInterest ), (Lcom/ca/apim/gateway/extension/sharedstate/counter/CounterFieldOfInterest;)Z)()).collect(Collectors.toList()));
        Counter.e = Logger.getLogger(Counter.class.getName());
    }

    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] ^ 0x57);
        }
        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 -> 84;
                    case 1 -> 34;
                    case 2 -> 110;
                    case 3 -> 110;
                    case 4 -> 24;
                    case 5 -> 18;
                    default -> 87;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

