package com.brilliance.isc.common.transaction.check;

import com.brilliance.isc.common.context.SettleContext;
import com.brilliance.isc.common.trndoc.vo.DoceotVo;
import com.brilliance.isc.common.util.I18nUtil;
import com.brilliance.mda.runtime.mda.util.Strings;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

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

/**
 * /**
 * * Doceot的部分方法
 */
@Component
public class DoceotCheck implements ICheck<DoceotVo> {

    @Override
    public Map<String, String> check(DoceotVo doceotVo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.putAll(checkAddstrN1001(doceotVo));
        resultMap.putAll(checkCortypN1000(doceotVo));
        resultMap.putAll(checkAdrdtacidN1000(doceotVo));
        resultMap.putAll(checkAdrdtecidN1000(doceotVo));
        resultMap.putAll(checkStaflgN1000(doceotVo));
        resultMap.putAll(checkTag79zN1000(doceotVo));
        return resultMap;
    }

    public Map<String, String> check(List<DoceotVo> doceotVoList) {
        Map<String, String> errorMap = new HashMap<String, String>();
        if (!CollectionUtils.isEmpty(doceotVoList)) {
            for (DoceotVo doceotVo : doceotVoList) {
                errorMap.putAll(check(doceotVo));
            }
        }
        return errorMap;
    }


    public Map<String, String> checkAddstrN1001(DoceotVo doceotVo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        String transName = SettleContext.getTransName();
        if (!StringUtils.isEmpty(doceotVo.getAddstr())) {
            if ("AME".equals(Strings.mid(transName, 4, 3)) || "OPN".equals(Strings.mid(transName, 4, 3))) {
                if ("SWT".equals(doceotVo.getCortyp()) && !character(doceotVo.getAddstr())) {
//                    doceotVo.getMaps().put("tag79z","This fields is not allowed [}|$`^~]");
                    resultMap.put("tag79z", I18nUtil.getMessage("0006"));
                }
            }
        }
        return resultMap;
    }

    public Map<String, String> checkCortypN1000(DoceotVo doceotVo) {
        return new HashMap<String, String>();
    }


    public Map<String, String> checkAdrdtacidN1000(DoceotVo doceotVo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        if (!StringUtils.isEmpty(doceotVo.getId())) {
            if (" ".equals(doceotVo.getDelflg())) {
                if ("DTA".equals(doceotVo.getCortyp()) && StringUtils.isEmpty(doceotVo.getAdrdtacid())) {
//                    doceotVo.getMaps().put("adrdtacid", MdaUtils.getI18NString("doceot", "CT000137"));
                    resultMap.put("adrdtacid", I18nUtil.getMessage("0030"));
                }
            }
        }
        return resultMap;
    }


    public Map<String, String> checkAdrdtecidN1000(DoceotVo doceotVo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        if (!StringUtils.isEmpty(doceotVo.getId()) && " ".equals(doceotVo.getDelflg()) && "DTE".equals(doceotVo.getCortyp()) && StringUtils.isEmpty(doceotVo.getAdrdtecid())) {
//            doceotVo.getMaps().put("adrdtecid", MdaUtils.getI18NString("doceot", "CT000138"));
            resultMap.put("adrdtecid", I18nUtil.getMessage("0030"));
        }
        return resultMap;
    }


    public Map<String, String> checkStaflgN1000(DoceotVo doceotVo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        //>>>ZL: ISS.000010   从501中移植过来---------
        switch (doceotVo.getStaflg()) {
            case "M":
                if (!"SWT".equals(doceotVo.getCortyp()) || ("SWT".equals(doceotVo.getCortyp()) && !"SWT999".equals(doceotVo.getDocsnf()))) {
//                    doceotVo.getMaps().put("staflg", MdaUtils.getI18NString("doceot","CT000168"));
                    resultMap.put("staflg", I18nUtil.getMessage("0054"));
                }
                break;
            case "T":
                if (!"TLX".equals(doceotVo.getCortyp())) {
//                    doceotVo.getMaps().put("staflg", MdaUtils.getI18NString("doceot","CT000169"));
                    resultMap.put("staflg", I18nUtil.getMessage("0055"));
                }
                /**  case "B"
                 $pandsc = Trim( PANDSC )
                 $pandsc = Mid( $pandsc, 1, 3 )
                 if $pandsc <> "CMT" then
                 Error( 'LG000174' )
                 endif**/
                break;
        }
        //<<<ZL: ISS.000010  end-------------


        return resultMap;
    }

    public Map<String, String> checkTag79zN1000(DoceotVo doceotVo) {
        Map<String, String> resultMap = new HashMap<String, String>();
        if (!StringUtils.isEmpty(doceotVo.getTag79z()) && "SWT".equals(doceotVo.getCortyp())) {
            if (!character(doceotVo.getTag79z())) {
//                doceotVo.getMaps().put("tag79z","This fields is not allowed [}|$`^~]");
                resultMap.put("tag79z", I18nUtil.getMessage("0006"));
            }
        }
        return resultMap;
    }

    public boolean character(String line) {
        //This fields is not allowed [}|$`^~]
        if (!StringUtils.isEmpty(line)) {
            int p = 0;
            for (int j = 1; j <= Strings.len(line); j++) {
                p = Strings.pos("[}|$`^~]", Strings.mid(line, j, 1));
                if (p > 0) {
                    return false;
                }
            }
        }
        return true;
    }

    public Map<String, String> check(DoceotVo doceotVo, int i) {
        Map<String, String> checked = check(doceotVo);
        checked.forEach((key, value) -> {
            checked.put(key + "[" + i + "]", value);
            checked.remove(key);
        });
        return checked;
    }
}
