/*
 * Copyright 2017-2021 Dromara.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.dromara.hmily.config.loader.bind;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import lombok.Data;

/**
 * The type Data type.
 *
 * @author xiaoyu
 */
@Data
public final class DataType {

    private Type type;

    private DataType[] generics = new DataType[0];

    private Class<?> typeClass;
    
    /**
     * Instantiates a new Data type.
     *
     * @param type the type
     */
    private DataType(final Type type) {
        this.type = type;
        this.typeClass = (Class<?>) type;
    }

    /**
     * Is assignable from boolean.
     *
     * @param clazz the clazz
     * @return the boolean
     */
    private boolean isAssignableFrom(final Class<?> clazz) {
        return clazz.isAssignableFrom(typeClass);
    }
    
    /**
     * Is map boolean.
     *
     * @return the boolean
     */
    boolean isMap() {
        return isAssignableFrom(Map.class);
    }
    
    /**
     * Is collection boolean.
     *
     * @return the boolean
     */
    boolean isCollection() {
        return isAssignableFrom(Collection.class);
    }
    
    /**
     * Is array boolean.
     *
     * @return the boolean
     */
    boolean isArray() {
        return getTypeClass().isArray();
    }
    
    /**
     * 设置泛型类型.
     *
     * @param field the field
     * @return data type
     */
    DataType withGenerics(final Field field) {
        if (field != null) {
            Type genericType = field.getGenericType();
            if (genericType instanceof ParameterizedType) {
                ParameterizedType pt = (ParameterizedType) genericType;
                Type[] actualTypeArguments = pt.getActualTypeArguments();
                generics = Arrays.stream(actualTypeArguments).map(DataType::of).toArray(DataType[]::new);
            }
        }
        return this;
    }
    
    /**
     * Gets component type.
     *
     * @return the component type
     */
    Class<?> getComponentType() {
        return getTypeClass().getComponentType();
    }
    
    /**
     * Of data type.
     *
     * @param type the type
     * @return the data type
     */
    public static DataType of(final Type type) {
        return new DataType(type);
    }
}
