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

export interface InviteRequest extends ProtocolRequest {
  // 0. 대화방SEQ(s)
  roomSeq: string;
  // 1n. 초대자 userSeq(n)...
  inviteUserSeqs: number[];
}

export interface InviteResponse extends ProtocolResponse {
  // 0. 대화방SEQ(s)
  roomSeq: string;
}
export interface InviteNotification extends ProtocolNotification {
  // 0. 대화방SEQ(s)
  roomSeq: string;
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.roomSeq });
  for (const userSeq of req.inviteUserSeqs) {
    bodyList.push({ type: PacketBodyValue.Integer, value: userSeq });
  }

  return bodyList;
};

export const decodeInvite: ProtocolDecoder<InviteResponse> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    roomSeq: message.bodyList[0]
  } as InviteResponse);
};

export const decodeInviteNotification: ProtocolDecoder<InviteNotification> = (
  message: ProtocolMessage
) => {
  return decodeProtocolMessage(message, {
    roomSeq: message.bodyList[0]
  } as InviteNotification);
};
