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

import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.Uint32Codec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgChannelRequest;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgChannelRequestPtyReq;
import com.jscape.inet.ssh.protocol.v2.messages.TerminalMode;

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

/**
 * @author Alex Usun
 */
public class SshMsgChannelRequestPtyReqCodec
        implements SshMsgChannelRequestCodec.RequestCodec {

    @Override
    public SshMsgChannelRequest read(InputStream in, int recipientChannel, boolean wantReply)
            throws IOException {
        String terminalEnvironmentVariable = StringCodec.readUsAsciiValue(in);
        int terminalCharactersWidth = Uint32Codec.readValue(in);
        int terminalRowsHeight = Uint32Codec.readValue(in);
        int terminalPixelsWidth = Uint32Codec.readValue(in);
        int terminalPixelsHeight = Uint32Codec.readValue(in);
        List<TerminalMode> terminalModes = TerminalModesCodec.readValue(in);

        return new SshMsgChannelRequestPtyReq(recipientChannel,
                                              wantReply,
                                              terminalEnvironmentVariable,
                                              terminalCharactersWidth,
                                              terminalRowsHeight,
                                              terminalPixelsWidth,
                                              terminalPixelsHeight,
                                              terminalModes);
    }

    @Override
    public void write(SshMsgChannelRequest value, OutputStream out)
            throws IOException {
        SshMsgChannelRequestPtyReq message = (SshMsgChannelRequestPtyReq) value;
        StringCodec.writeUsAsciiValue(message.terminalEnvironmentVariable, out);
        Uint32Codec.writeValue(message.terminalCharactersWidth, out);
        Uint32Codec.writeValue(message.terminalRowsHeight, out);
        Uint32Codec.writeValue(message.terminalPixelsWidth, out);
        Uint32Codec.writeValue(message.terminalPixelsHeight, out);
        TerminalModesCodec.writeValue(message.terminalModes, out);
    }
}
