package org.jpos.tcpay.connection;

import org.jpos.iso.ISOMsg;
import org.jpos.iso.ISOUtil;
import org.jpos.tcpay.acquirer.LengthMode;
import org.jpos.tcpay.db.entity.AcquirerConnection;

import java.net.SocketTimeoutException;

public interface AcquirerChannel {

    ISOMsg transceive(ISOMsg req, AcquirerConnection acquirerConnection) throws SocketTimeoutException;

    default byte[] getLengthByte(int length, LengthMode lengthMode) {
        if (LengthMode.HEX.equals(lengthMode)) {
            byte[] lengthByte = new byte[2];
            lengthByte[0] = (byte)(length/256);
            lengthByte[1] = (byte)(length%255);

            return lengthByte;
        } else if (LengthMode.BCD.equals(lengthMode)){
            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 ;
            }

            return ISOUtil.str2bcd(pktLength, true);
        } else {
            return null;
        }
    }

}
