package org.example.utils;

import org.jpos.iso.ISOUtil;

import java.util.Map;
import java.util.Optional;

public class YcsUtils {
    static Map<String, String> RESPONSE_MTI = Map.of("0200", "0210");

    public static byte[] getPacketLength(int length){
        String pktLength = "";
        if(length < 10){
            pktLength = "000"  + length ;
        }else if(length < 100){
            pktLength = "00"  + length ;
        }else if (length < 1000) {
            pktLength = "0"  + length ;
        }else{
            pktLength = ""  + length ;
        }

        byte[] lengthArray = ISOUtil.str2bcd(pktLength, true);
        return lengthArray;
    }

    public static byte[] concatenate(byte[]... arrays) {
        // Calculate total length
        int totalLength = 0;
        for (byte[] array : arrays) {
            totalLength += array.length;
        }

        // Allocate new array and copy each one in
        byte[] result = new byte[totalLength];
        int currentPos = 0;
        for (byte[] array : arrays) {
            System.arraycopy(array, 0, result, currentPos, array.length);
            currentPos += array.length;
        }

        return result;
    }

    public String getResponseMti(String mti) {
        return Optional.ofNullable(RESPONSE_MTI.get(mti)).orElse("0810");
    }
}
