package com.ruoyi.project.business.controller.ConfigRule;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.business.dto.ConfigRuleDto;
import com.ruoyi.project.business.enums.JEPFuncEnum;
import com.ruoyi.project.business.enums.JEPSplitEnum;
import lombok.extern.slf4j.Slf4j;
import org.nfunk.jep.JEP;
import org.nfunk.jep.function.If;
import org.nfunk.jep.function.Round;
import org.nfunk.jep.function.Sum;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jubs
 */
@RestController
@RequestMapping("/business/configRule")
@Slf4j
public class ConfigRuleController extends BaseController {


    @PostMapping()
    public AjaxResult addRule(@RequestBody ConfigRuleDto dto) {
        log.info("前端公式传入:{}", dto.getFormValueStr());
        String str = dto.getFormValueStr();
        JEP jep = new JEP();
        jep.addFunction("SUM", new Sum());
        jep.addFunction("IF", new If());
        jep.addFunction("ROUND", new Round());
        //通过 `##$` 获取变量
        List<String> paramList = getParam(dto.getFormValueStr());
        //通过 `#$` 获取公式
        List<String> funcList = getFuncShort(dto.getFormValueStr());
//        paramList.forEach(param -> {
//                    log.info(JEPSplitEnum.PARAM_SPLIT_FLAG.getFuncKey() + param + JEPSplitEnum.PARAM_SPLIT_FLAG.getFuncKey());
//                    jep.addVariable(JEPSplitEnum.PARAM_SPLIT_FLAG.getFuncKey() + param + JEPSplitEnum.PARAM_SPLIT_FLAG.getFuncKey(), 2);
//                }
//        );
        jep.addVariable("$_$BJEDZJ$_$", 2);
        jep.addVariable("$_$BJEDSD$_$", 3);
        for (String func : funcList) {
            str = str.replace(JEPSplitEnum.FUNC_SPLIT_FLAG.getFuncKey(), "");
            JEPFuncEnum jEPFuncEnum = JEPFuncEnum.fromByKey(func);
            str = str.replace(func, jEPFuncEnum.getFuncValue());
        }

        jep.parseExpression(str);
        log.info("解析后的公式：{}", str);
        log.info("解析后的公式：{}", jep.getValue());
        return AjaxResult.success(jep.getValue());
    }


    List<String> getParam(String str) {
        List<String> matchList = new ArrayList<>();
        String regx = "\\$_\\$\\s*(.*?)\\s*\\$_\\$";
        Pattern pattern = Pattern.compile(regx);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            matchList.add(matcher.group(1));
        }
        return matchList;
    }

    List<String> getFuncShort(String str) {
        List<String> matchList = new ArrayList<>();
        Pattern pattern = Pattern.compile("\\$@\\$([^\\$]+)\\$@\\$");
        Matcher matcher = pattern.matcher(str);

// 查找匹配的内容并输出
        if (matcher.find()) {
            String content = matcher.group(1);
            matchList.add(content);
        }
        return matchList;
    }


}
