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

import com.jscape.inet.ssh.protocol.messages.Message;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequest;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgUserAuthRequestUnknown;
import com.jscape.util.IOTools;
import com.jscape.util.io.DataCodec;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

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

/**
 * @author Alex Usun
 */
public class SshMsgUserAuthRequestCodec
        implements DataCodec<Message> {

    private final Map<String, Entry> readingEntries;
    private final Map<Class, Entry> writingEntries;

    public SshMsgUserAuthRequestCodec(Entry... entries) {
        this.readingEntries = new HashMap<>();
        this.writingEntries = new HashMap<>();
        set(entries);
    }

    public SshMsgUserAuthRequestCodec set(Entry... entries) {
        for (Entry entry : entries) {
            this.readingEntries.put(entry.method, entry);

            for (Class messageClass : entry.messageClasses) {
                this.writingEntries.put(messageClass, entry);
            }
        }

        return this;
    }

    @Override
    public Message read(InputStream in)
            throws IOException {
        String username = StringCodec.readUtf8Value(in);
        String serviceName = StringCodec.readUsAsciiValue(in);
        String method = StringCodec.readUsAsciiValue(in);

        try {
            Entry entry = entryFor(method);

            return entry.codec.read(in, username, serviceName);
        } catch (Exception e) {
            byte[] data = IOTools.readFully(in);
            return new SshMsgUserAuthRequestUnknown(username, serviceName, method, data);
        }
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        SshMsgUserAuthRequest message = (SshMsgUserAuthRequest) value;

        Entry entry = entryFor(message);

        StringCodec.writeUtf8Value(message.username, out);
        StringCodec.writeUsAsciiValue(message.serviceName, out);
        StringCodec.writeUsAsciiValue(entry.method, out);
        entry.codec.write(message, out);
    }

    @Override
    public String toString() {
        return "SshMsgUserAuthRequestCodec {"
                + "readingEntries=" + this.readingEntries
                + ", writingEntries=" + this.writingEntries
                + '}';
    }

    private Entry entryFor(String method) {
        Entry entry = this.readingEntries.get(method);

        if (entry == null) {
            throw new IllegalArgumentException("Unsupported method: " + method);
        }

        return entry;
    }

    private Entry entryFor(SshMsgUserAuthRequest message) {
        Entry entry = this.writingEntries.get(message.getClass());

        if (entry == null) {
            throw new IllegalArgumentException("Unsupported message: " + message.getClass());
        }

        return entry;
    }

    public interface MethodCodec {

        SshMsgUserAuthRequest read(InputStream in, String username, String serviceName)
                throws IOException;

        void write(SshMsgUserAuthRequest value, OutputStream out)
                throws IOException;

    }

    public static class Entry {

        public final String method;
        public final MethodCodec codec;
        public final Class[] messageClasses;

        public Entry(String method, MethodCodec codec, Class... messageClasses) {
            assertNotNull(method);
            this.method = method;

            assertNotNull(codec);
            this.codec = codec;

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

        @Override
        public String toString() {
            return "Entry {"
                    + "method='" + this.method + '\''
                    + ", codec=" + this.codec
                    + ", messageClasses=" + Arrays.toString(this.messageClasses)
                    + '}';
        }
    }
}
