package com.its.dsrc.xnetudp.protocol;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
@ToString
public class CENTER_DSRC_RES_STATE extends CENTER_COMM_MESSAGE {

	public static int STATE_RES_HEAD_SIZE = 9;

	private short   total;                        /* 전체 DSRC 갯수 */
	private short   error;                        /* 통신오류 DSRC */
	private short   normal;                       /* 정상 DSRC */
	private short   module;                       /* 모듈이상 DSRC */
	//private byte	count;                        /* 대상 DSRC 개수 */
	private List<voRseState> objList = null;

	public CENTER_DSRC_RES_STATE(byte msgSeq) {

		super(CENTER_COMM_DEFINE.INT_ID_DSRC_SERVER, CENTER_COMM_DEFINE.INT_OP_DSRC_STATE_RES, 0, msgSeq);

		if (objList == null) {
			objList = new ArrayList<voRseState>();
		}
	}

	@Override
	public ByteBuffer getBuffer() {
		int total = objList.size();
		if (total > CENTER_COMM_DEFINE.INT_DSRC_MAX_STATE) {
			total = CENTER_COMM_DEFINE.INT_DSRC_MAX_STATE;
		}
		int pktSize = (total* voRseState.STATE_SIZE)+STATE_RES_HEAD_SIZE;
		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((short)getLength());

		byteBuffer.putShort(this.total);
		byteBuffer.putShort(this.error);
		byteBuffer.putShort(this.normal);
		byteBuffer.putShort(this.module);
		byteBuffer.put((byte)this.objList.size());

		int cnt = 0;
		for (voRseState vo : this.objList) {
			byteBuffer.putInt(Integer.parseInt(vo.getCTLR_NMBR()));
			byteBuffer.put(vo.getComm());
			byteBuffer.put(vo.getState1());
			byteBuffer.put(vo.getState2());
			byteBuffer.put(vo.getState3());
			byteBuffer.put(vo.getState4());
			byteBuffer.put(vo.getState5());
			byteBuffer.put(vo.getState6());
			byteBuffer.put(vo.getState7());
			cnt++;
			if (cnt >= CENTER_COMM_DEFINE.INT_DSRC_MAX_STATE) {
				break;
			}
		}

		return byteBuffer;
		//return byteBuffer.array();
	}

}
