import {
  ProtocolRequest,
  ProtocolEncoder,
  PacketBody,
  ProtocolDecoder,
  ProtocolMessage,
  PacketBodyValue,
  ProtocolResponse,
  ProtocolNotification,
  decodeProtocolMessage
} from '@ucap-webmessenger/protocol';

export interface ReadRequest extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string;
  // LR이벤트SEQ(n)
  lastReadSeq: number;
}
export interface ReadResponse extends ProtocolResponse {
  // 대화방SEQ(s)
  roomSeq: string;
  // LR이벤트SEQ(n)
  lastReadSeq: number;
}
export interface ReadNotification extends ProtocolNotification {
  // 대화방SEQ(s)
  roomSeq: string;
  // LR이벤트SEQ(n)
  lastReadSeq: number;
}

export const encodeRead: ProtocolEncoder<ReadRequest> = (req: ReadRequest) => {
  const bodyList: PacketBody[] = [];

  bodyList.push(
    { type: PacketBodyValue.String, value: req.roomSeq },
    { type: PacketBodyValue.Integer, value: req.lastReadSeq }
  );
  return bodyList;
};
export const decodeRead: ProtocolDecoder<ReadResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    roomSeq: message.bodyList[0],
    lastReadSeq: message.bodyList[1]
  } as ReadResponse);
};
export const decodeReadNotification: ProtocolDecoder<ReadNotification> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    roomSeq: message.bodyList[0],
    lastReadSeq: message.bodyList[1]
  } as ReadNotification);
};
