package com.brilliance.isc.convert.other;

import com.brilliance.isc.swift.objmtconvert.manager.MtCreatorManager;
import com.brilliance.isc.swift.objmtconvert.result.Mt2ObjResult;
import com.prowidesoftware.swift.model.SwiftBlock3;
import com.prowidesoftware.swift.model.Tag;
import com.prowidesoftware.swift.model.field.Field;
import com.prowidesoftware.swift.model.field.Field61;
import com.prowidesoftware.swift.model.mt.AbstractMT;
import com.prowidesoftware.swift.model.mt.mt1xx.MT103;
import com.prowidesoftware.swift.model.mt.mt2xx.MT201;
import com.prowidesoftware.swift.model.mt.mt2xx.MT202;
import com.prowidesoftware.swift.model.mt.mt2xx.MT202COV;
import com.prowidesoftware.swift.model.mt.mt7xx.MT760;
import com.prowidesoftware.swift.model.mt.mt9xx.MT950;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author gechengyang
 * @date 2023/3/2
 */
public class Mt2ObjectTest {

    @Test
    public  void test_mt201(){
        try {
            File file = FileUtils.toFile(Mt2ObjectTest.class.getResource("/mt/mt201.sf2"));
            String mtStr= FileUtils.readFileToString(file);
            Mt2ObjResult mt2ObjResult = MtCreatorManager.unPackMT(mtStr);
            List<Tag> tags = mt2ObjResult.getAbstractMT().getSwiftMessage().getBlock4().getTags();

            MT201 mt201= (MT201) mt2ObjResult.getAbstractMT();
            System.out.println(mt201.getField72());

           // mt201.getSequenceList()

            System.out.println(mt2ObjResult);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public  void test_mt760(){


        try {
            File file = FileUtils.toFile(Mt2ObjectTest.class.getResource("/mt/mt760.sf2"));
            String mtStr= FileUtils.readFileToString(file);
//            MT760 mt760= (MT760) AbstractMT.parse(mtStr);
//            List<Field> fields = mt760.getFields();
//            for (Field field:fields) {
//                System.out.println(field.getName()+":"+field.getValue());
//            }
            Mt2ObjResult mt2ObjResult = MtCreatorManager.unPackMT(mtStr);
            List<Tag> tags = mt2ObjResult.getAbstractMT().getSwiftMessage().getBlock4().getTags();
            MT760 mt760= (MT760) mt2ObjResult.getAbstractMT();
            List<Field> fields = mt760.getFields();
            for (Field field:fields) {
                System.out.println(field.getName()+":"+field.getValue());
            }

            // mt201.getSequenceList()

         //   System.out.println(mt2ObjResult);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public  void test_mt103(){

        try {
            File file = FileUtils.toFile(Mt2ObjectTest.class.getResource("/mt/mt103.sf2"));
            String mt103Str= FileUtils.readFileToString(file);
            AbstractMT abstractMT = AbstractMT.parse(mt103Str);
            MT103 mt103= (MT103) abstractMT;
            System.out.println("20*******"+mt103.getField20().getValue());
            System.out.println("50K account*****"+mt103.getField50K().getAccount());
            System.out.println("50K nameAddress*****"+mt103.getField50K().getNameAndAddress());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Test
    public  void test_mt950(){


        try {
            File file = FileUtils.toFile(Mt2ObjectTest.class.getResource("/mt/mt950.sf2"));
            String mtStr= FileUtils.readFileToString(file);
            Mt2ObjResult mt2ObjResult = MtCreatorManager.unPackMT(mtStr);
            Field61 field61 = ((MT950) mt2ObjResult.getAbstractMT()).getField61().get(0);
            System.out.println(field61);
            Tag tag = mt2ObjResult.getAbstractMT().getSwiftMessage().getBlock4().getTagByName("1111");
            Tag[] tags61 = mt2ObjResult.getAbstractMT().getSwiftMessage().getBlock4().getTagsByName("61");
            for (Tag tempTag:tags61) {
                System.out.println(tempTag.getValue());
            }
            System.out.println(mt2ObjResult);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public  void test_mt202COV(){
        try {
            File file = FileUtils.toFile(Mt2ObjectTest.class.getResource("/mt/mt202cov.sf2"));
            String mt202covStr= FileUtils.readFileToString(file);
            AbstractMT abstractMT = AbstractMT.parse(mt202covStr);
            MT202COV mt202COV= (MT202COV) abstractMT;
            System.out.println(abstractMT.getMessageType());
            SwiftBlock3 block3 = mt202COV.getSwiftMessage().getBlock3();
            Map<String, String> tagMap = block3.getTagMap();
            //判断为COV报文
            if(tagMap.containsKey("119")&&"COV".equals(tagMap.get("119")))
            {
                System.out.println(111);
            }
            Mt2ObjResult mt2ObjResult = MtCreatorManager.unPackMT(mt202covStr);
            System.out.println(mt2ObjResult.getMsgType());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Test
    public  void test_mt202(){
        try {
            File file = FileUtils.toFile(Mt2ObjectTest.class.getResource("/mt/mt202.sf2"));
            String mt202Str= FileUtils.readFileToString(file);
            AbstractMT abstractMT = AbstractMT.parse(mt202Str);
            MT202 mt202= (MT202) abstractMT;
            System.out.println(abstractMT.getMessageType());
            SwiftBlock3 block3 = mt202.getSwiftMessage().getBlock3();
            Map<String, String> tagMap = block3.getTagMap();
            if(tagMap.containsKey("119")&&"COV".equals(tagMap.get("119")))
            {
                System.out.println(111);
            }
            System.out.println(mt202.toJson());

            Mt2ObjResult mt2ObjResult = MtCreatorManager.unPackMT(mt202Str);
            System.out.println(mt2ObjResult);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
