package com.its.op.xnetudp.protocol;

import com.its.op.dto.its.vms.VmsControlDto;
import com.its.utils.SysUtils;
import org.apache.commons.lang.StringUtils;

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

public class CENTER_VMS_REQ_OPER_MODE extends CENTER_VMS_REQ_COMMON {

	private byte Count;
	private Long vmsCtlrNmbr;
	private byte Mode;

	public CENTER_VMS_REQ_OPER_MODE(Long id, VmsControlDto.VmsControlOprModeReq req) {
		super(CENTER_COMM_DEFINE.INT_OP_VMS_MODE, 0x50, 0, id, req.getUserId(), null);
		this.Count = 1;
		this.vmsCtlrNmbr = id;
		if (StringUtils.equals("F", req.getOprMode())) {
			this.Mode = 'F';
		}
		else if (StringUtils.equals("B", req.getOprMode())) {
			this.Mode = 'B';
		}
		else {
			this.Mode = 'A';
		}
	}

	@Override
	public ByteBuffer getBuffer() {
		int pktSize = CENTER_COMM_DEFINE.INT_VMS_MAX_DATETIME + CENTER_COMM_DEFINE.INT_VMS_MAX_OPER_ID + 1 + 4 + 1;

		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];
		byte[] operIdArr = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_OPER_ID];

		SysUtils.copyStringToByteArray(cmdTimeArr, cmdTimeArr.length, getCmdTime());
		SysUtils.copyStringToByteArray(operIdArr, operIdArr.length, getOperId());

		byteBuffer.put(cmdTimeArr);
		byteBuffer.put(operIdArr);

		byteBuffer.put(this.Count);

		//byte[] vmsIdArr = new byte[CENTER_COMM_DEFINE.INT_VMS_MAX_ID];
		//SysUtils.copyStringToByteArray(vmsIdArr, vmsIdArr.length, String.valueOf(getVmsCtlrNmbr()));
		//byteBuffer.put(vmsIdArr);
		byteBuffer.putInt(this.vmsCtlrNmbr.intValue());
		byteBuffer.put(this.Mode);

		return byteBuffer;

	}

}
