package com.brilliance;

import com.brilliance.swift.SwiftTransfer;
import com.brilliance.swift.constants.Mx2MtConstants;
import com.brilliance.swift.exception.SwiftException;
import com.brilliance.swift.util.StringUtil;
import com.brilliance.swift.vo.SwiftTranslationErrorInfo;
import com.brilliance.swift.vo.SwiftTranslationReport;
import com.prowidesoftware.swift.model.mt.AbstractMT;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Mt2MxTest {

    public void printSwiftTranslationReport(SwiftTranslationReport str) {
        System.out.println(str.getTranslationResult());
        System.out.println(str.getMtType());
        System.out.println(str.getMxType());
        System.out.println("***************");
        if (str != null) {
            String errorDsp = "";
            List<SwiftTranslationErrorInfo> errorInfos = str.getErrorInfos();
            if (errorInfos != null && errorInfos.size() > 0) {
                for (int i=0; i<errorInfos.size(); i++) {
                    SwiftTranslationErrorInfo errorInfo = errorInfos.get(i);
                    errorDsp += errorInfo.getErrorType()
                            + "-" + errorInfo.getErrorCode()
                            + ":" + errorInfo.getDescription();
                    if (StringUtil.isNotEmpty(errorInfo.getLocation())) {
                        errorDsp += ";Occur in the " + errorInfo.getLocation();
                    }
                    if (StringUtil.isNotEmpty(errorInfo.getOriginalValue())) {
                        errorDsp += ";Original Value is " + errorInfo.getOriginalValue();
                    }
                    errorDsp += Mx2MtConstants.NEW_LINE;
                }
            }
            if (StringUtil.isNotEmpty(errorDsp)) {
                System.out.println(errorDsp);
            }
            System.out.println(str.getMessage());
        }
    }

    public void test(String source, Map<String, Object> extraMap) {
        File file = FileUtils.toFile(Mx2MtTest.class.getResource(source));

        try {
            String mtStr=FileUtils.readFileToString(file);
            AbstractMT abstractMT = AbstractMT.parse(mtStr);
            abstractMT.getSwiftMessage().getBlock4().getTagValue("50");
        } catch (Exception e) {
            throw new SwiftException(e.getMessage());
        }

        SwiftTranslationReport str = SwiftTransfer.mt2MxPlus(file, null, extraMap);
        printSwiftTranslationReport(str);
    }

    public String refreshValue(String value, String continuePattern, int maxLength) {
        if (StringUtil.isEmpty(value)) {
            return "";
        }
        String regex = "(.*)\\\r\\\n" + continuePattern;
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(value);
        while (m.find()) {
            String findStr = m.group(1);
            if (findStr.length() < maxLength) {
                value = value.replace(findStr, findStr + " ");
            }
        }
        return value;
    }

    @Test
    public void testRefreshValue1() {
        String tag72="\\abc\r\n\\122\r\n//INSabc";
        String s = refreshValue(tag72, "//", 35);
        System.out.println("s====="+s);
    }


    @Test
    public void testPacs008001() {
        test("/swiftTxt/MT103.txt", null);
    }

    @Test
    public void testPacs008001_tag72() {
        test("/swiftTxt/MT03_tag72.txt", null);
    }

    @Test
    public void testPacs009001() {
        test("/swiftTxt/MT202.txt", null);
    }

    @Test
    public void testPacs009001_COV() {
        test("/swiftTxt/MT202_COV.txt", null);
    }

    @Test
    public void testPacs009001_tag72() {
        test("/swiftTxt/MT202_tag72.txt", null);
    }
    @Test
    public void testPacs009001_tag72_2() {
        test("/swiftTxt/MT202_tag72_2.txt", null);
    }

    @Test
    public void testCamt052001() {
        test("/swiftTxt/MT942.txt", null);
    }

    @Test
    public void testCamt053001() {
        test("/swiftTxt/MT940.txt", null);
    }

    @Test
    public void testPacs002001_199() {
        test("/swiftTxt/MT199.txt", null);
    }

    @Test
    public void testPacs002001_299() {
        test("/swiftTxt/MT299.txt", null);
    }


    @Test
    public void testPacs004001() {
        test("/swiftTxt/MT199.txt", null);
    }

    @Test
    public void testCamt054001() {
        test("/swiftTxt/MT910.txt", null);
    }

    @Test
    public void testCamt056001() {
        test("/swiftTxt/MT192.txt", null);
    }

    @Test
    public void testCamt060001() {
        test("/swiftTxt/MT920.txt", null);
    }

    @Test
    public void testCamt029001_1() {
        test("/swiftTxt/MT196.txt", null);
    }

    @Test
    public void testCamt057001() {
        test("/swiftTxt/MT210.txt", null);
    }

    @Test
    public void testPacs002001_103rej() {
        test("/swiftTxt/MT103REJ.txt", null);
    }

    @Test
    public void testPacs002001_202rej() {test("/swiftTxt/MT202REJ.txt", null);}

    @Test
    public void testPacs004001_103rej() {test("/swiftTxt/MT103REJ_pacs004.txt", null);}

    @Test
    public void testPacs004001_202rej() {test("/swiftTxt/MT202REJ_pacs004.txt", null);}

}