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.marshaling.primitive.Uint32Codec;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgChannelRequest;
import com.jscape.inet.ssh.protocol.v2.messages.SshMsgChannelRequestUnknown;
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 SshMsgChannelRequestCodec
        implements DataCodec<Message> {

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

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

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

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

        return this;
    }

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

        try {
            Entry entry = entryFor(requestType);

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

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

        Entry entry = entryFor(message);

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

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

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

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

        return entry;
    }

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

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

        return entry;
    }

    public interface RequestCodec {

        SshMsgChannelRequest read(InputStream in, int recipientChannel, boolean wantReply)
                throws IOException;

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

    }

    public static class Entry {

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

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

            assertNotNull(codec);
            this.codec = codec;

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

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