package com.jscape.inet.ssh.protocol.v2.authentication;

import com.jscape.inet.ssh.protocol.v2.marshaling.*;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthInfoRequest;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthInfoResponse;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequestKeyboardInteractive;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequestNone;
import com.jscape.util.CollectionTools;

import java.util.Collection;

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

/**
 * @author Alex Usun
 */
public class KeyboardInteractiveMessages
        implements MessageCodec.Factory {

    public static final Class[] REQUEST_TYPES = {
            SshMsgUserAuthRequestNone.class,
            SshMsgUserAuthRequestKeyboardInteractive.class
    };

    public static final SshMsgUserAuthRequestCodec.Entry[] METHOD_ENTRIES = {
            new SshMsgUserAuthRequestCodec.Entry("none", new SshMsgUserAuthRequestNoneCodec(), SshMsgUserAuthRequestNone.class),
            new SshMsgUserAuthRequestCodec.Entry("keyboard-interactive", new SshMsgUserAuthRequestKeyboardInteractiveCodec(), SshMsgUserAuthRequestKeyboardInteractive.class),
    };

    public static final MessageCodec.Entry[] ENTRIES = {
            new MessageCodec.Entry(50, new SshMsgUserAuthRequestCodec(METHOD_ENTRIES), REQUEST_TYPES),
            new MessageCodec.Entry(60, new SshMsgUserAuthInfoRequestCodec(), SshMsgUserAuthInfoRequest.class),
            new MessageCodec.Entry(61, new SshMsgUserAuthInfoResponseCodec(), SshMsgUserAuthInfoResponse.class),
    };

    private final MessageCodec.Entry[] entries;

    public static KeyboardInteractiveMessages defaultMessages() {
        Collection<MessageCodec.Entry> entries = CollectionTools.collect(AuthenticationMessages.ENTRIES, ENTRIES);
        return new KeyboardInteractiveMessages(entries.toArray(new MessageCodec.Entry[entries.size()]));
    }

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

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