package com.jscape.inet.ssh.protocol.v2.transport.keyexchange;

import com.jscape.inet.ssh.protocol.v2.marshaling.*;
import com.jscape.inet.ssh.protocol.v2.marshaling.keys.PublicKeyCodec;
import com.jscape.inet.ssh.protocol.v2.marshaling.keys.PublicKeyCodecs;
import com.jscape.inet.ssh.protocol.v2.marshaling.keys.SignatureCodec;
import com.jscape.inet.ssh.protocol.v2.marshaling.keys.SignatureCodecs;
import com.jscape.inet.ssh.protocol.v2.messages.*;
import com.jscape.util.crypto.CryptoServicesProvider;
import com.jscape.util.io.DataCodec;

import java.security.Provider;
import java.security.PublicKey;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Uzun
 */
public class EcDiffieHellmanKeyExchangeMessages
        implements MessageCodec.Factory {

    private final MessageCodec.Entry[] entries;

    public static EcDiffieHellmanKeyExchangeMessages defaultMessages(String curveIdentifier) {
        return defaultMessages(curveIdentifier, CryptoServicesProvider.instance());
    }

    public static EcDiffieHellmanKeyExchangeMessages defaultMessages(String curveIdentifier, Object provider) {
        return provider instanceof String ? defaultMessages(curveIdentifier, (String) provider)
                                          : provider instanceof Provider ? defaultMessages(curveIdentifier, (Provider) provider)
                                                                         : defaultMessages(curveIdentifier);
    }

    public static EcDiffieHellmanKeyExchangeMessages defaultMessages(String curveIdentifier, String provider) {
        DataCodec<PublicKey> publicKeyCodec = PublicKeyCodecs.init(new PublicKeyCodec(), provider);
        DataCodec<Signature> signatureCodec = SignatureCodecs.init(new SignatureCodec());

        return new EcDiffieHellmanKeyExchangeMessages(new MessageCodec.Entry(30, new SshMsgKexEcdhInitCodec(curveIdentifier, provider), SshMsgKexEcdhInit.class),
                                                      new MessageCodec.Entry(31, new SshMsgKexEcdhReplyCodec(publicKeyCodec, signatureCodec, curveIdentifier, provider), SshMsgKexEcdhReply.class));
    }

    public static EcDiffieHellmanKeyExchangeMessages defaultMessages(String curveIdentifier, Provider provider) {
        DataCodec<PublicKey> publicKeyCodec = PublicKeyCodecs.init(new PublicKeyCodec(), provider);
        DataCodec<Signature> signatureCodec = SignatureCodecs.init(new SignatureCodec());

        return new EcDiffieHellmanKeyExchangeMessages(new MessageCodec.Entry(30, new SshMsgKexEcdhInitCodec(curveIdentifier, provider), SshMsgKexEcdhInit.class),
                                                      new MessageCodec.Entry(31, new SshMsgKexEcdhReplyCodec(publicKeyCodec, signatureCodec, curveIdentifier, provider), SshMsgKexEcdhReply.class));
    }

    public EcDiffieHellmanKeyExchangeMessages(MessageCodec.Entry... entries) {
        assertNotNull(entries);
        this.entries = entries;
    }

    @Override
    public MessageCodec update(MessageCodec codec) {
        return codec.set(this.entries);
    }
}
