package uk.hfox.morphix.mapper.lifecycle;

import org.junit.jupiter.api.Test;
import uk.hfox.morphix.annotations.lifecycle.field.CreatedAt;
import uk.hfox.morphix.exception.connection.InvalidConfigurationException;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static uk.hfox.morphix.mapper.lifecycle.LifecycleAction.BEFORE_CREATE;
import static uk.hfox.morphix.mapper.lifecycle.LifecycleAction.CREATED_AT;

class LifecycleActionTest {

    @Test
    void isFieldSupported() throws Exception {
        Class<LifecycleTestClass> testClass = LifecycleTestClass.class;
        assertFalse(BEFORE_CREATE.isSupported(testClass.getDeclaredField("invalidStatic")));
        assertFalse(CREATED_AT.isSupported(testClass.getDeclaredField("invalidNotAnnotated")));
        assertThrows(InvalidConfigurationException.class, () -> CREATED_AT.isSupported(testClass.getDeclaredField("invalidStatic")));
        assertFalse(CREATED_AT.isSupported(testClass.getDeclaredField("invalidType")));
        assertTrue(CREATED_AT.isSupported(testClass.getDeclaredField("createdAt")));

        Map<LifecycleAction, List<Field>> populate = new HashMap<>();
        LifecycleAction.populateFields(CreatedAtTest.class, populate);

        List<Field> fields = populate.get(CREATED_AT);
        assertEquals(1, fields.size());
        assertEquals(CreatedAtTest.class.getDeclaredField("createdAt"), fields.get(0));

        populate = new HashMap<>();
        LifecycleAction.populateFields(CreatedAtInheritedTest.class, populate);

        fields = populate.get(CREATED_AT);
        assertEquals(1, fields.size());
        assertEquals(CreatedAtTest.class.getDeclaredField("createdAt"), fields.get(0));
    }

    @Test
    void isMethodSupported() {

    }

    @Test
    void populate() {

    }

    public static class LifecycleTestClass {

        @CreatedAt
        private static String invalidStatic;

        private String invalidNotAnnotated;

        @CreatedAt
        private String invalidType;

        @CreatedAt
        private LocalDateTime createdAt;

        public static void invalidStatic() {
        }

    }

    public static class CreatedAtTest {

        @CreatedAt
        private LocalDateTime createdAt;

    }

    public static class CreatedAtInheritedTest extends CreatedAtTest {
    }

}
@@ -133,16 +133,82 @@
 atedAt;%0A
+import uk.hfox.morphix.annotations.lifecycle.method.BeforeCreate;%0A
 import u
@@ -307,16 +307,49 @@
 .Field;%0A
+import java.lang.reflect.Method;%0A
 import j
@@ -1963,18 +1963,1166 @@
 orted() 
-%7B%0A
+throws Exception %7B%0A        Class%3CLifecycleTestClass%3E testClass = LifecycleTestClass.class;%0A        assertFalse(CREATED_AT.isSupported(testClass.getDeclaredMethod(%22invalidStatic%22)));%0A        assertFalse(BEFORE_CREATE.isSupported(testClass.getDeclaredMethod(%22invalidNotAnnotated%22)));%0A        assertThrows(InvalidConfigurationException.class, () -%3E BEFORE_CREATE.isSupported(testClass.getDeclaredMethod(%22invalidStatic%22)));%0A        assertTrue(BEFORE_CREATE.isSupported(testClass.getDeclaredMethod(%22beforeCreate%22)));%0A%0A        Map%3CLifecycleAction, List%3CMethod%3E%3E populate = new HashMap%3C%3E();%0A        LifecycleAction.populateMethods(CreatedAtTest.class, populate);%0A%0A        List%3CMethod%3E methods = populate.get(BEFORE_CREATE);%0A        assertEquals(1, methods.size());%0A        assertEquals(CreatedAtTest.class.getDeclaredMethod(%22beforeCreate%22), methods.get(0));%0A%0A        populate = new HashMap%3C%3E();%0A        LifecycleAction.populateMethods(CreatedAtInheritedTest.class, populate);%0A%0A        methods = populate.get(BEFORE_CREATE);%0A        assertEquals(1, methods.size());%0A        assertEquals(CreatedAtTest.class.getDeclaredMethod(%22beforeCreate%22), methods.get(0));
 %0A    %7D%0A%0A
@@ -3430,24 +3430,46 @@
 createdAt;%0A%0A
+        @BeforeCreate%0A
         publ
@@ -3508,24 +3508,149 @@
 %0A        %7D%0A%0A
+        public void invalidNotAnnotated() %7B%0A        %7D%0A%0A        @BeforeCreate%0A        public void beforeCreate() %7B%0A        %7D%0A%0A
     %7D%0A%0A    p
@@ -3742,24 +3742,94 @@
 createdAt;%0A%0A
+        @BeforeCreate%0A        public void beforeCreate() %7B%0A        %7D%0A%0A
     %7D%0A%0A    p

Add test for isSupported(Method)