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

package com.google.crypto.tink.streamingaead;

import com.google.crypto.tink.streamingaead.internal.LegacyFullStreamingAead;
import com.google.crypto.tink.internal.PrimitiveRegistry;
import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import java.util.List;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.KeyStatus;
import java.util.ArrayList;
import com.google.crypto.tink.internal.MonitoringAnnotations;
import com.google.crypto.tink.internal.KeysetHandleInterface;
import com.google.crypto.tink.internal.LegacyProtoKey;
import com.google.crypto.tink.internal.PrimitiveConstructor;
import com.google.crypto.tink.StreamingAead;
import com.google.crypto.tink.internal.PrimitiveWrapper;

public class StreamingAeadWrapper implements PrimitiveWrapper<StreamingAead, StreamingAead>
{
    private static final StreamingAeadWrapper WRAPPER;
    private static final PrimitiveConstructor<LegacyProtoKey, StreamingAead> LEGACY_FULL_STREAMING_AEAD_PRIMITIVE_CONSTRUCTOR;
    
    StreamingAeadWrapper() {
    }
    
    @Override
    public StreamingAead wrap(final KeysetHandleInterface handle, final MonitoringAnnotations annotations, final PrimitiveFactory<StreamingAead> factory) throws GeneralSecurityException {
        final List<StreamingAead> allStreamingAeads = new ArrayList<StreamingAead>();
        for (int i = 0; i < handle.size(); ++i) {
            final KeysetHandleInterface.Entry entry = handle.getAt(i);
            if (entry.getStatus().equals(KeyStatus.ENABLED)) {
                final StreamingAead streamingAead = factory.create(entry);
                allStreamingAeads.add(streamingAead);
            }
        }
        final KeysetHandleInterface.Entry primaryEntry = handle.getPrimary();
        if (primaryEntry == null) {
            throw new GeneralSecurityException("No primary set");
        }
        final StreamingAead primaryStreamingAead = factory.create(primaryEntry);
        if (primaryStreamingAead == null) {
            throw new GeneralSecurityException("No primary set");
        }
        return new StreamingAeadHelper(allStreamingAeads, primaryStreamingAead);
    }
    
    @Override
    public Class<StreamingAead> getPrimitiveClass() {
        return StreamingAead.class;
    }
    
    @Override
    public Class<StreamingAead> getInputPrimitiveClass() {
        return StreamingAead.class;
    }
    
    public static void register() throws GeneralSecurityException {
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveWrapper((PrimitiveWrapper<Object, Object>)StreamingAeadWrapper.WRAPPER);
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveConstructor(StreamingAeadWrapper.LEGACY_FULL_STREAMING_AEAD_PRIMITIVE_CONSTRUCTOR);
    }
    
    public static void registerToInternalPrimitiveRegistry(final PrimitiveRegistry.Builder primitiveRegistryBuilder) throws GeneralSecurityException {
        primitiveRegistryBuilder.registerPrimitiveWrapper((PrimitiveWrapper<Object, Object>)StreamingAeadWrapper.WRAPPER);
    }
    
    static {
        WRAPPER = new StreamingAeadWrapper();
        LEGACY_FULL_STREAMING_AEAD_PRIMITIVE_CONSTRUCTOR = PrimitiveConstructor.create(LegacyFullStreamingAead::create, LegacyProtoKey.class, StreamingAead.class);
    }
}
