package com.yiqi.system.domain.dto;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.yiqi.system.domain.SysConfig;
import com.yiqi.system.domain.SysConfigRecord;
import io.swagger.models.auth.In;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author douya
 * @description
 * @date 2024/10/30
 */
@Data
public class DataGroupRules implements Serializable {

    private static final long serialVersionUID = 1L;

    private String field;

    private String type;

    private Object value;

    private String title;

    private String fixed;

    private Props props;

    private Map suffix;

    private Map col;

    private List<Option> options;

    public static DataGroupRules create(String type, String field, String title, String params, Object value) {
        DataGroupRules dataGroupHeader = new DataGroupRules();
        dataGroupHeader.setTitle(title);
        dataGroupHeader.setType(type);
        dataGroupHeader.setField(field);
        if (value != null) {
            dataGroupHeader.setValue(value);
        }
        if (type.equals("input")) {
            Props props = new Props();
            props.setType("text");
            props.setPlaceholder("请输入" + title);
            if (params != null) {
                props.setPlaceholder(params);
            }
            dataGroupHeader.setProps(props);
        }
        if (type.equals("inputNumber")) {
            Props props = new Props();
            props.setType("inputNumber");
            props.setPlaceholder("请输入" + title);
            dataGroupHeader.setProps(props);
        }
        if (type.equals("textarea")) {
            dataGroupHeader.setType("input");
            Props props = new Props();
            props.setType("textarea");
            props.setPlaceholder("请输入" + title);
            if (params != null) {
                props.setPlaceholder(params);
            }
            props.setRows(4);
            dataGroupHeader.setProps(props);
        }
        if (type.equals("radio")) {
            dataGroupHeader.setOptions(transform(params));
        }
        if (type.equals("checkbox")) {
            dataGroupHeader.setOptions(transform(params));
        }
        if (type.equals("select")) {
            dataGroupHeader.setOptions(transform(params));
            Props props = new Props();
            props.setMultiple(false);
            props.setPlaceholder("请选择" + title);
            dataGroupHeader.setProps(props);
        }
        if (type.equals("upload")) {
            dataGroupHeader.setType("image-upload");
        }
        return dataGroupHeader;
    }

    /**
     * 创建配置表单
     */
    public static DataGroupRules createConfigForm(SysConfigRecord sysConfigRecord) {
        DataGroupRules dataGroupHeader = new DataGroupRules();
        dataGroupHeader.setTitle(sysConfigRecord.getInfo());
        dataGroupHeader.setType(sysConfigRecord.getType());
        dataGroupHeader.setField(sysConfigRecord.getMenuName());
        if (sysConfigRecord.getValue() != null) {
            dataGroupHeader.setValue(sysConfigRecord.getValue());
        }
        String type = sysConfigRecord.getType();
        String title = sysConfigRecord.getInfo();
        String params = sysConfigRecord.getParameter();
        String placeholder = sysConfigRecord.getDescition();
        String inputType = sysConfigRecord.getInputType();
        if (type.equals("text")) {
            dataGroupHeader.setType("input");
            if (inputType.equals("input")) {
                Props props = new Props();
                props.setType("text");
                props.setPlaceholder("请输入" + title);
                if (placeholder != null) {
                    props.setPlaceholder(placeholder);
                }
                dataGroupHeader.setProps(props);
            } else if (inputType.equals("number")) {
                Props props = new Props();
                props.setType("inputNumber");
                props.setPlaceholder("请输入" + title);
                if (placeholder != null) {
                    props.setPlaceholder(placeholder);
                }
                dataGroupHeader.setProps(props);
            } else {
                Props props = new Props();
                props.setType("text");
                props.setPlaceholder("请输入" + title);
                if (placeholder != null) {
                    props.setPlaceholder(placeholder);
                }
                dataGroupHeader.setProps(props);
            }
        }
        if (type.equals("inputNumber")) {
            Props props = new Props();
            props.setType("inputNumber");
            props.setPlaceholder("请输入" + title);
            if (placeholder != null) {
                props.setPlaceholder(placeholder);
            }
            dataGroupHeader.setProps(props);
        }
        if (type.equals("textarea")) {
            dataGroupHeader.setType("input");
            Props props = new Props();
            props.setType("textarea");
            props.setPlaceholder("请输入" + title);
            props.setRows(6);
            if (placeholder != null) {
                props.setPlaceholder(placeholder);
            }
            dataGroupHeader.setProps(props);
        }
        if (type.equals("radio")) {
            dataGroupHeader.setOptions(transform(params));
        }
        if (type.equals("checkbox")) {
            dataGroupHeader.setOptions(transform(params));
            if (sysConfigRecord.getValue() != null) {
                dataGroupHeader.setValue(JSONUtil.parseArray(sysConfigRecord.getValue()));
            }
        }
        if (type.equals("select")) {
            dataGroupHeader.setOptions(transform(params));
            Props props = new Props();
            props.setMultiple(false);
            props.setPlaceholder("请选择" + title);
            if (placeholder != null) {
                props.setPlaceholder(placeholder);
            }
            dataGroupHeader.setProps(props);
        }
        if (type.equals("upload")) {
            dataGroupHeader.setType("image-upload");
            if (sysConfigRecord.getValue() != null) {
                dataGroupHeader.setValue(sysConfigRecord.getValue());
            }
        }
        if (placeholder != null) {
            Map<String, Object> suffix = new HashMap<>();
            suffix.put("class", "tips-info");
            suffix.put("type", "div");
            Map<String, String> innerHTML = new HashMap<>();
            innerHTML.put("innerHTML", placeholder);
            suffix.put("domProps", innerHTML);
            dataGroupHeader.setSuffix(suffix);
        }
        Map col = new HashMap();
        col.put("span", 13);
        dataGroupHeader.setCol(col);
        return dataGroupHeader;
    }


