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

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

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

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

    public final int id;
    public final String filename;
    public final FileOpenFlags openFlags;
    public final FileAttributes attributes;

    public static SshFxpOpen reading(int id, String filename, boolean textModeRequired) {
        return new SshFxpOpen(id, filename, new FileOpenFlags(true, false, false, false, false, false, textModeRequired), FileAttributes.empty());
    }

    public static SshFxpOpen writing(int id, String filename, boolean truncate, boolean textModeRequired) {
        return new SshFxpOpen(id, filename, new FileOpenFlags(false, true, false, true, truncate, false, textModeRequired), FileAttributes.empty());
    }

    public static SshFxpOpen appending(int id, String filename, boolean textModeRequired) {
        return new SshFxpOpen(id, filename, new FileOpenFlags(false, true, true, true, false, false, textModeRequired), FileAttributes.empty());
    }

    public SshFxpOpen(int id, String filename, FileOpenFlags openFlags, FileAttributes attributes) {
        this.id = id;

        assertNotNull(filename);
        this.filename = filename;

        assertNotNull(openFlags);
        this.openFlags = openFlags;

        assertNotNull(attributes);
        this.attributes = attributes;
    }

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

    @Override
    public String toString() {
        return "SshFxpOpen {"
                + "id=" + this.id
                + ", filename='" + this.filename + '\''
                + ", openFlags=" + this.openFlags
                + ", attributes=" + this.attributes
                + '}';
    }

    public interface Handler
            extends Message.HandlerBase {

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

    }
}
