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

import com.jscape.inet.ssh.protocol.messages.Message;
import com.jscape.inet.ssh.protocol.v2.messages.UnknownMessage;
import com.jscape.util.IOTools;
import com.jscape.util.SuppressObfuscation;
import com.jscape.util.io.DataCodec;
import com.jscape.util.io.primitive.ByteCodec;

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
 */
@SuppressObfuscation
public class MessageCodec
        implements DataCodec<Message> {

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

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

        set(entries);
    }

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

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

        return this;
    }

    @Override
    public Message read(InputStream in)
            throws IOException {
        int messageCode = ByteCodec.readValue(in) & 0xFF;
    
        Entry entry = entryFor(messageCode);
        
        if (entry == null) {
            byte[] data = IOTools.readFully(in);
            return new UnknownMessage(messageCode, data);
        }
    
        return entry.codec.read(in);
    }

    @Override
    public void write(Message value, OutputStream out)
            throws IOException {
        Entry entry = entryFor(value);
        ByteCodec.writeValue((byte) entry.messageCode, out);
        entry.codec.write(value, out);
    }

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

    private Entry entryFor(int type) {
        return this.readingEntries.get(type);
    }

    private Entry entryFor(Message message) {
        Entry entry = this.writingEntries.get(message.getClass());
    
        if (entry == null) {
            throw new IllegalArgumentException(String.format("Unsupported message: %s.", message.getClass()));
        }

        return entry;
    }

    @SuppressObfuscation
    public static class Entry {

        public final int messageCode;
        public final DataCodec<Message> codec;
        public final Class[] messageClasses;

        public Entry(int messageCode, DataCodec<Message> codec, Class... messageClasses) {
            this.messageCode = messageCode;

            assertNotNull(messageClasses);
            this.messageClasses = messageClasses;

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

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

    @SuppressObfuscation
    public interface Factory {

        MessageCodec update(MessageCodec codec);

    }

}
