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

import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.Map;
import net.ranides.assira.collection.maps.ArrayMap;
import net.ranides.assira.collection.maps.HashMap;
import net.ranides.assira.collection.maps.MapUtils;
import net.ranides.assira.reflection.IClass;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class GContext extends AContext {
    
    private final Map<String, IClass<?>> params;
    
    GContext(Class<?> type, IClass<?>[] params) {
        this.params = new ArrayMap<>(names(type.getTypeParameters()), params);
    }
    
    private static String[] names(TypeVariable<?>[] types) {
        return Arrays.stream(types).map(a -> a.getName()).toArray(String[]::new);
    }

    @Override
    public IClass<?> resolveTV(TypeVariable type) {
        IClass<?> ic = params.get(type.getName());
        return ic!=null ? ic : super.typeinfo(type);
    }

    @Override
    public Map<String, Type> map() {
        HashMap<String, Type> map = new HashMap<>();
        map.putAll(MapUtils.map(params, c -> c.reflective()));
        map.putAll(super.map());
        return map;
    }
    
}
