package com.its.app;

import com.its.app.utils.SysUtils;
import com.its.vms.service.VmsFormService;
import com.its.vms.service.VmsSymbService;
import com.its.vms.xnettcp.vms.protocol.enums.eVmsOpCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;

import java.io.UnsupportedEncodingException;

@Slf4j
//@SpringBootTest(classes = VmsCommServerApplication.class)
public class VmsCommServerApplicationTests {

    @Test
    void test1() {
        //                 0123 45 67 89 0123
        String testData = "20230102034000";
        log.info("{}", testData.length());
        String sFrom = testData.substring(4,6) + "월" + testData.substring(6,8) + "일 " + testData.substring(8,10) + "시";
        log.info("{}", sFrom);
    }

    @Test
    void test2() {
        String testData = " ";
        log.info("{}", StringUtils.isEmpty(StringUtils.trim(testData)));
    }

    @Test
    void test3() {
        byte b = (byte)0xFF;
        eVmsOpCode opCode = eVmsOpCode.getValue(b);
        log.info("{}, {}", b, opCode);
    }

    @Test
    void crc() {
        //byte[] data = {(byte)0x05, (byte)0x9A, (byte)0x01, (byte)0x01, (byte)0x1A, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x23, (byte)0x28, (byte)0x00, (byte)0x01, (byte)0x23, (byte)0x28, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x02, (byte)0x00, (byte)0x07, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x20, (byte)0x00, (byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x0B};
        byte[] data = {(byte)0x10, (byte)0x02,
                (byte)0x00, (byte)0x05, (byte)0x9A, (byte)0x01, (byte)0x01, (byte)0x1A, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x23,
                (byte)0x28, (byte)0x00, (byte)0x01, (byte)0x23, (byte)0x28, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x02,
                (byte)0x00, (byte)0x07, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x20,
                (byte)0x00, (byte)0xA0, (byte)0x00, (byte)0x00, (byte)0x0B,
                (byte)0x10, (byte)0x03, (byte)0x00, (byte)0x04};
//        int arc = Crc16.ARC(data, 2, data.length - 6);
//        log.info("ARC: {}, {}", arc, String.format("%X", arc));
//        int arc1 = VmsDleFramePacket.getCRC16ARC(data, 2, data.length - 6);
//        log.info("ARC: {}, {}", arc1, String.format("%X", arc1));
    }

    @Test
    void hangul() {
        String str = "30km 이하 서행!";
        //default charset 으로 인코딩된 바이트 배열
        byte[] bytes = str.getBytes();
        //인코딩된 바이트 출력

        System.out.print("Default charset encoding: ");
        for(int i=0;i<bytes.length;i++)
            System.out.print(bytes[i]+" ");
        System.out.println();

        //default charset 으로 디코딩된 문자열 출력
        String decoded = new String(bytes);
        System.out.println(decoded);

        System.out.println();
        try {
            //UTF-8로 인코딩된 바이트 배열
            bytes = str.getBytes("UTF-8");
            System.out.print("UTF-8 charset encoding: ");
            for(int i=0;i<bytes.length;i++)
                System.out.print(bytes[i]+" ");
            System.out.println();
            //이 바이트 배열을 default charset 으로 디코딩된 문자열 출력 : charset 이 다르므로 한글이 깨짐.
            decoded = new String(bytes);
            System.out.println(decoded);
            //인코딩된 UTF-8로 디코딩되어 한글이 깨지지 않음.
            decoded = new String(bytes,"UTF-8");
            System.out.println(decoded);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Test
    void hangul2() {
        String str = "30km 이하 서행!";
        byte[] arr1 = str.getBytes();
        byte[] arr2 = SysUtils.stringToByteArr(str);
        log.error("{}, {}, {}", str.length(), arr1.length, arr2.length);
    }

    @Test
    void getDnldNmbr() {
        log.info("{}", VmsSymbService.getDnldSymbNmbr("60100"));
        log.info("{}", VmsSymbService.getDnldSymbNmbr("80061"));
        log.info("{}", VmsSymbService.getDnldSymbNmbr("80060"));
        log.info("{}", VmsSymbService.getDnldSymbNmbr("80021"));
        log.info("{}", VmsSymbService.getDnldSymbNmbr("8002"));

        log.info("{}", VmsFormService.getDnldFormNo(10013));
        log.info("{}", VmsFormService.getDnldFormNo(10054));
        log.info("{}", VmsFormService.getDnldFormNo(10186));
        log.info("{}", VmsFormService.getDnldFormNo(13));
    }
}
