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

import com.jscape.util.net.connection.MessageConnection;

import static com.jscape.util.Parameters.assertFalse;
import static com.jscape.util.Parameters.assertTrue;

/**
 * The SSH identification string.
 *
 * @author Alex Usun
 */
public class IdentificationString
        extends Message<IdentificationString.Handler> {

    /**
     * The optional banner.
     */
    public final String banner;

    /**
     * The protocol version.
     */
    public final String protocolVersion;

    /**
     * The software version.
     */
    public final String softwareVersion;

    /**
     * The optional comments.
     */
    public final String comments;
    
    public byte[] rawData;

    /**
     * Creates a server identification string for protocol version 2.0.
     *
     * @param banner          an optional banner
     * @param softwareVersion a software version
     * @param comments        optional comments
     *
     * @return a new identification string
     */
    public static IdentificationString serverVersion20(String banner, String softwareVersion, String comments) {
        return new IdentificationString(banner, "2.0", softwareVersion, comments);
    }

    /**
     * Creates a client identification string for protocol version 2.0.
     *
     * @param softwareVersion a software version
     * @param comments        optional comments
     *
     * @return a new identification string
     */
    public static IdentificationString clientVersion20(String softwareVersion, String comments) {
        return new IdentificationString(null, "2.0", softwareVersion, comments);
    }

    /**
     * Creates a new identification string instance.
     *
     * @param banner          an optional banner
     * @param protocolVersion a protocol version
     * @param softwareVersion a software version
     * @param comments        optional comments
     */
    public IdentificationString(String banner, String protocolVersion, String softwareVersion, String comments) {
        assertTrue(banner == null || (!banner.startsWith("SSH-") && !banner.contains("\rSSH-") && !banner.contains("\nSSH-")), "Invalid banner.");
        this.banner = banner;

        assertFalse(protocolVersion.contains(" ") || protocolVersion.contains("-"), "Invalid protocol version.");
        this.protocolVersion = protocolVersion;

        assertFalse(softwareVersion.contains(" "), "Invalid software version.");
        this.softwareVersion = softwareVersion;

        this.comments = comments;
    }

    @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 "IdentificationString {"
                + "banner='" + this.banner + '\''
                + ", protocolVersion='" + this.protocolVersion + '\''
                + ", softwareVersion='" + this.softwareVersion + '\''
                + ", comments='" + this.comments + '\''
                + '}';
    }

    public interface Handler
            extends Message.HandlerBase {

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

    }
}
