package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.rulematch;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesNumberRuleMatchDispatchService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.pojo.mes.bean.MesProdRuleIgnoreCfg;
import com.google.common.base.Objects;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式匹配
 */
@Slf4j
@Service
public class MesNumberRuleMatchRegularExpressionService implements IMesNumberRuleMatchDispatchService {

    @Override
    public Map<String, Object> matchNumberRule(String organizeCode, String sn, Object... params) {

        Map<String, Object> result = new HashMap<>();
        result.put(MesPcnExtConstWords.RESULT, false);

        if (StringUtils.isEmpty(params[1])) {
            result.put(MesPcnExtConstWords.MESSAGE, "未维护匹配规则!");
            return result;
        }

        String matchRule =  params[1].toString();

        if (!match(matchRule, sn)) {
            result.put(MesPcnExtConstWords.MESSAGE, String.format("条码[%s]不匹配规则[%s]!", sn, matchRule));
            return result;
        }

        result.put(MesPcnExtConstWords.RESULT, true);

        return result;
    }

    private /*static*/ Boolean match(String ruleStr, String barCode) {
        if (StringUtils.isEmpty(ruleStr)) {
            return false;
        }
        if (!ruleStr.contains(MesPcnExtConstWords.ASTERISK) && !ruleStr.contains(MesPcnExtConstWords.QUESTION_MARK) && !ruleStr.contains(MesPcnExtConstWords.COMMA)) {
            if (Objects.equal(ruleStr.toLowerCase(), barCode.toLowerCase())) {
                return true;
            }
            return false;
        }
        String ruleArray[] = ruleStr.split(MesPcnExtConstWords.COMMA);
        for (String rule : ruleArray) {
            if (rule.contains(MesPcnExtConstWords.DATE_FORMAT_yyyyMMdd)) {
                String pattern = "\\d{4}\\d{2}\\d{2}";
                Pattern regex = Pattern.compile(pattern);
                Matcher matcher = regex.matcher(rule);
                return  matcher.matches();
            } else if (rule.contains(MesPcnExtConstWords.DATE_FORMAT_POINT) || rule.contains(MesPcnExtConstWords.DATE_FORMAT_SLASH) || rule.contains(MesPcnExtConstWords.DATE_FORMAT_SEPARATOR)) {
                String pattern = "\\d{4}[./-]\\d{2}[./-]\\d{2}";
                Pattern regex = Pattern.compile(pattern);
                Matcher matcher = regex.matcher(rule);
                return  matcher.matches();
            }

            String ruleNoSpecialChar = transfer(rule);
            String barcodeNoSpecialChar = transfer(barCode);
            ///将规则转换为正则表达式，使用正则表达式进行匹配，正则中去除二维码的起始特殊符号
            String regStr = "^" + ruleNoSpecialChar.toLowerCase().replace(MesPcnExtConstWords.BRACKET_R, "\\)").replace(MesPcnExtConstWords.QUESTION_MARK, "(?:.|\n){1}").replace(MesPcnExtConstWords.ASTERISK, "(?:.|-\n)*").replace(MesPcnExtConstWords.SQUARE_BRACKETS_L, "\\[") + "$";
            Pattern regex = Pattern.compile(regStr);
            //end update
            if (regex.matcher(barcodeNoSpecialChar.toLowerCase()).matches()) return true;
            if (rule == barCode) return true;

        }
        return false;
    }

    public /*static*/ String transfer(String str){
        // 1. 定义正则表达式，匹配特殊字符
        String regex = "[^a-zA-Z0-9^\\s^*?[)>.:^/]]";
        // 2. 使用正则表达式替换特殊字符为空字符串
       return str.replaceAll(regex, "");
    }

    @Override
    public Map<String, Object> matchIgnoreCfg(String organizeCode, String sn, Map<String, Object> result, List<MesProdRuleIgnoreCfg> prodRuleIgnoreCfgList) {
        if (StringUtils.isEmpty(sn) || CollectionUtils.isEmpty(prodRuleIgnoreCfgList)) return result;
        for (MesProdRuleIgnoreCfg prodRuleIgnoreCfg : prodRuleIgnoreCfgList) {
            if (null == prodRuleIgnoreCfg || StringUtils.isEmpty(prodRuleIgnoreCfg.getMatchRule())) continue;
            if (match(prodRuleIgnoreCfg.getMatchRule(), sn)) {
                result.put(MesPcnExtConstWords.RESULT, false);
                result.put(MesPcnExtConstWords.MESSAGE, String.format("条码[%s]匹配不可用规则[%s]!", sn, prodRuleIgnoreCfg.getMatchRule()));
                return result;
            }
        }
        return result;
    }

    /*  public static void main(String[] args) {
        String sn = "[)>@06@Y8360100000000B@P26462392@12V654406818@T1124107207000344@@";
        String matchRule = "*26462392*";
        System.out.println(match(matchRule, sn));
    }*/

}
