/* Generated by camel build tools - do NOT edit this file! */
package org.apache.camel.groovy.json;

import javax.annotation.processing.Generated;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.DeferredContextBinding;
import org.apache.camel.Exchange;
import org.apache.camel.Ordered;
import org.apache.camel.TypeConversionException;
import org.apache.camel.TypeConverterLoaderException;
import org.apache.camel.TypeConverter;
import org.apache.camel.spi.TypeConvertible;
import org.apache.camel.spi.BulkTypeConverters;
import org.apache.camel.spi.TypeConverterLoader;
import org.apache.camel.spi.TypeConverterRegistry;

/**
 * Generated by camel build tools - do NOT edit this file!
 */
@Generated("org.apache.camel.maven.packaging.TypeConverterLoaderGeneratorMojo")
@SuppressWarnings("unchecked")
@DeferredContextBinding
public final class GroovyJsonBulkConverterLoader implements TypeConverterLoader, BulkTypeConverters, CamelContextAware {

    private CamelContext camelContext;

    public GroovyJsonBulkConverterLoader() {
    }

    @Override
    public void setCamelContext(CamelContext camelContext) {
        this.camelContext = camelContext;
    }

    @Override
    public CamelContext getCamelContext() {
        return camelContext;
    }

    @Override
    public int size() {
        return 6;
    }

    @Override
    public void load(TypeConverterRegistry registry) throws TypeConverterLoaderException {
        registry.addBulkTypeConverters(this);
        doRegistration(registry);
    }

    @Override
    public <T> T convertTo(Class<?> from, Class<T> to, Exchange exchange, Object value) throws TypeConversionException {
        try {
            Object obj = doConvertTo(from, to, exchange, value);
            return (T) obj;
        } catch (TypeConversionException e) {
            throw e;
        } catch (Exception e) {
            throw new TypeConversionException(value, to, e);
        }
    }

    private Object doConvertTo(Class<?> from, Class<?> to, Exchange exchange, Object value) throws Exception {
        if (to == org.apache.camel.util.json.JsonArray.class) {
            if (value instanceof java.util.List) {
                return org.apache.camel.groovy.json.GroovyJsonConverter.convertToJsonArray((java.util.List) value, exchange);
            }
        } else if (to == org.apache.camel.util.json.JsonObject.class) {
            if (value instanceof groovy.util.Node) {
                return org.apache.camel.groovy.json.GroovyJsonConverter.convertToJsonObject((groovy.util.Node) value, exchange);
            }
            if (value instanceof org.apache.groovy.json.internal.LazyMap) {
                return org.apache.camel.groovy.json.GroovyJsonConverter.convertToJsonObject((org.apache.groovy.json.internal.LazyMap) value, exchange);
            }
        } else if (to == org.apache.camel.util.json.Jsonable.class) {
            if (value instanceof groovy.util.Node) {
                return org.apache.camel.groovy.json.GroovyJsonConverter.convertToJson((groovy.util.Node) value, exchange);
            }
            if (value instanceof org.apache.groovy.json.internal.LazyMap) {
                return org.apache.camel.groovy.json.GroovyJsonConverter.convertToJson((org.apache.groovy.json.internal.LazyMap) value, exchange);
            }
            if (value instanceof java.util.List) {
                return org.apache.camel.groovy.json.GroovyJsonConverter.convertToJson((java.util.List) value, exchange);
            }
        }
        return null;
    }

    private void doRegistration(TypeConverterRegistry registry) {
        registry.addConverter(new TypeConvertible<>(java.util.List.class, org.apache.camel.util.json.JsonArray.class), this);
        registry.addConverter(new TypeConvertible<>(groovy.util.Node.class, org.apache.camel.util.json.JsonObject.class), this);
        registry.addConverter(new TypeConvertible<>(org.apache.groovy.json.internal.LazyMap.class, org.apache.camel.util.json.JsonObject.class), this);
        registry.addConverter(new TypeConvertible<>(groovy.util.Node.class, org.apache.camel.util.json.Jsonable.class), this);
        registry.addConverter(new TypeConvertible<>(org.apache.groovy.json.internal.LazyMap.class, org.apache.camel.util.json.Jsonable.class), this);
        registry.addConverter(new TypeConvertible<>(java.util.List.class, org.apache.camel.util.json.Jsonable.class), this);
    }

    public TypeConverter lookup(Class<?> to, Class<?> from) {
        if (to == org.apache.camel.util.json.JsonArray.class) {
            if (from == java.util.List.class) {
                return this;
            }
        } else if (to == org.apache.camel.util.json.JsonObject.class) {
            if (from == groovy.util.Node.class) {
                return this;
            }
            if (from == org.apache.groovy.json.internal.LazyMap.class) {
                return this;
            }
        } else if (to == org.apache.camel.util.json.Jsonable.class) {
            if (from == groovy.util.Node.class) {
                return this;
            }
            if (from == org.apache.groovy.json.internal.LazyMap.class) {
                return this;
            }
            if (from == java.util.List.class) {
                return this;
            }
        }
        return null;
    }

}
