// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package nosql.mongodb.jnosql;

import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import jakarta.nosql.TypeReference;
import jakarta.nosql.document.DocumentEntity;

//++++
//
//    I think there should be an easy way to convert between DocumentEntity and a custom class but I cannot find it,
//    so I came up with this hack.
//
//++++
public class Util {
    public static <T> void autoMapFrom(T o, DocumentEntity de) {
        try {
            for(PropertyDescriptor pd : Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors()) {
                if(pd.getName().equals("class")) {
                    // ignore Object getClass
                } else {
                    de.add(pd.getName(), pd.getReadMethod().invoke(o));
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T, U> void autoMapTo(DocumentEntity de, T o, TypeReference<List<U>> tr) {
        try {
            for(PropertyDescriptor pd : Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors()) {
                if(pd.getName().equals("class")) {
                    // ignore Object getClass
                } else if(pd.getPropertyType().isPrimitive()
                           || pd.getPropertyType().equals(BigInteger.class)
                           || pd.getPropertyType().equals(BigDecimal.class)
                           || pd.getPropertyType().equals(String.class)
                           || !pd.getPropertyType().getName().startsWith("java")) {
                    if(pd.getWriteMethod() != null) {
                        pd.getWriteMethod().invoke(o, de.find(pd.getName()).get().get(pd.getPropertyType()));
                    }
                } else if(pd.getPropertyType().equals(List.class)) {
                    List from = de.find(pd.getName()).get().get(tr);
                    List to = (List)pd.getReadMethod().invoke(o);
                    for(Object so : from) {
                        to.add(so);
                    }
                } else {
                    throw new RuntimeException("Unsupported type " + pd.getPropertyType() + " for property " + pd.getName());
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
