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

export interface RoomUpdateRequest extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string;
  // 대화방제목(s)
  roomName: string;
  // 알람여부(y)
  isAlarm: boolean;
  // 동기화여부(s)
  isSyncAll: boolean;
}

export interface RoomUpdateResponse extends ProtocolResponse {
  // 대화방SEQ(s)
  roomSeq: string;
  // 대화방제목(s)
  roomName: string;
  // 알람여부(y)
  isAlarm: boolean;
  // 동기화여부(s)
  isSyncAll: boolean;
}

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

  bodyList.push({ type: PacketBodyValue.String, value: req.roomSeq });
  bodyList.push({ type: PacketBodyValue.String, value: req.roomName });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.isAlarm ? 'Y' : 'N'
  });
  bodyList.push({
    type: PacketBodyValue.String,
    value: req.isSyncAll ? 'Y' : 'N'
  });
  return bodyList;
};

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

export interface RoomUpdateTimerSetRequest extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string;
  // 타이머시간(n)
  timerInterval: number;
}

export interface RoomUpdateTimerSetResponse extends ProtocolResponse {
  // 대화방SEQ(s)
  roomSeq: string;
  // 타이머시간(n)
  timerInterval: number;
}

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

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

export const decodeRoomUpdateTimerSet: ProtocolDecoder<
  RoomUpdateTimerSetResponse
> = (message: ProtocolMessage) => {
  return {
    roomSeq: message.bodyList[0],
    timerInterval: message.bodyList[1] || 0
  } as RoomUpdateTimerSetResponse;
};

export interface RoomUpdateFontRequest extends ProtocolRequest {
  // 대화방SEQ(s)
  roomSeq: string;
  // 폰트색(s) cf)0x000000 형태의 스트링
  fontColor: string;
  // sender사용자SEQ(n)
  senderSeq: number;
}

export interface RoomUpdateFontResponse extends ProtocolResponse {
  // 대화방SEQ(s)
  roomSeq: string;
  // 폰트색(s) cf)0x000000 형태의 스트링
  fontColor: string;
  // sender사용자SEQ(n)
  senderSeq: number;
}

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

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

export const decodeRoomUpdateFont: ProtocolDecoder<RoomUpdateFontResponse> = (
  message: ProtocolMessage
) => {
  return {
    roomSeq: message.bodyList[0],
    fontColor: message.bodyList[1],
    senderSeq: message.bodyList[2]
  } as RoomUpdateFontResponse;
};

export const decodeRoomUpdateFontNotification: ProtocolDecoder<
  RoomUpdateFontResponse
> = (message: ProtocolMessage) => {
  return {
    roomSeq: message.bodyList[0],
    fontColor: message.bodyList[1],
    senderSeq: message.bodyList[2]
  } as RoomUpdateFontResponse;
};
