package com.its.pis.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_VDS_RES_STATE extends CENTER_COMM_MESSAGE {

	public static int STATE_RES_HEAD_SIZE = 9;

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

	public CENTER_VDS_RES_STATE(byte msgSeq) {

		super(CENTER_COMM_DEFINE.INT_ID_VDS_SERVER, CENTER_COMM_DEFINE.INT_OP_VDS_STATE_RES, 0, msgSeq);

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

	@Override
	public ByteBuffer getBuffer() {
		int total = objList.size();
		if (total > CENTER_COMM_DEFINE.INT_VDS_MAX_STATE) {
			total = CENTER_COMM_DEFINE.INT_VDS_MAX_STATE;
		}
		int pktSize = (total*voVdsState.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 (voVdsState vo : this.objList) {
			byteBuffer.putInt(Integer.parseInt(vo.getCTLR_NMBR()));
			byteBuffer.put(vo.getComm());

			byteBuffer.put(vo.getVideoInput());
			byteBuffer.put(vo.getHeater());
			byteBuffer.put(vo.getFan());
			byteBuffer.put(vo.getBackDoorOpen());
			byteBuffer.put(vo.getFrontDoorOpen());
			byteBuffer.putShort(vo.getTemperature());
			byteBuffer.put(vo.getFanMode());
			byteBuffer.put(vo.getFanRunTemp());
			byteBuffer.put(vo.getHetrMode());
			byteBuffer.put(vo.getHetrRunTemp());
			cnt++;
			if (cnt >= CENTER_COMM_DEFINE.INT_DSRC_MAX_STATE) {
				break;
			}
		}

		return byteBuffer;
	}

}
