package net.openhft.chronicle.wire;

import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.core.annotation.UsedViaReflection;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
import org.junit.Test;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Rob Austin.
 */
public class InnerMapTest {

    static class MyMarshable extends AbstractMarshallable {


        String name;

        Map<String, Double> commission;


        @UsedViaReflection
        public MyMarshable(@NotNull WireIn wire) {
            readMarshallable(wire);
        }

        public MyMarshable() {
            this.commission = new LinkedHashMap<>();
        }

        public String name() {
            return name;
        }

        public Map<String, Double> commission() {
            return commission;
        }

        public MyMarshable name(String name) {
            this.name = name;
            return this;
        }
    }


    @Test
    public void testMyInnnerMap() {
        MyMarshable myMarshable = new MyMarshable().name("rob");
        myMarshable.commission().put("hello", 123.4);

        String asString = myMarshable.toString();
        Assert.assertEquals("!net.openhft.chronicle.wire.InnerMapTest$MyMarshable {\n" +
                "  name: rob,\n" +
                "  commission: {\n" +
                "    hello: 123.4\n" +
                "  }\n" +
                "}\n", asString);


        Bytes b = Bytes.elasticByteBuffer();
        Wire w = new BinaryWire(b);     // works with text fails with binary

        try (DocumentContext dc = w.writingDocument(false)) {
            dc.wire().write("marshable").typedMarshallable(myMarshable);
        }


        try (DocumentContext dc = w.readingDocument()) {
            MyMarshable tm = dc.wire().read(() -> "marshable").typedMarshallable();
            Assert.assertEquals(asString, tm.toString());
        }

    }
}

@@ -346,633 +346,8 @@
  %7B%0A%0A
-    static class MyMarshable extends AbstractMarshallable %7B%0A%0A%0A        String name;%0A%0A        Map%3CString, Double%3E commission;%0A%0A%0A        @UsedViaReflection%0A        public MyMarshable(@NotNull WireIn wire) %7B%0A            readMarshallable(wire);%0A        %7D%0A%0A        public MyMarshable() %7B%0A            this.commission = new LinkedHashMap%3C%3E();%0A        %7D%0A%0A        public String name() %7B%0A            return name;%0A        %7D%0A%0A        public Map%3CString, Double%3E commission() %7B%0A            return commission;%0A        %7D%0A%0A        public MyMarshable name(String name) %7B%0A            this.name = name;%0A            return this;%0A        %7D%0A    %7D%0A%0A%0A
     
@@ -820,17 +820,16 @@
 ring);%0A%0A
-%0A
         
@@ -1089,17 +1089,16 @@
      %7D%0A%0A
-%0A
         
@@ -1304,11 +1304,657 @@
 %7D%0A%0A    %7D
+%0A%0A    static class MyMarshable extends AbstractMarshallable implements Demarshallable %7B%0A        String name;%0A        Map%3CString, Double%3E commission;%0A%0A        @UsedViaReflection%0A        public MyMarshable(@NotNull WireIn wire) %7B%0A            readMarshallable(wire);%0A        %7D%0A%0A        public MyMarshable() %7B%0A            this.commission = new LinkedHashMap%3C%3E();%0A        %7D%0A%0A        public String name() %7B%0A            return name;%0A        %7D%0A%0A        public Map%3CString, Double%3E commission() %7B%0A            return commission;%0A        %7D%0A%0A        public MyMarshable name(String name) %7B%0A            this.name = name;%0A            return this;%0A        %7D%0A    %7D
 %0A%7D%0A

FIx hanlding of nested collections and deprecate duplicate Map writing/reading methods.