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.SshMsgGlobalRequest;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgGlobalRequestUnknown;
import com.jscape.util.IOTools;
import com.jscape.util.io.DataCodec;
import com.jscape.util.io.primitive.BooleanCodec;

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 SshMsgGlobalRequestCodec
        implements DataCodec<Message> {

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

    public SshMsgGlobalRequestCodec(Entry... entries) {
        this.readingEntries = new HashMap<String, Entry>();
        this.writingEntries = new HashMap<Class, Entry>();
        set(entries);
    }

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

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

        return this;
    }

    @Override
    public Message read(InputStream in)
            throws IOException {
        String requestName = StringCodec.readUsAsciiValue(in);
        boolean wantReply = BooleanCodec.readValue(in);

        try {
            Entry entry = entryFor(requestName);

            return entry.codec.read(in, wantReply);
        } catch (Exception e) {
            byte[] data = IOTools.readFully(in);
            return new SshMsgGlobalRequestUnknown(requestName, wantReply, data);
        }
    }

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

        Entry entry = entryFor(message);

        StringCodec.writeUsAsciiValue(entry.requestName, out);
        BooleanCodec.writeValue(message.wantReply, out);
        entry.codec.write(message, out);
    }

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

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

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

        return entry;
    }

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

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

        return entry;
    }

    public interface RequestCodec {

        SshMsgGlobalRequest read(InputStream in, boolean wantReply)
                throws IOException;

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

    }

    public static class Entry {

        public final String requestName;
        public final RequestCodec codec;
        public final Class[] messageClasses;

        public Entry(String requestName, RequestCodec codec, Class... messageClasses) {
            assertNotNull(requestName);
            this.requestName = requestName;

            assertNotNull(codec);
            this.codec = codec;

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

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