package com.its.op.xnetudp.protocol;

import com.its.utils.SysUtils;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

@Slf4j
public class CENTER_COMM_MESSAGE {

	public static int CENTER_HEADER_SIZE = 10;
	
	private InetSocketAddress sender;
	
	private byte sendId;
	private byte recvId;
	private byte totalFrame;
	private byte currentFrame;
	private byte msgSeq;		// reserved
	private byte opCode;
	private short length;		// 2byte unsigned short
	
	private byte[] body;

	public CENTER_COMM_MESSAGE() {

	}
	public CENTER_COMM_MESSAGE(int recvId, int opCode, int length, byte msgSeq) {
		this.sendId       = (byte)CENTER_COMM_DEFINE.INT_ID_MAIN_OPER;
		this.recvId       = (byte)recvId;
		this.totalFrame   = 1;
		this.currentFrame = 1;
		this.msgSeq       = msgSeq;
		this.opCode       = (byte)opCode;
		this.length       = (short)length;
	}

	public CENTER_COMM_MESSAGE(byte[] buffer, InetSocketAddress inetSocketAddress) {

		this.sender = inetSocketAddress;

		ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

		this.sendId       = byteBuffer.get();
		this.recvId       = byteBuffer.get();
		this.totalFrame   = byteBuffer.get();
		this.currentFrame = byteBuffer.get();
		this.msgSeq       = byteBuffer.get();
		this.opCode       = byteBuffer.get();
		short length      = byteBuffer.getShort();
		this.length       = (short)(byteBuffer.array().length - 8);
		if (this.length > 0) {
			this.body = new byte[this.length];
			byteBuffer.get(this.body);

			if (length != this.length) {
				log.error("######################################################################");
				log.error("######################################################################");
				log.error("######################################################################, Length Error: {}, {}, {}", length, this.length, buffer.length);
				log.error("VMS PACKET: {}", SysUtils.byteArrayToHex(buffer));
			}
		}
	}

	public String getSenderIp() {
		return this.sender.getAddress().getHostAddress();
	}
	public int getSenderPort() {
		return this.sender.getPort();
	}
	
	public byte getOpCode() {
		return this.opCode;
	}
	public void setOpCode(byte opCode) {
		this.opCode = opCode;
	}
	
	public short getLength() {
		return this.length;
	}
	public void setLength(short length) {
		this.length = length;
	}
	public InetSocketAddress getSender() {
		return sender;
	}

	public void setSender(InetSocketAddress sender) {
		this.sender = sender;
	}

	public byte getSendId() {
		return sendId;
	}

	public void setSendId(byte sendId) {
		this.sendId = sendId;
	}

	public byte getRecvId() {
		return recvId;
	}

	public void setRecvId(byte recvId) {
		this.recvId = recvId;
	}

	public byte getTotalFrame() {
		return totalFrame;
	}

	public void setTotalFrame(byte totalFrame) {
		this.totalFrame = totalFrame;
	}

	public byte getCurrentFrame() {
		return currentFrame;
	}

	public void setCurrentFrame(byte currentFrame) {
		this.currentFrame = currentFrame;
	}

	public byte getMsgSeq() {
		return msgSeq;
	}

	public void setMsgSeq(byte msgSeq) {
		this.msgSeq = msgSeq;
	}

	public byte[] getBody() {
		return this.body;
	}
	public void setBody(byte[] body) {
		if (body != null && body.length != 0) {
			this.body = new byte[body.length];
			System.arraycopy(body, 0, this.body, 0, body.length);
		}
	}
	public byte[] getMessage() {
		return null;
	}

	public ByteBuffer getBuffer() {
		return null;
	}
}
