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

import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.NameListCodec;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequest;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequestKeyboardInteractive;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * @author Alex Usun
 */
public class SshMsgUserAuthRequestKeyboardInteractiveCodec
        implements SshMsgUserAuthRequestCodec.MethodCodec {

    @Override
    public SshMsgUserAuthRequest read(InputStream in, String username, String serviceName)
            throws IOException {
        String languageTag = StringCodec.readUsAsciiValue(in);
        List<String> subMethods = NameListCodec.readUtf8Value(in);

        return new SshMsgUserAuthRequestKeyboardInteractive(username, serviceName, languageTag, subMethods);
    }

    @Override
    public void write(SshMsgUserAuthRequest value, OutputStream out)
            throws IOException {
        SshMsgUserAuthRequestKeyboardInteractive message = (SshMsgUserAuthRequestKeyboardInteractive) value;
        StringCodec.writeUtf8Value(message.languageTag, out);
        NameListCodec.writeUtf8Value(message.subMethods, out);
    }
}
