package com.jscape.inet.sftp.protocol.v3.marshaling;

import com.jscape.inet.sftp.protocol.messages.Message;
import com.jscape.inet.sftp.protocol.v3.messages.SshFxpExtended;
import com.jscape.inet.sftp.protocol.v3.messages.SshFxpExtendedUnknown;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.StringCodec;
import com.jscape.inet.ssh.protocol.v2.marshaling.primitive.Uint32Codec;
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.HashMap;
import java.util.Map;

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

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

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

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

    public SshFxpExtendedCodec set(Entry... entries) {
        for (Entry entry : entries) {
            this.readingEntries.put(entry.type, entry);
            this.writingEntries.put(entry.messageClass, entry);
        }

        return this;
    }

    @Override
    public Message read(InputStream in)
            throws IOException {
        int id = Uint32Codec.readValue(in);
        String type = StringCodec.readUsAsciiValue(in);

        try {
            Entry entry = entryFor(type);

            return entry.codec.read(in, id);
        } catch (IllegalArgumentException e) {
            byte[] data = IOTools.readFully(in);
            return new SshFxpExtendedUnknown(id, type, data);
        }
    }

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

        Entry entry = entryFor(message);

        Uint32Codec.writeValue(message.id, out);
        StringCodec.writeUsAsciiValue(entry.type, out);
        entry.codec.write(message, out);
    }

    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(SshFxpExtended message) {
        Entry entry = this.writingEntries.get(message.getClass());

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

        return entry;
    }

    public interface TypeCodec {

        SshFxpExtended read(InputStream in, int id)
                throws IOException;

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

    }

    public static class Entry {

        public final String type;
        public final TypeCodec codec;
        public final Class messageClass;

        public Entry(String type, TypeCodec codec, Class messageClass) {
            assertNotNull(type);
            this.type = type;

            assertNotNull(codec);
            this.codec = codec;

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

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