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

import com.brframework.commoncms.annatotion.column.CheckBoxColumn;
import com.brframework.commoncms.core.layout.AbstractPanelLayoutColumn;
import com.brframework.commoncms.core.cms.Select;
import com.brframework.commoncms.core.layout.SelectOption;
import com.brframework.commonweb.core.SpringContext;
import com.brframework.commonweb.exception.HandleException;
import com.google.common.collect.Lists;
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 15:03
 * @see CheckBoxColumn
 */
@Data
public class LayoutCheckBoxColumn extends AbstractPanelLayoutColumn {

    private static final String TYPE = "CHECK_BOX";
    private String key;
    private String title;

    /**
     * 选项
     */
    private List<SelectOption> select;

    private String optionsMappingContext;

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

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

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

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

        //强制要求使用String
        if (!columnField.getType().getName().equals(String.class.getName())) {
            throw new HandleException("CheckBox强制要求使用String类型，不同选项使用,(逗号)隔开");
        }

        //获取select option
        CheckBoxColumn checkBoxColumn = AnnotationUtils.findAnnotation(columnField, CheckBoxColumn.class);
        String[] options = checkBoxColumn.options();
        setSelect(Lists.newArrayList());
        String optionsMappingContext = checkBoxColumn.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);
        }
    }

    @Override
    public List<Class<?>> supportType() {
        return Lists.newArrayList(
        );
    }

}