    public static DataGroupRules createGidHeader(Integer gid) {
        DataGroupRules dataGroupHeader = new DataGroupRules();
        dataGroupHeader.setType("hidden");
        dataGroupHeader.setField("gid");
        dataGroupHeader.setValue(gid);
        return dataGroupHeader;
    }

    public static DataGroupRules createIdHeader(Integer id) {
        DataGroupRules dataGroupHeader = new DataGroupRules();
        dataGroupHeader.setType("hidden");
        dataGroupHeader.setField("id");
        dataGroupHeader.setValue(id);
        return dataGroupHeader;
    }

    public static DataGroupRules createStatusHeader(Integer defaultValue) {
        DataGroupRules dataGroupHeader = new DataGroupRules();
        dataGroupHeader.setType("radio");
        dataGroupHeader.setTitle("状态");
        dataGroupHeader.setField("status");
        dataGroupHeader.setValue(defaultValue);
        Option option1 = new Option();
        option1.setValue(1);
        option1.setLabel("显示");
        Option option2 = new Option();
        option2.setValue(0);
        option2.setLabel("隐藏");
        dataGroupHeader.setOptions(CollUtil.list(false, option1, option2));
        return dataGroupHeader;
    }

    public static DataGroupRules createSortHeader(Integer defaultValue) {
        DataGroupRules dataGroupHeader = new DataGroupRules();
        dataGroupHeader.setType("inputNumber");
        dataGroupHeader.setTitle("排序");
        dataGroupHeader.setField("sort");
        dataGroupHeader.setValue(defaultValue);
        return dataGroupHeader;
    }

    @Data
    public static class Props {

        private String type;

        private Boolean multiple;

        private String placeholder;

        private Integer precision = 0;

        private Integer rows;

    }

    @Data
    public static class Option {

        private Object value;

        private String label;
    }

    public static List<Option> transform(String params) {
        String[] lines = params.split("\n");
        // 遍历每一行，并按'='分割为键和值
        List<Option> options = CollUtil.newArrayList();
        for (String line : lines) {
            Option map = new Option();
            String[] parts = line.split("=");
            if (parts.length == 2) {
                map.setLabel(parts[1]);
                map.setValue(parts[0]);
                options.add(map);
            }
        }
        return options;
    }

    /**
     * 获取单选的值
     *
     * @param params
     * @return
     */
    public static String getSigleOptionValue(String value, String params) {
        String[] lines = params.split("\n");
        // 遍历每一行，并按'='分割为键和值
        for (String line : lines) {
            String[] parts = line.split("=");
            if (parts.length == 2 && parts[0].equals(value)) {
                return parts[1];
            }
        }
        return "";
    }

    /**
     * 获取多选的值  用逗号隔开
     *
     * @param value  ["1","4"]
     * @param params
     * @return
     */
    public static String getMuliteOptionValue(String value, String params) {
        String[] lines = params.split("\n");
        // 遍历每一行，并按'='分割为键和值
        JSONArray jsonArray = JSONUtil.parseArray(value);
        List<String> arrVal = CollUtil.newArrayList();
        for (String line : lines) {
            String[] parts = line.split("=");
            for (int i = 0; i < jsonArray.size(); i++) {
                String val = jsonArray.getStr(i);
                if (parts.length == 2 && parts[0].equals(val)) {
                    arrVal.add(parts[1]);
                }
            }
        }
        return CollUtil.join(arrVal, ",");
    }

    public static boolean isNumeric(String str) {
        return str.matches("\\d+");
    }
}
