package com.tokheim.aifueling;

import cn.hutool.core.convert.Convert;
import com.tokheim.aifueling.utils.ByteArrayUtils;
import com.tokheim.aifueling.utils.SM4Utils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SpringBootTest
class AiFuelingApplicationTests {

    @Test
    void testSm4Encryption() {
        byte[] bytes = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,0x11,0x12};
        try {
            byte[] resultBytes = SM4Utils.encrypt(bytes);
            System.out.println(Convert.toHex(resultBytes));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    void testSm4Decryption() {
        byte[] bytes = {0x24,0x72,0x3f,0x04,0x24,0x1f, (byte) 0x88, (byte) 0xc4, (byte) 0xab,0x34,0x62, (byte) 0xf2,
                (byte) 0xbc, (byte) 0x83, (byte) 0xac, (byte) 0x99, (byte) 0xce, 0x34,0x73, (byte) 0x9b, (byte) 0xaa,
                0x72, (byte) 0xdf,0x77,0x68, (byte) 0xb0,0x35, (byte) 0xa7,0x2e,0x0e,0x57};

        try {
            byte[] resultbytes = SM4Utils.decrypt(bytes);
            System.out.println(Convert.toHex(resultbytes));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    void testFindSubIndex(){
        byte[] source = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
        byte[] target = {0x02,0x03};
        System.out.println(ByteArrayUtils.findSubarrayIndex(source,target));

        byte[] source2 = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
        byte[] target2 = {0x10,0x24};
        System.out.println(ByteArrayUtils.findSubarrayIndex(source2,target2));

        byte[] source3 = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09};
        byte[] target3 = {0x02,0x03};
        System.out.println(ByteArrayUtils.findSubarrayIndex(source3,target3));
    }

    @Test
    void testBytes2Hex(){
        byte[] bytes = {0x12,0x01,0x10, (byte) 0xaa, (byte) 0xff,0x00,0x1a,0x11, (byte) 0xab};
        String s = ByteArrayUtils.bytesToHexString(bytes);
        System.out.println(s);
    }

    @Test
    void testDecimalStrToBCD(){
        String amount1 = "123";
        String amount1_ = "1,23";
        String amount2 = "123.00";
        String amount2_ = "1,23.00";
        String amount3 = "123.000";
        String amount3_ = "1,23.000";
        System.out.println(amount1 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount1)));
        System.out.println(amount1_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount1_)));
        System.out.println(amount2 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount2)));
        System.out.println(amount2_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount2_)));
        System.out.println(amount3 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount3)));
        System.out.println(amount3_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount3_)));

        String amount4 = "999999";
        String amount4_ = "999,999";
        String amount5 = "999999.00";
        String amount5_ = "999,999.00";
        String amount6 = "999999.000";
        String amount6_ = "999,999.000";
        System.out.println(amount4 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount4)));
        System.out.println(amount4_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount4_)));
        System.out.println(amount5 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount5)));
        System.out.println(amount5_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount5_)));
        System.out.println(amount6 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount6)));
        System.out.println(amount6_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount6_)));


        String amount7 = "1000000";
        String amount7_ = "1,000,000";
        String amount8 = "1000000.00";
        String amount8_ = "1,000,000.00";
        String amount9 = "1000000.000";
        String amount9_ = "1,000,000.000";
        System.out.println(amount7 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount7)));
        System.out.println(amount7_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount7_)));
        System.out.println(amount8 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount8)));
        System.out.println(amount8_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount8_)));
        System.out.println(amount9 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount9)));
        System.out.println(amount9_ + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount9_)));

        String amount10 = "asdad";
        System.out.println(amount10 + "=>" + ByteArrayUtils.bytesToHexString(ByteArrayUtils.decimalStrToBCD(amount10)));
    }

    @Test
    void testInt2Bytes() {
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(0)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(1)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(9)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(10)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(11)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(99)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(100)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(101)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(4080)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(4081)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(4082)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(65534)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(65535)));
        System.out.println(ByteArrayUtils.bytesToHexString(ByteArrayUtils.intToTwoByteArray(65536)));
    }

}