package com.brframework.commoncms.core.layout;

import com.brframework.commoncms.annatotion.Column;
import com.google.common.base.Strings;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;

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

/**
 * 布局列的抽象实现
 *
 * @author xu
 * @date 2018/10/17 10:44
 */
public abstract class AbstractPanelLayoutColumn implements PanelLayoutColumn {

    private boolean selectShow = true;

    /**
     * 默认使用的初始化
     *
     * @param field
     */
    protected void init(Field field) {
        ApiModelProperty apiModelProperty =
                AnnotationUtils.findAnnotation(field, ApiModelProperty.class);

        String name = apiModelProperty.name();
        String value = apiModelProperty.value();

        if (Strings.isNullOrEmpty(name)) {
            name = field.getName();
        }

        //设置数据
        setKey(name);
        setTitle(value);

        Column column =
                AnnotatedElementUtils.findMergedAnnotation(field, Column.class);
        if(column != null){
            //设置是否是默认的列表展示项
            setSelectShow(column.selectShow());
        }

    }

    @Override
    public boolean isSelectShow(){
        return selectShow;
    }

    @Override
    public void setSelectShow(boolean selectShow){
        this.selectShow = selectShow;
    }


    @Override
    public boolean isSupportType(Class<?> type) {
        List<Class<?>> classes = supportType();
        if (classes == null) {
            return false;
        }

        for (Class<?> aClass : classes) {
            if (aClass.isAssignableFrom(type)) {
                return true;
            }
        }

        return false;
    }

}
