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

import com.jscape.inet.ssh.protocol.messages.Message;
import com.jscape.util.net.connection.MessageConnection;

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

/**
 * @author Alex Usun
 */
public class SshMsgChannelExtendedData
        extends Message<SshMsgChannelExtendedData.Handler> {

    public final int recipientChannel;
    public final int dataTypeCode;
    public final byte[] data;
    public final int offset;
    public final int length;

    public SshMsgChannelExtendedData(int recipientChannel, int dataTypeCode, byte[] data, int offset, int length) {
        this.recipientChannel = recipientChannel;
        this.dataTypeCode = dataTypeCode;

        assertNotNull(data);
        this.data = data;

        this.offset = offset;
        this.length = length;
    }

    public SshMsgChannelExtendedData(int recipientChannel, int dataTypeCode, byte[] data) {
        this(recipientChannel, dataTypeCode, data, 0, data.length);
    }

    public SshMsgChannelExtendedData(int recipientChannel, DataType dataType, byte[] data, int offset, int length) {
        this(recipientChannel, dataType.code, data, offset, length);
    }

    public SshMsgChannelExtendedData(int recipientChannel, DataType dataType, byte[] data) {
        this(recipientChannel, dataType, data, 0, data.length);
    }

    @Override
    public Class handlerClass() {
        return Handler.class;
    }

    @Override
    public void accept(Handler handler, MessageConnection<Message> connection) {
        handler.handle(this, connection);
    }

    @Override
    public String toString() {
        return "SshMsgChannelExtendedData {"
                + "recipientChannel=" + this.recipientChannel
                + ", dataTypeCode=" + this.dataTypeCode
                + ", data.length=" + this.data.length
                + ", offset=" + this.offset
                + ", length=" + this.length
                + '}';
    }

    public interface Handler
            extends Message.HandlerBase {

        void handle(SshMsgChannelExtendedData message, MessageConnection<Message> connection);

    }
}
