package com.mortals.xhx.common.pdu.gen.component;

import cn.hutool.core.util.StrUtil;
import com.mortals.xhx.common.code.ComponentEnum;
import com.mortals.xhx.common.pdu.gen.*;
import com.mortals.xhx.common.utils.ExportDocUtil;
import com.mortals.xhx.common.utils.StringUtils;

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

/**
 * 下拉框组件
 *
 * @author: zxfei
 * @date: 2021/10/26 10:35
 * @description:
 **/
public class CheckboxComponent extends Component {

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

    @Override
    public FieldsItem buildDefaultComponent(ComponentCons cons) {
        //复选框
        FieldsItem fieldsItem = new FieldsItem();
        Config config = new Config();
        config.setLabel("多选框组");
        config.setTag("el-checkbox-group");
        config.setTagIcon("checkbox");
        config.setDefaultValue(new ArrayList<>());
        config.setLabelWidth("");
        config.setShowLabel(true);
        config.setChangeTag(true);
        config.setRequired(true);
        config.setLayout("colFormItem");
        config.setDocument("https://element.eleme.cn/#/zh-CN/component/checkbox");
        config.setRegList(new ArrayList<>());
        config.setSpan(12);
        config.setBorder(false);
        fieldsItem.setConfig(config);

        Slot slot = new Slot();
        List<OptionsItem> list = new ArrayList<>();
        OptionsItem optionsItem = new OptionsItem();
        optionsItem.setLabel("选项一");
        optionsItem.setValue("1");
        list.add(optionsItem);
        slot.setOptions(list);
        fieldsItem.setSlot(slot);

        Style style = new Style();
        fieldsItem.setStyle(style);

        fieldsItem.setDisabled(false);
        fieldsItem.setVModel("checkbox");

        initDefaultCheckBoxValue(cons, fieldsItem);
        return fieldsItem;
    }

    private void initDefaultCheckBoxValue(ComponentCons cons, FieldsItem fieldsItem) {
        if (cons.getLabel().indexOf("[") != -1) {
            String cks = StringUtils.substringBetween(cons.getLabel(), "[", "]");
            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());
            fieldsItem.getSlot().setOptions(optionsItemList);

            cons.setVal(cons.getVal().substring(0, cons.getVal().indexOf("[")));
            cons.setLabel(cons.getLabel().substring(0, cons.getLabel().indexOf("[")));
            //修改模板文件 通过id 查询xpath路径
            String parentId = cons.getNodeEntity().getParentId();
            List<String> paths = new ArrayList<>();
            ExportDocUtil.getPath(paths, cons.getList(), parentId);
            String elpress = String.format("${%s}", cons.getVal());
            ExportDocUtil.replaceTemplateEl(paths, elpress, cons.getEl(), cons.getDocument());
            cons.getNodeEntity().setContent(elpress);
        }
    }

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

}
