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.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

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

    public DesignSelectComponent(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("select");
        columnsItem.setCompName(cons.getLabel());
        columnsItem.setCompIcon("select");
        columnsItem.setEle("el-select");
        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.setMultiple(false);
        columnsItem.setCollapseTags(false);
        columnsItem.setDisabled(false);
        columnsItem.setGutter(15);
        columnsItem.setSpan(24);
        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");
        initDefaultSelectValue(cons, columnsItem);
        return columnsItem;
    }

    private void initDefaultSelectValue(ComponentCons cons, ListItem columnsItem) {
        //判断如果包含年，月，日，选项框 基础化
        if (cons.getVal().indexOf("年") != -1) {
            //初始化10年
            List<OptionsItem> optionsItems = initYears();
            columnsItem.setOptions(optionsItems);
        } else if (cons.getVal().indexOf("月") != -1) {
            List<OptionsItem> optionsItems = initMonths();
            columnsItem.setOptions(optionsItems);
        } else if (cons.getVal().indexOf("日") != -1) {
            List<OptionsItem> optionsItems = initDays();
            columnsItem.setOptions(optionsItems);
        }

        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);

            // cons.setVal(cons.getVal().substring(0, cons.getVal().indexOf("[")));
            // cons.setLabel(cons.getLabel().substring(0, cons.getLabel().indexOf("[")));

        }
    }
    public List<OptionsItem> initYears() {
        List<OptionsItem> list = new ArrayList<>();
        for (int i = 2020; i < 2030; i++) {
            OptionsItem optionsItem = new OptionsItem();
            optionsItem.setLabel(i + "");
            optionsItem.setValue(i + "");
            list.add(optionsItem);
        }
        return list;
    }

    public List<OptionsItem> initMonths() {
        List<OptionsItem> list = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            OptionsItem optionsItem = new OptionsItem();
            optionsItem.setLabel(i + "");
            optionsItem.setValue(i + "");
            list.add(optionsItem);
        }
        return list;
    }

    public List<OptionsItem> initDays() {
        List<OptionsItem> list = new ArrayList<>();
        for (int i = 1; i <= 31; i++) {
            OptionsItem optionsItem = new OptionsItem();
            optionsItem.setLabel(i + "");
            optionsItem.setValue(i + "");
            list.add(optionsItem);
        }
        return list;
    }

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