package com.brframework.commoncms.core.layout.column;

import com.brframework.commonweb.core.SpringContext;
import com.brframework.commonweb.exception.HandleException;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.brframework.commoncms.annatotion.column.SelectColumn;
import com.brframework.commoncms.core.layout.AbstractPanelLayoutColumn;
import com.brframework.commoncms.core.cms.Select;
import com.brframework.commoncms.core.layout.SelectOption;
import lombok.Data;
import org.springframework.core.annotation.AnnotationUtils;

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

/**
 * 下拉框
 *
 * @author xu
 * @date 2018/9/26 14:11
 * @see SelectColumn
 */
@Data
public class LayoutSelectColumn extends AbstractPanelLayoutColumn {

    private static final String TYPE_DEF = "SELECT";
    private String key;
    private String title;
    /**
     * 选项
     */
    private List<SelectOption> select;

    /**
     * 多级联动关联属性
     */
    private String cascade;

    private String optionsMappingContext;

    private boolean onlyRead;

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public String getType() {
        return TYPE_DEF;
    }

    @Override
    public void create(Field columnField) throws Throwable {
        init(columnField);

        //获取select option
        SelectColumn cmsSelectColumn = AnnotationUtils.findAnnotation(columnField, SelectColumn.class);
        String[] options = cmsSelectColumn.options();
        setSelect(Lists.newArrayList());
        String optionsMappingContext = cmsSelectColumn.optionsMappingContext();
        if (options.length == 0) {
            Select context = SpringContext.getBean(Select.class);
            Map<String, String> map = context.get(optionsMappingContext);
            if (map != null && map.size() > 0) {
                map.forEach((key, value1) -> {
                    SelectOption selectOption = SelectOption.builder()
                            .id(key)
                            .value(value1)
                            .build();

                    getSelect().add(selectOption);
                });
            } else {
                setOptionsMappingContext(optionsMappingContext);
            }
        } else {
            List<SelectOption> selectOptions = Lists.newArrayList();
            for (String option : options) {
                String[] o = option.split(",");
                if (o.length != 2) {
                    throw new HandleException("非正确键值对:" + option);
                }

                SelectOption selectOption = SelectOption.builder()
                        .id(o[0])
                        .value(o[1])
                        .build();

                selectOptions.add(selectOption);
            }
            setSelect(selectOptions);
        }

        if (Strings.isNullOrEmpty(cmsSelectColumn.cascade())) {
            setCascade(null);
        } else {
            setCascade(cmsSelectColumn.cascade());
            setOptionsMappingContext(optionsMappingContext);
            setSelect(null);
        }

        //设置只读
        setOnlyRead(isOnlyRead(columnField));
    }

    /**
     * 获取下拉框是否为只读
     *
     * @param columnField 对应列
     * @return 是否为只读
     */
    private boolean isOnlyRead(Field columnField) {
        SelectColumn selectColumn = AnnotationUtils.findAnnotation(columnField, SelectColumn.class);
        return selectColumn != null && selectColumn.onlyRead();
    }

    @Override
    public List<Class<?>> supportType() {
        return null;
    }

}
