// 
// Decompiled by Procyon v0.6.0
// 

package io.sentry;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.Enumeration;
import org.jetbrains.annotations.ApiStatus;
import io.sentry.protocol.FeatureFlags;
import io.sentry.protocol.Spring;
import io.sentry.util.HintUtils;
import io.sentry.protocol.Response;
import io.sentry.protocol.Gpu;
import io.sentry.protocol.SentryRuntime;
import io.sentry.protocol.OperatingSystem;
import io.sentry.protocol.Device;
import io.sentry.protocol.Browser;
import io.sentry.protocol.App;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.NotNull;
import io.sentry.protocol.Contexts;

public final class CombinedContextsView extends Contexts
{
    private static final long serialVersionUID = 3585992094653318439L;
    @NotNull
    private final Contexts globalContexts;
    @NotNull
    private final Contexts isolationContexts;
    @NotNull
    private final Contexts currentContexts;
    @NotNull
    private final ScopeType defaultScopeType;
    
    public CombinedContextsView(@NotNull final Contexts globalContexts, @NotNull final Contexts isolationContexts, @NotNull final Contexts currentContexts, @NotNull final ScopeType defaultScopeType) {
        this.globalContexts = globalContexts;
        this.isolationContexts = isolationContexts;
        this.currentContexts = currentContexts;
        this.defaultScopeType = defaultScopeType;
    }
    
    @Nullable
    @Override
    public SpanContext getTrace() {
        final SpanContext current = this.currentContexts.getTrace();
        if (current != null) {
            return current;
        }
        final SpanContext isolation = this.isolationContexts.getTrace();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getTrace();
    }
    
    @Override
    public void setTrace(@NotNull final SpanContext traceContext) {
        this.getDefaultContexts().setTrace(traceContext);
    }
    
    @NotNull
    private Contexts getDefaultContexts() {
        switch (this.defaultScopeType) {
            case CURRENT: {
                return this.currentContexts;
            }
            case ISOLATION: {
                return this.isolationContexts;
            }
            case GLOBAL: {
                return this.globalContexts;
            }
            default: {
                return this.currentContexts;
            }
        }
    }
    
    @Nullable
    @Override
    public App getApp() {
        final App current = this.currentContexts.getApp();
        if (current != null) {
            return current;
        }
        final App isolation = this.isolationContexts.getApp();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getApp();
    }
    
    @Override
    public void setApp(@NotNull final App app) {
        this.getDefaultContexts().setApp(app);
    }
    
    @Nullable
    @Override
    public Browser getBrowser() {
        final Browser current = this.currentContexts.getBrowser();
        if (current != null) {
            return current;
        }
        final Browser isolation = this.isolationContexts.getBrowser();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getBrowser();
    }
    
    @Override
    public void setBrowser(@NotNull final Browser browser) {
        this.getDefaultContexts().setBrowser(browser);
    }
    
    @Nullable
    @Override
    public Device getDevice() {
        final Device current = this.currentContexts.getDevice();
        if (current != null) {
            return current;
        }
        final Device isolation = this.isolationContexts.getDevice();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getDevice();
    }
    
    @Override
    public void setDevice(@NotNull final Device device) {
        this.getDefaultContexts().setDevice(device);
    }
    
    @Nullable
    @Override
    public OperatingSystem getOperatingSystem() {
        final OperatingSystem current = this.currentContexts.getOperatingSystem();
        if (current != null) {
            return current;
        }
        final OperatingSystem isolation = this.isolationContexts.getOperatingSystem();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getOperatingSystem();
    }
    
    @Override
    public void setOperatingSystem(@NotNull final OperatingSystem operatingSystem) {
        this.getDefaultContexts().setOperatingSystem(operatingSystem);
    }
    
    @Nullable
    @Override
    public SentryRuntime getRuntime() {
        final SentryRuntime current = this.currentContexts.getRuntime();
        if (current != null) {
            return current;
        }
        final SentryRuntime isolation = this.isolationContexts.getRuntime();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getRuntime();
    }
    
    @Override
    public void setRuntime(@NotNull final SentryRuntime runtime) {
        this.getDefaultContexts().setRuntime(runtime);
    }
    
    @Nullable
    @Override
    public Gpu getGpu() {
        final Gpu current = this.currentContexts.getGpu();
        if (current != null) {
            return current;
        }
        final Gpu isolation = this.isolationContexts.getGpu();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getGpu();
    }
    
