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

export interface RoomOpenRequest extends ProtocolRequest {
  // 0. 구분자
  divCd: string;
  // 1n. 초대자 userSeq(n)...
  userSeqs: number[];
}

export interface RoomOpenResponse extends ProtocolResponse {
  // 0. 구분자
  divCd: string;
  // 1. 대화방SEQ(s)
  roomSeq: string;
  // 2.	신규여부(y)
  newRoom: boolean;
}

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

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

  return bodyList;
};

export const decodeRoomOpen: ProtocolDecoder<RoomOpenResponse> = (
  message: ProtocolMessage
) => {
  return {
    divCd: message.bodyList[0],
    roomSeq: message.bodyList[1],
    newRoom: message.bodyList[2] === 'Y' ? true : false
  } as RoomOpenResponse;
};

export interface RoomOpen2Request extends ProtocolRequest {
  // 0. 구분자
  divCd: string;
  // 1. 대화방제목(s)
  roomName: string;
  // 2n. 초대자 userSeq(n)...
  userSeqs: number[];
}

export interface RoomOpen2Response extends ProtocolResponse {
  // 0. 구분자
  divCd: string;
  // 1. 대화방제목(s)
  roomName: string;
  // 2. 대화방SEQ(s)
  roomSeq: string;
  // 3.	신규여부(y)
  newRoom: boolean;
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.divCd });
  bodyList.push({ type: PacketBodyValue.String, value: req.roomName.trim() });
  for (const userSeq of req.userSeqs) {
    bodyList.push({ type: PacketBodyValue.Integer, value: userSeq });
  }

  return bodyList;
};

export const decodeRoomOpen2: ProtocolDecoder<RoomOpen2Response> = (
  message: ProtocolMessage
) => {
  return {
    divCd: message.bodyList[0],
    roomName: message.bodyList[1],
    roomSeq: message.bodyList[2],
    newRoom: message.bodyList[3] === 'Y' ? true : false
  } as RoomOpen2Response;
};

export interface RoomOpen3Request extends ProtocolRequest {
  // 0. 구분자
  divCd: string;
  // 1. 대화방제목(s)
  roomName: string;
  // 2. 타이머대화방여부YN(s)
  isTimerRoom: boolean;
  // 3. 타이머시간(n)
  timerRoomInterval: number;
  // 4n. 초대자 userSeq(n)...
  userSeqs: number[];
}

export interface RoomOpen3Response extends ProtocolResponse {
  // 0. 구분자
  divCd: string;
  // 1. 대화방제목(s)
  roomName: string;
  // 2. 대화방SEQ(s)
  roomSeq: string;
  // 3.	신규여부(y)
  newRoom: boolean;
  // 4. 타이머대화방여부YN(s)
  isTimerRoom: boolean;
  // 5. 타이머시간(n)
  timerRoomInterval: number;
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.divCd });
  bodyList.push({ type: PacketBodyValue.String, value: req.roomName.trim() });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.isTimerRoom === true ? 'Y' : 'N'
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.isTimerRoom !== true ? 0 : req.timerRoomInterval
  });
  for (const userSeq of req.userSeqs) {
    bodyList.push({ type: PacketBodyValue.Integer, value: userSeq });
  }

  return bodyList;
};

export const decodeRoomOpen3: ProtocolDecoder<RoomOpen3Response> = (
  message: ProtocolMessage
) => {
  return {
    divCd: message.bodyList[0],
    roomName: message.bodyList[1],
    roomSeq: message.bodyList[2],
    newRoom: message.bodyList[3] === 'Y' ? true : false,
    isTimerRoom: message.bodyList[4] === 'Y' ? true : false,
    timerRoomInterval: message.bodyList[5] || 0
  } as RoomOpen3Response;
};

export interface RoomOpen4Request extends ProtocolRequest {
  // 0. 구분자
  divCd: string;
  // 1. 대화방제목(s)
  roomName: string;
  // 2. 타이머대화방여부YN(s)
  isTimerRoom: boolean;
  // 3. 타이머시간(n)
  timerRoomInterval: number;
  // 4n. 초대자 userSeq(n)...
  userSeqs: number[];
}

export interface RoomOpen4Response extends ProtocolResponse {
  // 0. 구분자
  divCd: string;
  // 1. 대화방제목(s)
  roomName: string;
  // 2. 대화방SEQ(s)
  roomSeq: string;
  // 3.	신규여부(y)
  newRoom: boolean;
  // 4. 타이머대화방여부YN(s)
  isTimerRoom: boolean;
  // 5. 타이머시간(n)
  timerRoomInterval: number;
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.divCd });
  bodyList.push({ type: PacketBodyValue.String, value: req.roomName.trim() });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.isTimerRoom === true ? 'Y' : 'N'
  });
  bodyList.push({
    type: PacketBodyValue.Integer,
    value: req.isTimerRoom !== true ? 0 : req.timerRoomInterval
  });
  for (const userSeq of req.userSeqs) {
    bodyList.push({ type: PacketBodyValue.Integer, value: userSeq });
  }

  return bodyList;
};

export const decodeRoomOpen4: ProtocolDecoder<RoomOpen4Response> = (
  message: ProtocolMessage
) => {
  return {
    divCd: message.bodyList[0],
    roomName: message.bodyList[1],
    roomSeq: message.bodyList[2],
    newRoom: message.bodyList[3] === 'Y' ? true : false,
    isTimerRoom: message.bodyList[4] === 'Y' ? true : false,
    timerRoomInterval:
      message.bodyList[4] !== 'Y' ? 0 : message.bodyList[5] || 0
  } as RoomOpen4Response;
};
