import {
  ProtocolRequest,
  ProtocolResponse,
  ProtocolEncoder,
  PacketBody,
  PacketBodyValue,
  ProtocolDecoder,
  ProtocolMessage
} from '@ucap-webmessenger/protocol';
import { RoomExitType } from '../types/room.type';

export interface RoomExitRequest extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string;
}

export interface RoomExitResponse extends ProtocolResponse {
  // 대화방SEQ(s)
  roomSeq: string;
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.roomSeq });
  return bodyList;
};

export const decodeRoomExit: ProtocolDecoder<RoomExitResponse> = (
  message: ProtocolMessage
) => {
  return {
    roomSeq: message.bodyList[0]
  } as RoomExitResponse;
};
export const decodeRoomExitNotification: ProtocolDecoder<RoomExitResponse> = (
  message: ProtocolMessage
) => {
  return {
    roomSeq: message.bodyList[0]
  } as RoomExitResponse;
};

export interface RoomExitAllRequest extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string[];
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.roomSeq.join(',') });
  return bodyList;
};

export interface RoomExitForcingRequest extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string;
  // 강퇴요청타입(s)
  type: string;
  // sender사용자SEQ(n)
  senderSeq: number;
  // 사용자SEQ(n)...
  userSeqs: number[];
}

export interface RoomExitForcingResponse extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string;
  // 강퇴요청타입(s)
  type: RoomExitType;
  // sender사용자SEQ(n)
  senderSeq: number;
  // 사용자SEQ(n)...
  userSeqs: number[];
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.roomSeq });
  bodyList.push({ type: PacketBodyValue.String, value: req.type });
  bodyList.push({ type: PacketBodyValue.Integer, value: req.senderSeq });
  req.userSeqs.forEach(userSeq => {
    bodyList.push({ type: PacketBodyValue.Integer, value: userSeq });
  });
  return bodyList;
};

export const decodeRoomExitForcing: ProtocolDecoder<RoomExitForcingResponse> = (
  message: ProtocolMessage
) => {
  let userSeqs: number[] = [];
  if (message.bodyList.length > 3) {
    userSeqs = message.bodyList.slice(3);
  }
  return {
    roomSeq: message.bodyList[0],
    type: message.bodyList[1] as RoomExitType,
    senderSeq: message.bodyList[2],
    userSeqs
  } as RoomExitForcingResponse;
};
export const decodeRoomExitForcingNotification: ProtocolDecoder<
  RoomExitForcingResponse
> = (message: ProtocolMessage) => {
  let userSeqs: number[] = [];
  if (message.bodyList.length > 3) {
    userSeqs = message.bodyList.slice(3);
  }
  return {
    roomSeq: message.bodyList[0],
    type: message.bodyList[1] as RoomExitType,
    senderSeq: message.bodyList[2],
    userSeqs
  } as RoomExitForcingResponse;
};
