package com.its.op.xnetudp.protocol;

import com.its.utils.SysUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@Data
@EqualsAndHashCode(callSuper=false)
public class CENTER_VMS_REQ_COMMON extends CENTER_VMS_COMM_MESSAGE {

	private String cmdTime;
	private String operId;
	private Long vmsCtlrNmbr;
	private Byte cmd;

	public CENTER_VMS_REQ_COMMON(int opCode, int msgSeq, int length, Long vmsCtlrNmbr, String operId, Byte cmd) {
		super(CENTER_COMM_DEFINE.INT_ID_VMS_SERVER, opCode, length, (byte)msgSeq);
		this.cmdTime = SysUtils.getSysTime();
		this.operId = operId;
		this.vmsCtlrNmbr = vmsCtlrNmbr;
		this.cmd = cmd;
	}

	@Override
	public ByteBuffer getBuffer() {
		int pktSize = CENTER_COMM_DEFINE.INT_VMS_MAX_DATETIME + CENTER_COMM_DEFINE.INT_VMS_MAX_OPER_ID + 4;
		if (null != this.cmd) {
			pktSize++;
		}

		setLength(pktSize);
		ByteBuffer byteBuffer = ByteBuffer.allocate(CENTER_HEADER_SIZE + getLength());
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

		byteBuffer.put(getSendId());
		byteBuffer.put(getRecvId());
		byteBuffer.put(getTotalFrame());
		byteBuffer.put(getCurrentFrame());
		byteBuffer.put(getMsgSeq());
		byteBuffer.put(getOpCode());
		byteBuffer.putShort(getLength());
		byte[] cmdTimeArr = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_DATETIME];
		SysUtils.copyStringToByteArray(cmdTimeArr, cmdTimeArr.length, this.cmdTime);
		byteBuffer.put(cmdTimeArr);

		byte[] operIdArr = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_OPER_ID];
		SysUtils.copyStringToByteArray(operIdArr, operIdArr.length, this.operId);
		byteBuffer.put(operIdArr);

		//byte[] vmsIdArr = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_ID];
		//SysUtils.copyStringToByteArray(vmsIdArr, vmsIdArr.length, String.valueOf(this.vmsCtlrNmbr));
		byteBuffer.putInt(this.vmsCtlrNmbr.intValue());
		if (null != this.cmd) {
			byteBuffer.put(this.cmd);
		}
		return byteBuffer;

	}


}
