package com.mortals.xhx.common.formdesign.component;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.mortals.xhx.common.code.ComponentEnum;
import com.mortals.xhx.common.formdesign.ColumnsItem;
import com.mortals.xhx.common.formdesign.DesignComponent;
import com.mortals.xhx.common.formdesign.ListItem;
import com.mortals.xhx.common.formdesign.OptionsItem;
import com.mortals.xhx.common.pdu.gen.component.ComponentCons;
import com.mortals.xhx.common.utils.StringUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 复选框组件
 *
 * @author: zxfei
 * @date: 2021/10/26 10:35
 * @description:
 **/
public class DesignRadioComponent extends DesignComponent {

    public DesignRadioComponent(String type) {
        super(type);
    }

    @Override
    public ListItem buildDefaultComponent(ComponentCons cons) {
        ListItem columnsItem = new ListItem();
        columnsItem.setId(cons.getVal());
        columnsItem.set_id(cons.getVal());
        columnsItem.setCompType("radio");
        columnsItem.setCompName(cons.getLabel());
        columnsItem.setCompIcon("radio");
        columnsItem.setEle("el-radio-group");
        columnsItem.setViewType("component");
        columnsItem.setConfig(true);
        columnsItem.setShowLabel(true);
        columnsItem.setLabel(cons.getLabel());
        columnsItem.setLabelWidth(120);
        columnsItem.setPlaceholder("请输入"+cons.getLabel());
        columnsItem.setMaxLength(50);
        columnsItem.setRequired(false);
        columnsItem.setDisabled(false);
        columnsItem.setBorder(false);
        columnsItem.setSize("medium");
        columnsItem.setGutter(15);
        columnsItem.setSpan(24);
        columnsItem.setOptionType("default");
        columnsItem.setDataType("static");
        columnsItem.setAction("https://www.fastmock.site/mock/51715c0157535b99010bde55f2df33c8/formDesigner/api/options");
        columnsItem.setClearable(false);
        columnsItem.setRules(Lists.newArrayList());
        columnsItem.setLayout("colItem");
        columnsItem.setRuleError("该字段不能为空");
        columnsItem.setRulesType("default");
        initDefaultRadioValue(cons, columnsItem);
        return columnsItem;
    }

    private void initDefaultRadioValue(ComponentCons cons, ListItem columnsItem) {
        if (cons.getVal().indexOf("<") != -1) {
            String cks = StringUtils.substringBetween(cons.getVal(), "<", ">");
            if(ObjectUtils.isEmpty(cks))return;
            List<OptionsItem> optionsItemList = StrUtil.splitTrim(cks, "-", -1).stream().map(option -> {
                OptionsItem options = new OptionsItem();
                options.setLabel(option);
                options.setValue(option);
                return options;
            }).collect(Collectors.toList());
            columnsItem.setOptions(optionsItemList);
        }
    }

    @Override
    public String getType() {
        return ComponentEnum.RADIO.getValue();
    }

}
