package com.github.averyregier.club.domain.club.adapter;

import com.github.averyregier.club.domain.club.InputField;
import com.github.averyregier.club.domain.club.InputFieldDesignator;
import com.github.averyregier.club.domain.club.InputFieldGroup;
import org.junit.Test;

import java.util.List;

import static org.junit.Assert.*;

public class InputFieldAdapterGroupTest {

    @Test
    public void id() throws Exception {
        InputFieldGroup classUnderTest = new InputFieldGroupBuilder()
                .id("an id")
                .name("descriptor")
                .build();

        assertEquals("an id", classUnderTest.getId());
        assertEquals("an id", classUnderTest.getShortCode());
        assertEquals("descriptor", classUnderTest.getName());

    }

    @Test
    public void name() throws Exception {
        InputFieldGroup classUnderTest = new InputFieldGroupBuilder()
                .name("descriptor")
                .build();

        assertEquals("descriptor", classUnderTest.getName());
    }

    @Test
    public void noFields() throws Exception {
        InputFieldGroup classUnderTest = new InputFieldGroupBuilder()
                .build();

        assertTrue(classUnderTest.getFields().isEmpty());
    }

    @Test
    public void parent() {
        InputFieldGroup classUnderTest = new InputFieldGroupBuilder()
                .field(f -> f)
                .build();

        InputField field = classUnderTest.getFields().get(0);
        assertNotNull(field);
        assertEquals(classUnderTest, field.getContainer());
    }

    @Test
    public void parentID() {
        InputFieldGroup classUnderTest = new InputFieldGroupBuilder()
                .id("foo")
                .field(f -> f.id("bar"))
                .build();

        InputField field = classUnderTest.getFields().get(0);
        assertEquals("bar", field.getShortCode());
        assertEquals("foo:bar", field.getId());
        assertNull(classUnderTest.getContainer());
    }

    @Test
    public void grandparent() {
        InputFieldGroup classUnderTest = new InputFieldGroupBuilder()
                .id("foo")
                .group(g -> g
                                .id("baz")
                                .field(f -> f.id("bar"))
                )
                .build();

        assertTrue(classUnderTest.getFields().isEmpty());
        assertFalse(classUnderTest.getGroups().isEmpty());
        InputFieldGroup group = classUnderTest.getGroups().get(0);
        assertEquals("baz", group.getShortCode());
        assertEquals(classUnderTest, group.getContainer());
        assertEquals("foo:baz", group.getId());
        assertEquals("foo:baz:bar", group.getFields().get(0).getId());
        assertNull(classUnderTest.getContainer());
    }

    @Test
    public void fieldOrder() {
        InputFieldGroup classUnderTest = new InputFieldGroupBuilder()
                .id("foo")
                .field(f -> f.id("name"))
                .group(g -> g
                                .id("address")
                                .field(f -> f.id("state"))
                )
                .field(f -> f.id("alergies"))
                .build();

        List<InputFieldDesignator> designators = classUnderTest.getFieldDesignations();
        InputFieldDesignator name = designators.get(0);
        assertEquals("foo:name", name.getId());
        assertEquals(classUnderTest, name.getContainer());
        assertFalse(name.asGroup().isPresent());
        assertTrue(name.asField().isPresent());
        assertEquals(name, name.asField().get());

        InputFieldDesignator address = designators.get(1);
        assertEquals("foo:address", address.getId());
        assertEquals(classUnderTest, address.getContainer());
        assertTrue(address.asGroup().isPresent());
        assertFalse(address.asField().isPresent());
        assertEquals(address, address.asGroup().get());

        InputFieldDesignator allergies = designators.get(2);
        assertEquals("foo:alergies", allergies.getId());
        assertEquals(classUnderTest, allergies.getContainer());
        assertFalse(allergies.asGroup().isPresent());
        assertTrue(allergies.asField().isPresent());
        assertEquals(allergies, allergies.asField().get());

    }
}
@@ -3143,16 +3143,17 @@
 f.id(%22al
+l
 ergies%22)
@@ -4004,16 +4004,17 @@
 (%22foo:al
+l
 ergies%22,
@@ -4261,20 +4261,19 @@
 d().get());%0A
-%0A
     %7D%0A%7D

correct spelling