/*
 * Copyright 2021 Tianmian Tech. All Rights Reserved.
 *
 * 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 com.welab.wefe.common.fieldvalidate;


import com.welab.wefe.common.exception.StatusCodeWithException;
import com.welab.wefe.common.util.ClassUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author Zane
 */
public abstract class AbstractCheckModel {
    protected final Logger LOG = LoggerFactory.getLogger(this.getClass());

    public void checkAndStandardize() throws StatusCodeWithException {
        try {
            FieldValidateUtil.checkAndStandardize(this);

            for (Field field : ClassUtils.listFields(this.getClass())) {
                Class<?> type = field.getType();
                Type fieldGenericType = field.getGenericType();
                field.setAccessible(true);

                if (AbstractCheckModel.class.isAssignableFrom(type)) {
                    Object value = field.get(this);
                    if (value != null) {
                        ((AbstractCheckModel) value).checkAndStandardize();
                    }
                } else if ("List".equals(type.getSimpleName())) {
                    if (ParameterizedType.class.isAssignableFrom(fieldGenericType.getClass())) {
                        Type actualTypeArgument = ((ParameterizedType) fieldGenericType).getActualTypeArguments()[0];
                        if (actualTypeArgument instanceof Class) {
                            type = (Class<?>) actualTypeArgument;
                            if (AbstractCheckModel.class.isAssignableFrom(type)) {
                                Object list = field.get(this);
                                if (list != null) {
                                    for (Object item : (List) list) {
                                        ((AbstractCheckModel) item).checkAndStandardize();
                                    }
                                }
                            }
                        }
                    }
                }
            }
        } catch (IllegalAccessException e) {
            LOG.error(e.getClass().getSimpleName() + " " + e.getMessage(), e);
        }
    }

    public void standardize() {
        try {
            FieldValidateUtil.standardize(this);
        } catch (IllegalAccessException e) {
            LOG.error(e.getClass().getSimpleName() + " " + e.getMessage(), e);
        }
    }
}
