package com.brilliance;

import com.brilliance.swift.SwiftTransfer;
import com.brilliance.swift.constants.Mx2MtConstants;
import com.brilliance.swift.mx2mt.AbstractMx2MtCreator;
import com.brilliance.swift.vo.SwiftTranslationErrorInfo;
import com.brilliance.swift.vo.SwiftTranslationReport;
import org.apache.commons.io.FileUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Mx2MtTest {

    public void printSwiftTranslationReport(SwiftTranslationReport str) {
        if (str != null) {
            System.out.println("MX Type = " + str.getMxType());
            System.out.println("MT Type = " + str.getMtType());
            System.out.println("Translation Result = " + str.getTranslationResult());
            List<SwiftTranslationErrorInfo> errorInfos = str.getErrorInfos();
            if (errorInfos != null && errorInfos.size() > 0) {
                for (int i=0; i<errorInfos.size(); i++) {
                    SwiftTranslationErrorInfo errorInfo = errorInfos.get(i);
                    System.out.println(errorInfo.toString());
                }
            }
            System.out.println(str.getMessage());
        }
    }

    public void test(String source, Map<String, Object> extraMap) {
        File file = FileUtils.toFile(Mx2MtTest.class.getResource(source));
        SwiftTranslationReport str = SwiftTransfer.mx2MtPlus(file, null, extraMap);
        printSwiftTranslationReport(str);
    }

    @Test
    public void test103() {
        test("/swiftXml/Pacs00800108.xml", null);
    }

    @Test
    public void test103Retn() {
        test("/swiftXml/MxPacs004001_Pacs008.xml", null);
    }

    @Test
    public void test202() {
        test("/swiftXml/Pacs00900108.xml", null);
    }

    @Test
    public void test202Cov() {
        test("/swiftXml/MxPacs00900108_COV.xml", null);
    }

    @Test
    public void test202Retn() {
        test("/swiftXml/MxPacs004001_full.xml", null);
    }

    @Test
    public void test900() {
        test("/swiftXml/MxCamt05400108_DEBIT.xml", null);
    }

    @Test
    public void test910() {
        test("/swiftXml/MxCamt05400108_CREDIT.xml", null);
    }

    @Test
    public void test940() {
        test("/swiftXml/MxCamt05300108_940.xml", null);
    }

    @Test
    public void test950() {
        test("/swiftXml/MxCamt05300108_950.xml", null);
    }

    @Test
    public void test942() {
        test("/swiftXml/MXcamt05200108.xml", null);
    }

    @Test
    public void test210() {
        test("/swiftXml/MxCamt05700106.xml", null);
    }

    @Test
    public void test199() {
        test("/swiftXml/MxPacs00200110_199.xml", null);
    }

    @Test
    public void test299() {
        test("/swiftXml/MxPacs00200110_299.xml", null);
    }

    @Test
    public void test192() {
        test("/swiftXml/MxCamt05600108_192.xml", null);
    }

    @Test
    public void test292() {
        test("/swiftXml/MxCamt05600108_292.xml", null);
    }

    @Test
    public void test196() {
        test("/swiftXml/MxCamt02900109_196.xml", null);
    }

    @Test
    public void test296() {
        test("/swiftXml/MxCamt02900109_296.xml", null);
    }

    @Test
    public void test920() {
        test("/swiftXml/MxCamt06000105.xml", null);
    }

    @Test
    public void test292_2() {
        test("/swiftXml/camt05800108.xml", null);
    }

    @Test
    public void test110() {
        test("/swiftXml/camt10700101.xml", null);
    }

    @Test
    public void testXmlFilePath() {
        SwiftTranslationReport str = SwiftTransfer.mx2MtPlus("d:/test/MxPacs00800108.xml", true, null, null);
        System.out.println(str.getMessage());
    }


    @Test
    public void testSwiftFile() {
        try {
            File file = FileUtils.toFile(Mx2MtTest.class.getResource("/swiftXml/test.xml"));
            String xml = FileUtils.readFileToString(file);
            String regex = "<!--(.*?)-->";
            Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
            Matcher m = p.matcher(xml);
            String mt = "";
            while (m.find()) {
                String message = m.group(1);
                if (message.indexOf("{4:") > -1) {
                    mt = message;
                }
            }
            mt = mt.trim().replace("^~", Mx2MtConstants.NEW_LINE);
            //去掉首行空格
            String[] mts = mt.split(Mx2MtConstants.NEW_LINE);
            mt = "";
            for (int i=0; i<mts.length; i++) {
                mt += mts[i].trim();
                if (i < (mts.length-1)) {
                    mt += Mx2MtConstants.NEW_LINE;
                }
            }
            System.out.println(mt);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test() {
        String tagName = "445566/TELB/123/HOLD/456456/PHOB/1123/HOLD/789/UDLC/yyy/HOLD/xxx/TELB/qqq";
        String regex = "([/(UDLC|PHOB|TELB)/]{1}[0-9a-zA-Z/\\-\\?:\\(\\)\\.,'\\+]*)";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(tagName);
        while (m.find()){
            String str = m.group(1);
            //System.out.println(str);
        }
        tagName = "123/UDLC/123/PHOB/456";
        String[] strs = tagName.split("(?=/UDLC/|/TELB/|/PHOB/)");
        System.out.println(strs[0]);

        String tmpPath = "Document/FIToFICstmrCdtTrf/CdtTrfTxInf/ChrgsInf(1)/Amt".replaceAll("\\([0-9]+\\)", "");
        System.out.println(tmpPath);

        System.out.println("60".matches("50[CK]?"));
    }

    /**
     * 找到xsd中所有节点都是必填的PATH放到map中
     * @param prefixPath
     * @param nodeList
     * @param list
     */
    private void buildPath(String prefixPath, List nodeList, List<String> list) {
        for (int i=0; i<nodeList.size(); i++) {
            Element e = (Element)nodeList.get(i);
            String type = e.attributeValue("type");
            List nodeList1 = e.selectNodes("//xs:simpleType[@name='"+type+"']");
            if ("0".equals(e.attributeValue("minOccurs"))) {
                continue;
            }
            if (nodeList1 != null && nodeList1.size() > 0) {
                list.add(prefixPath + "." + e.attributeValue("name"));
            } else {
                nodeList1 = e.selectNodes("//xs:complexType[@name='"+type+"']");
                Element element = (Element)nodeList1.get(0);
                nodeList1 = element.selectNodes(".//xs:element");
                buildPath(prefixPath + "." + e.attributeValue("name"), nodeList1, list);
            }
        }
    }

    public void readXsd2Txt(String xsdName) throws DocumentException, IOException {
        URL resource = AbstractMx2MtCreator.class.getResource("/xsd/"+xsdName+".xsd");
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(resource);
        Element element = document.getRootElement();
        List<Node> nodeList = element.selectNodes(".//xs:element[@name='Document']");
        element = (Element)nodeList.get(0);
        String type = element.attributeValue("type");
        nodeList = element.selectNodes("//xs:complexType[@name='"+type+"']");
        element = (Element)nodeList.get(0);
        nodeList = element.selectNodes(".//xs:element");
        List<String> list = new ArrayList<>();
        buildPath("Document", nodeList, list);
        File file = new File(System.getProperty("user.dir")+"\\src\\main\\resources\\xmlPathIgnore\\"+xsdName+".txt");
        List<String> orgList = FileUtils.readLines(file);
        orgList.addAll(list);
        FileWriter fw = new FileWriter(file);
        BufferedWriter bw = new BufferedWriter(fw);
        for (String str : orgList) {
            bw.write(str);
            bw.newLine();
        }
        bw.close();
        fw.close();
    }

    @Test
    public void testXsd2Txt() throws DocumentException, IOException {
        List<String> xsdList = new ArrayList<>();
        xsdList.add("camt02900109");
        xsdList.add("camt05200108");
        xsdList.add("camt05300108");
        xsdList.add("camt05400108");
        xsdList.add("camt05600108");
        xsdList.add("camt05700106");
        xsdList.add("camt06000105");
        xsdList.add("pacs00200110");
        xsdList.add("pacs00400109");
        xsdList.add("pacs00800108");
        xsdList.add("pacs00900108");
        for (String xsdName : xsdList) {
            readXsd2Txt(xsdName);
        }
    }

    @Test
    public void testMx2MtMap() throws IOException {
        File file = FileUtils.toFile(Mx2MtTest.class.getResource("/swiftXml/MxPacs00900108_COV.xml"));
        String xmlStr = FileUtils.readFileToString(file);
        Map<String, String> maps = SwiftTransfer.mx2MtMap(xmlStr, null);
        for (String key : maps.keySet()) {
            System.out.println(key + "=" + maps.get(key));
        }
    }
}