    @Override
    public void setGpu(@NotNull final Gpu gpu) {
        this.getDefaultContexts().setGpu(gpu);
    }
    
    @Nullable
    @Override
    public Response getResponse() {
        final Response current = this.currentContexts.getResponse();
        if (current != null) {
            return current;
        }
        final Response isolation = this.isolationContexts.getResponse();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getResponse();
    }
    
    @Override
    public void withResponse(final HintUtils.SentryConsumer<Response> callback) {
        if (this.currentContexts.getResponse() != null) {
            this.currentContexts.withResponse(callback);
        }
        else if (this.isolationContexts.getResponse() != null) {
            this.isolationContexts.withResponse(callback);
        }
        else if (this.globalContexts.getResponse() != null) {
            this.globalContexts.withResponse(callback);
        }
        else {
            this.getDefaultContexts().withResponse(callback);
        }
    }
    
    @Override
    public void setResponse(@NotNull final Response response) {
        this.getDefaultContexts().setResponse(response);
    }
    
    @Nullable
    @Override
    public Spring getSpring() {
        final Spring current = this.currentContexts.getSpring();
        if (current != null) {
            return current;
        }
        final Spring isolation = this.isolationContexts.getSpring();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getSpring();
    }
    
    @Override
    public void setSpring(@NotNull final Spring spring) {
        this.getDefaultContexts().setSpring(spring);
    }
    
    @Nullable
    @Override
    public FeatureFlags getFeatureFlags() {
        final FeatureFlags current = this.currentContexts.getFeatureFlags();
        if (current != null) {
            return current;
        }
        final FeatureFlags isolation = this.isolationContexts.getFeatureFlags();
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.getFeatureFlags();
    }
    
    @ApiStatus.Internal
    @Override
    public void setFeatureFlags(@NotNull final FeatureFlags spring) {
        this.getDefaultContexts().setFeatureFlags(spring);
    }
    
    @Override
    public int size() {
        return this.mergeContexts().size();
    }
    
    @Override
    public int getSize() {
        return this.size();
    }
    
    @Override
    public boolean isEmpty() {
        return this.globalContexts.isEmpty() && this.isolationContexts.isEmpty() && this.currentContexts.isEmpty();
    }
    
    @Override
    public boolean containsKey(@Nullable final Object key) {
        return this.globalContexts.containsKey(key) || this.isolationContexts.containsKey(key) || this.currentContexts.containsKey(key);
    }
    
    @Nullable
    @Override
    public Object get(@Nullable final Object key) {
        final Object current = this.currentContexts.get(key);
        if (current != null) {
            return current;
        }
        final Object isolation = this.isolationContexts.get(key);
        if (isolation != null) {
            return isolation;
        }
        return this.globalContexts.get(key);
    }
    
    @Nullable
    @Override
    public Object put(@Nullable final String key, @Nullable final Object value) {
        return this.getDefaultContexts().put(key, value);
    }
    
    @Nullable
    @Override
    public Object remove(@Nullable final Object key) {
        return this.getDefaultContexts().remove(key);
    }
    
    @NotNull
    @Override
    public Enumeration<String> keys() {
        return this.mergeContexts().keys();
    }
    
    @NotNull
    @Override
    public Set<Map.Entry<String, Object>> entrySet() {
        return this.mergeContexts().entrySet();
    }
    
    @Override
    public void serialize(@NotNull final ObjectWriter writer, @NotNull final ILogger logger) throws IOException {
        this.mergeContexts().serialize(writer, logger);
    }
    
    @Nullable
    @Override
    public Object set(@Nullable final String key, @Nullable final Object value) {
        return this.put(key, value);
    }
    
    @Override
    public void putAll(@Nullable final Map<? extends String, ?> m) {
        this.getDefaultContexts().putAll(m);
    }
    
    @Override
    public void putAll(@Nullable final Contexts contexts) {
        this.getDefaultContexts().putAll(contexts);
    }
    
    @NotNull
    private Contexts mergeContexts() {
        final Contexts allContexts = new Contexts();
        allContexts.putAll(this.globalContexts);
        allContexts.putAll(this.isolationContexts);
        allContexts.putAll(this.currentContexts);
        return allContexts;
    }
}
