package com.jscape.inet.sftp.protocol.v4.messages;

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

import java.util.Arrays;

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

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

    public final int id;
    public final byte[] handle;
    public final long offset;
    public final byte[] data;
    public final int dataOffset;
    public final int dataLength;

    public SshFxpWrite(int id, byte[] handle, long offset, byte[] data, int dataOffset, int dataLength) {
        this.id = id;

        assertNotNull(handle);
        this.handle = handle;

        this.offset = offset;

        assertNotNull(data);
        this.data = data;

        this.dataOffset = dataOffset;
        this.dataLength = dataLength;
    }

    public SshFxpWrite(int id, byte[] handle, long offset, byte[] data) {
        this(id, handle, offset, data, 0, data.length);
    }

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

    @Override
    public String toString() {
        return "SshFxpWrite {"
                + "id=" + this.id
                + ", handle=" + Arrays.toString(this.handle)
                + ", offset=" + this.offset
                + ", dataLength=" + this.dataLength
                + '}';
    }

    public interface Handler
            extends Message.HandlerBase {

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

    }
}
