/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.reflection;

import java.util.Arrays;
import java.util.List;
import javax.jws.WebParam;
import net.ranides.assira.collection.maps.OrderedMap;
import net.ranides.assira.collection.maps.RandomAccessMap;
import net.ranides.assira.generic.TypeToken;
import net.ranides.assira.reflection.mockup.ForIArguments;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;
import static net.ranides.assira.reflection.IAttribute.*;
import net.ranides.assira.reflection.mockup.ForMethodUtils;
import net.ranides.assira.reflection.util.AnnotationBuilder;
import net.ranides.assira.reflection.util.AnnotationUtils;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class IArgumentsTest {
    
    IClass<?> ic = IClass.typeinfo(ForIArguments.class);
    
    @Test
    public void testMethods() {
        IArguments a1 = IArguments.typeinfo(new TypeToken<List<String>>(){});
        IArguments a2 = IArguments.typeinfo(new TypeToken<List<Integer>>(){});
        IArguments a3 = IArguments.typeinfo(
            IClass.typeinfo(int.class), 
            IClass.typeinfo(int.class)
        );
        IArguments a4 = IArguments.typeinfo(float.class, float.class);
                
        OrderedMap<String, IClass<?>> args5 = new RandomAccessMap<>();
        args5.put("p4", IClass.typeinfo(char.class));
        args5.put("p2", IClass.typeinfo(boolean.class));
        IArguments a5 = IArguments.typeinfo(args5);
        
        OrderedMap<String, IClass<?>> args6 = new RandomAccessMap<>();
        args6.put("p4", IClass.typeinfo(boolean.class));
        args6.put("p2", IClass.typeinfo(char.class));
        IArguments a6 = IArguments.typeinfo(args6);
        
        
        
        assertEquals(Arrays.asList("fun6"), ic.methods().require(a1).names());
        assertEquals(Arrays.asList("fun5"), ic.methods().require(a2).names());
        assertEquals(Arrays.asList("fun1"), ic.methods().require(a3).names());
        assertEquals(Arrays.asList("fun2"), ic.methods().require(a4).names());
        assertEquals(Arrays.asList("fun7"), ic.methods().require(a5).names());
        assertEquals(Arrays.asList("fun8"), ic.methods().require(a6).names());
    }
    
    @Test
    public void testNames() {
        assertEquals(Arrays.asList("a","b"), ic.method("fun1").get().arguments().names());
        assertEquals(Arrays.asList("a","b"), ic.method("fun2").get().arguments().names());
        assertEquals(Arrays.asList("ia","ib"), ic.method("fun3").get().arguments().names());
        assertEquals(Arrays.asList("map"), ic.method("fun4").get().arguments().names());
        
        assertEquals(Arrays.asList(IClass.OBJECT), IClass.OBJECT.method("equals").get().arguments().types().list());
        assertThrows(UnsupportedOperationException.class, ()->{
            IClass.OBJECT.method("equals").get().arguments().names();
        });
    }
    
    @Test
    public void testParent() {
        IMethod m1 = ic.method("fun1").get();
        IMethod m2 = IClass.typeinfo(ForMethodUtils.A.class).method("fun3").get();
        IMethod m3 = IClass.typeinfo(ForMethodUtils.B.class).method("fun3").get();
        
        assertEquals(Arrays.asList(m1,m1), m1.arguments().parent().list());
        assertEquals(Arrays.asList(m2,m2), m2.arguments().parent().list());
        assertEquals(Arrays.asList(m3,m3), m3.arguments().parent().list());
        
        assertEquals(ic, m1.arguments().parent().parent().first().get());
        assertEquals(IClass.typeinfo(ForMethodUtils.A.class), m2.arguments().parent().parent().first().get());
        assertEquals(IClass.typeinfo(ForMethodUtils.Root.class), m3.arguments().parent().parent().first().get());
        
    }
    
    @Test
    public void testMap() {
        IMethod m1 = ic.method("fun1").get();
        IMethod m2 = IClass.typeinfo(ForMethodUtils.A.class).method("fun3").get();
        IMethod m3 = IClass.typeinfo(ForMethodUtils.B.class).method("fun3").get();
        IMethod m4 = ic.method("fun4").get();
        
        assertEquals("{a=int a, b=int b}", m1.arguments().map().toString());
        assertEquals("{x=int x, y=char y}", m2.arguments().map().toString());
        assertEquals("{x=int x, y=int y}", m3.arguments().map().toString());
        assertEquals("{map=java.util.Map<java.lang.Integer, java.lang.String> map}", m4.arguments().map().toString());
        
        assertEquals("[int a, int b]", m1.arguments().reflective().list().toString());
        assertEquals("[int x, char y]", m2.arguments().reflective().list().toString());
        assertEquals("[int x, int y]", m3.arguments().reflective().list().toString());
        assertEquals("[java.util.Map<java.lang.Integer, java.lang.String> map]", m4.arguments().reflective().list().toString());
    }
    
    @Test
    public void testAttributes() {
        List<IArgument> params = ic.method("fun9").get().arguments().list();
        
        assertEquals(collect(PACKAGE, DECLARED), params.get(0).attributes());
        assertEquals(collect(PACKAGE, DECLARED, PARAMETERIZED), params.get(1).attributes());
    }
    
    @Test
    public void testAnnotations() {
        List<IArgument> params = ic.method("fun9").get().arguments().list();
        Deprecated a1 = AnnotationUtils.make(Deprecated.class);
        WebParam a2 = new AnnotationBuilder<>(WebParam.class)
                .param("name", "yparam")
                .result();
        assertEquals(Arrays.asList(a1), params.get(0).annotations().list());
        assertEquals(Arrays.asList(a2), params.get(1).annotations().list());
        
        IArguments cparams = IArguments.typeinfo(IClass.typeinfo(boolean.class), new TypeToken<List<String>>(){});
        
        assertThrows(UnsupportedOperationException.class, ()->{
            cparams.names();
        });
        
        assertSymEquals(cparams.list(), params);
        assertSymEquals(cparams.list().get(0), params.get(0));
        assertSymEquals(cparams.list().get(1), params.get(1));
    }
    
    @Test
    public void testCParams() {
        IArguments cparams1 = IArguments.typeinfo(
            IClass.typeinfo(boolean.class), 
            new TypeToken<List<String>>(){}
        );
        
        assertThrows(UnsupportedOperationException.class, ()->{
            cparams1.names();
        });
        
        RandomAccessMap<String, IClass<?>> iparams2 = new RandomAccessMap<>();
        iparams2.put("name", IClass.typeinfo(boolean.class));
        iparams2.put("list", new TypeToken<List<String>>(){});
        
        IArguments cparams2 = IArguments.typeinfo(iparams2);
        assertEquals(Arrays.asList("name","list"), cparams2.names());
        
        assertEquals(collect(PACKAGE, DECLARED), cparams2.list().get(0).attributes());
        assertEquals(collect(PACKAGE, DECLARED, PARAMETERIZED), cparams2.list().get(1).attributes());
        
        assertEquals(IAnnotations.of(), cparams2.list().get(0).annotations());
        assertEquals(IAnnotations.of(), cparams2.list().get(1).annotations());
        
        assertEquals(IContext.DEFAULT, cparams2.list().get(0).context());
        assertEquals(IContext.DEFAULT, cparams2.list().get(1).context());

        assertThrows(UnsupportedOperationException.class, ()->{
            cparams2.parent().list();
        });
        assertThrows(UnsupportedOperationException.class, ()->{
            cparams2.reflective().list();
        });
        
    }
    
    
}
