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

package com.google.crypto.tink;

import com.google.crypto.tink.proto.KeysetInfo;
import com.google.crypto.tink.subtle.Base64;
import com.google.crypto.tink.proto.KeyData;
import java.util.Iterator;
import com.google.gson.JsonElement;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.crypto.tink.proto.EncryptedKeyset;
import com.google.gson.JsonParseException;
import com.google.crypto.tink.proto.Keyset;
import java.nio.file.Path;
import com.google.errorprone.annotations.InlineMe;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.nio.charset.Charset;

@Deprecated
public final class JsonKeysetWriter implements KeysetWriter
{
    private static final Charset UTF_8;
    private final OutputStream outputStream;
    
    private JsonKeysetWriter(final OutputStream stream) {
        this.outputStream = stream;
    }
    
    public static KeysetWriter withOutputStream(final OutputStream stream) {
        return new JsonKeysetWriter(stream);
    }
    
    @Deprecated
    @InlineMe(replacement = "JsonKeysetWriter.withOutputStream(new FileOutputStream(file))", imports = { "com.google.crypto.tink.JsonKeysetWriter", "java.io.FileOutputStream" })
    public static KeysetWriter withFile(final File file) throws IOException {
        return withOutputStream(new FileOutputStream(file));
    }
    
    @Deprecated
    @InlineMe(replacement = "JsonKeysetWriter.withOutputStream(new FileOutputStream(new File(path)))", imports = { "com.google.crypto.tink.JsonKeysetWriter", "java.io.File", "java.io.FileOutputStream" })
    public static KeysetWriter withPath(final String path) throws IOException {
        return withOutputStream(new FileOutputStream(new File(path)));
    }
    
    @Deprecated
    @InlineMe(replacement = "JsonKeysetWriter.withOutputStream(new FileOutputStream(path.toFile()))", imports = { "com.google.crypto.tink.JsonKeysetWriter", "java.io.FileOutputStream" })
    public static KeysetWriter withPath(final Path path) throws IOException {
        return withOutputStream(new FileOutputStream(path.toFile()));
    }
    
    @Override
    public void write(final Keyset keyset) throws IOException {
        try {
            this.outputStream.write(this.toJson(keyset).toString().getBytes(JsonKeysetWriter.UTF_8));
            this.outputStream.write(System.lineSeparator().getBytes(JsonKeysetWriter.UTF_8));
        }
        catch (final JsonParseException e) {
            throw new IOException(e);
        }
        finally {
            this.outputStream.close();
        }
    }
    
    @Override
    public void write(final EncryptedKeyset keyset) throws IOException {
        this.outputStream.write(this.toJson(keyset).toString().getBytes(JsonKeysetWriter.UTF_8));
        this.outputStream.write(System.lineSeparator().getBytes(JsonKeysetWriter.UTF_8));
        this.outputStream.close();
    }
    
    private long toUnsignedLong(final int x) {
        return (long)x & 0xFFFFFFFFL;
    }
    
    private JsonObject toJson(final Keyset keyset) {
        final JsonObject json = new JsonObject();
        json.addProperty("primaryKeyId", this.toUnsignedLong(keyset.getPrimaryKeyId()));
        final JsonArray keys = new JsonArray();
        for (final Keyset.Key key : keyset.getKeyList()) {
            keys.add(this.toJson(key));
        }
        json.add("key", keys);
        return json;
    }
    
    private JsonObject toJson(final Keyset.Key key) {
        final JsonObject json = new JsonObject();
        json.add("keyData", this.toJson(key.getKeyData()));
        json.addProperty("status", key.getStatus().name());
        json.addProperty("keyId", this.toUnsignedLong(key.getKeyId()));
        json.addProperty("outputPrefixType", key.getOutputPrefixType().name());
        return json;
    }
    
    private JsonObject toJson(final KeyData keyData) {
        final JsonObject json = new JsonObject();
        json.addProperty("typeUrl", keyData.getTypeUrl());
        json.addProperty("value", Base64.encode(keyData.getValue().toByteArray()));
        json.addProperty("keyMaterialType", keyData.getKeyMaterialType().name());
        return json;
    }
    
    private JsonObject toJson(final EncryptedKeyset keyset) {
        final JsonObject json = new JsonObject();
        json.addProperty("encryptedKeyset", Base64.encode(keyset.getEncryptedKeyset().toByteArray()));
        json.add("keysetInfo", this.toJson(keyset.getKeysetInfo()));
        return json;
    }
    
    private JsonObject toJson(final KeysetInfo keysetInfo) {
        final JsonObject json = new JsonObject();
        json.addProperty("primaryKeyId", this.toUnsignedLong(keysetInfo.getPrimaryKeyId()));
        final JsonArray keyInfos = new JsonArray();
        for (final KeysetInfo.KeyInfo keyInfo : keysetInfo.getKeyInfoList()) {
            keyInfos.add(this.toJson(keyInfo));
        }
        json.add("keyInfo", keyInfos);
        return json;
    }
    
    private JsonObject toJson(final KeysetInfo.KeyInfo keyInfo) {
        final JsonObject json = new JsonObject();
        json.addProperty("typeUrl", keyInfo.getTypeUrl());
        json.addProperty("status", keyInfo.getStatus().name());
        json.addProperty("keyId", this.toUnsignedLong(keyInfo.getKeyId()));
        json.addProperty("outputPrefixType", keyInfo.getOutputPrefixType().name());
        return json;
    }
    
    static {
        UTF_8 = Charset.forName("UTF-8");
    }
}
