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

import com.jscape.inet.ssh.protocol.v2.marshaling.*;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthPasswdChangeReq;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequestNewPassword;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequestNone;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequestPassword;
import com.jscape.util.CollectionTools;

import java.util.Collection;

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

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

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

    public static final SshMsgUserAuthRequestCodec.Entry[] METHOD_ENTRIES = {
            new SshMsgUserAuthRequestCodec.Entry("none", new SshMsgUserAuthRequestNoneCodec(), SshMsgUserAuthRequestNone.class),
            new SshMsgUserAuthRequestCodec.Entry("password", new SshMsgUserAuthRequestPasswordCodec(), SshMsgUserAuthRequestPassword.class, SshMsgUserAuthRequestNewPassword.class),
    };

    public static final MessageCodec.Entry[] ENTRIES = {
            new MessageCodec.Entry(50, new SshMsgUserAuthRequestCodec(METHOD_ENTRIES), REQUEST_TYPES),
            new MessageCodec.Entry(60, new SshMsgUserAuthPasswdChangeReqCodec(), SshMsgUserAuthPasswdChangeReq.class),
    };

    private final MessageCodec.Entry[] entries;

    public static MessageCodec init(MessageCodec codec) {
        return AuthenticationMessages.init(codec.set(ENTRIES));
    }

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

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

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