package com.brilliance.isc.convert.other;

import com.brilliance.isc.swift.objmtconvert.annotation.TagName;
import com.brilliance.isc.swift.objmtconvert.manager.MtCreatorManager;
import com.brilliance.isc.swift.objmtconvert.model.commontag.ActnoBicAdrTagXXX;
import com.brilliance.isc.swift.objmtconvert.model.commontag.DcDateCurAmtTagXXX;
import com.brilliance.isc.swift.objmtconvert.model.commontag.TagResult;
import com.brilliance.isc.swift.objmtconvert.result.MtResult;
import com.brilliance.isc.swift.objmtconvert.util.Constans;
import com.brilliance.isc.swift.objmtconvert.util.MtValidatorUtil;
import com.brilliance.isc.swift.swifteditor.tag.TagFormat;
import com.brilliance.isc.swift.swifteditor.tag.TagLine;
import com.google.gson.Gson;
import com.prowidesoftware.swift.model.Tag;
import com.prowidesoftware.swift.model.mt.mt7xx.MT760;
import org.junit.Test;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author gechengyang
 * @date 2023/2/14
 */
public class MethodTest {

    @Test
    public  void test()
    {
        String str = "love234csdnjava";
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        String result = m.replaceAll("").trim();
        System.out.println(result);
        System.out.println(str);
    }

    @Test
    public  void testActnoBicAdr1()
    {
        ActnoBicAdrTagXXX actnoBicAdrTag50X = new ActnoBicAdrTagXXX("52");
       // actnoBicAdrTag50X.setBic("PSBCCNBJXXX");
        actnoBicAdrTag50X.setAcctno("62260000116226");
        actnoBicAdrTag50X.setPartyIdentifier("CITYBJ");
       // actnoBicAdrTag50X.setLocation("TESTLOCATION");
        actnoBicAdrTag50X.setAddress("AAAA" + Constans.CRLF + "BBBB");
        TagResult tagResult = actnoBicAdrTag50X.getTagResult();
        System.out.println(tagResult.getTagBefore()+tagResult.getTagEnd());
        System.out.println(tagResult.getValue());
        System.out.println(ActnoBicAdrTagXXX.aDrIsTagOfFLAG_F("1/abc\r\n2/ddd"));
    }


    @Test
    public  void testActnoBicAdr2_tag4XX()
    {
        ActnoBicAdrTagXXX actnoBicAdrTag41X = new ActnoBicAdrTagXXX("41");
        // actnoBicAdrTag50X.setBic("PSBCCNBJXXX");
        actnoBicAdrTag41X.setAcctno("62260000116226");
        actnoBicAdrTag41X.setPartyIdentifier("CITYBJ");
        // actnoBicAdrTag50X.setLocation("TESTLOCATION");
        actnoBicAdrTag41X.setAddress("AAAA" + Constans.CRLF + "BBBB");
        TagResult tagResult = actnoBicAdrTag41X.getTagResult();
        System.out.println(tagResult.getTagBefore()+tagResult.getTagEnd());
        System.out.println(tagResult.getValue());
        System.out.println(ActnoBicAdrTagXXX.aDrIsTagOfFLAG_F("1/abc\r\n2/ddd"));

        List list=new LinkedList<>();
        Map map=new LinkedHashMap();
        //Map map=new HashMap();
        map.put("A","2");
        map.put("B","1");
        map.put("1","1");
        map.put("C","3");
        list.add(map);
        System.out.println(list);
    }


    @Test
    public  void testActnoBicAdr2_tag25()
    {
        ActnoBicAdrTagXXX actnoBicAdrTag41X = new ActnoBicAdrTagXXX("25");
        actnoBicAdrTag41X.setAddress("AAAA" + Constans.CRLF + "BBBB");
        actnoBicAdrTag41X.setBic("PSBCCNBJ");
        TagResult tagResult = actnoBicAdrTag41X.getTagResult();
        System.out.println(tagResult.getTagBefore()+tagResult.getTagEnd());
        System.out.println(tagResult.getValue());
        System.out.println(ActnoBicAdrTagXXX.aDrIsTagOfFLAG_F("1/abc\r\n2/ddd"));
    }
    @Test
    public  void testDcDateCurAmtTagXXX()
    {
        DcDateCurAmtTagXXX dcDateCurAmtTagXXX = new DcDateCurAmtTagXXX("32");
//        dcDateCurAmtTagXXX.setDate("220608");
        dcDateCurAmtTagXXX.setDayMonth("M");
        dcDateCurAmtTagXXX.setNumberOfDaysMonths("001");
        dcDateCurAmtTagXXX.setCode("ID");
        dcDateCurAmtTagXXX.setCurrency("EUR");
        dcDateCurAmtTagXXX.setAmount(new BigDecimal("111.1"));
        TagResult tagResult = dcDateCurAmtTagXXX.getTagResult();
        System.out.println(tagResult.getTagBefore()+tagResult.getTagEnd());
        System.out.println(tagResult.getValue());
    }


    @Test
    public  void testDcDateCurAmtTag24G()
    {
        ActnoBicAdrTagXXX actnoBicAdrTag24G = new ActnoBicAdrTagXXX("24G");
        actnoBicAdrTag24G.setAddress("AAAA" + Constans.CRLF + "BBBB");
        actnoBicAdrTag24G.setCode("CHINA");
        TagResult tagResult = actnoBicAdrTag24G.getTagResult();
        System.out.println(tagResult.getTagBefore()+tagResult.getTagEnd());
        System.out.println(tagResult.getValue());
    }



    @Test
    public  void testRegex()
    {
        String pattern="16x";
        TagLine tagLine= TagFormat.WalkTagCell("20",pattern);
        //获取swift格式对应的正则表达式
        System.out.println(tagLine.getAnalyzeReg());
        String str20="QAA123";
        System.out.println(str20.matches(tagLine.getAnalyzeReg()));
//CRLF SWIFTBIC AMOUNT NUMBER HHMM TIME2 TIME3 OFFSET SIGN DATE2 DATE4 YEAR DATE4 CUR DC
        // MT SB-LC  DM CC FUNC
        pattern="<DATE2><CUR><AMOUNT>15";
        tagLine= TagFormat.WalkTagCell("32A",pattern);
        //获取swift格式对应的正则表达式
        System.out.println(tagLine.getAnalyzeReg());
        str20="230301CNY1000.01";

        String str="(?<S1>\\d{6})(?<S2>[A-Z]{3})(?<S3>[\\d,.]{1,15})";
        System.out.println("HELL".matches(str));
        Pattern compile = Pattern.compile(str);
        Matcher matcher = compile.matcher(str20);
        while (matcher.find()){
            String group0 = matcher.group(0);
            String group1 = matcher.group(1);
            String group2 = matcher.group(2);
            String group3 = matcher.group(3);

            boolean matches = group1.matches("\\d{6}");
            String str2=matcher.group("S2");
            String str3=matcher.group("S3");

         // System.out.println(str1.matches(group1));


            System.out.println("wc="+matcher.group("S1"));
            System.out.println("wc="+matcher.group("S2"));
            System.out.println("wc="+matcher.group("S3"));
        }

    }

    @Test
    public void test_getSupperClass() throws Exception {
        TestC bean = new TestC();
        bean.setArgC("CCC");

        Class<?> clazz = bean.getClass();

        List<Field> list=new ArrayList<>();
        Field[] field = clazz.getDeclaredFields();
        for (Field tempField:field)
        {
            list.add(tempField);
        }
        while(true)
        {
           // System.out.println(field[0].getName());
            clazz=clazz.getSuperclass();
            if(clazz!=null&&clazz != Object.class){
                field = clazz.getDeclaredFields();
                //System.out.println(field[0].getName());
                List<Field> l= Arrays.asList(field);
                for (Field tempField:l)
                {
                    list.add(tempField);
                }
            }
            else {
                break;
            }
        }
        for (Field f : list) {
            f.setAccessible(true);
            //f.get(bean).toString()
            System.out.println("Atrribute：" + f.getName() );
            System.out.println(f.getAnnotation(TagName.class).value()[0]);
        }

    }


    @Test
    public void testEncoding() throws UnsupportedEncodingException {
        String str="abc123*%.()";
        byte[] b1=str.getBytes("gbk");
        byte[] b2=str.getBytes("utf-8");
       // System.out.println(b1==b2);
        for (byte b:b1)
        {
            System.out.println("b1=" + b);
        }
        for (byte b : b2) {
            System.out.println("b2=" + b);
        }

        System.out.println(b1.length);
        System.out.println(b2.length);
    }

    @Test
    public void testMT760() {
        MT760 mt760 = new MT760();
        mt760.set("15A", "15A");
        mt760.set("27", "1");
        mt760.set("22A", "abcd");
        mt760.set("72Z", "abc\r\nBJ\r\nHAIDIAN");
        mt760.set("23X", "1234");
        mt760.set("15B", "");
        mt760.set("20", "mt760REF001");
        mt760.set("30", "230307");
        mt760.set("40C", "040C");
        mt760.set("59A","PSBCCNBJ");
      //  System.out.println(mt760.message());

        List<Tag> tags = mt760.getSwiftMessage().getBlock4().getTags();

        for (Tag tag:tags)
        {
            System.out.println(tag.getName()+"----"+tag.getValue());
            if(tag.getName().equals("59"))
            {
                System.out.println("tag is 59");
            }else if(tag.getName().equals("59A")){
                System.out.println("tag is 59A");
            }
        }

    }


    @Test
    public void testBigDecimal(){
        BigDecimal bigDecimal=new BigDecimal(10);
        System.out.println(MtValidatorUtil.changeSymbol(bigDecimal));
    }


    @Test
    public void testLanguage(){
        Locale [] localses=Locale.
                getAvailableLocales();
        for (Locale locale : localses) {
            //国家-->国家CN
            if("CN".equals(locale.getCountry()))
            {
                System.out.println(111);
            }
            System.out.println(
                    locale.getDisplayCountry()+
                            "-->"+locale.getCountry());
            //语句--->国家的语言 zh
            System.out.println(
                    locale.getDisplayLanguage()+
                            "----->"+locale.getLanguage());
        }
    }


    @Test
    public void testJson2Mt(){
        Map map=new HashMap();
        map.put("test","test");
        MtResult mtResult = MtCreatorManager.packMt(new Gson().toJson(map));
        System.out.println(mtResult);
    }
}

