import {
  APIRequest,
  MessageAPIResponse,
  APIDecoder,
  APIJsonEncoder
} from '@ucap-webmessenger/api';
import { DeviceType } from '@ucap-webmessenger/core';
import { MessageType } from '../types/message.type';
import { MessageDetailInfo } from '../models/message-list';
import { DetailContent } from '../models/detail-content';
import { DetailReceiver } from '../models/detail-receiver';

export interface DetailRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  tokenKey: string;
  type: MessageType;

  msgId: number;
}

export interface DetailResponse extends MessageAPIResponse {
  msgInfo: MessageDetailInfo;

  contents: DetailContent[];

  recvList: DetailReceiver[];
}

export const encodeDetail: APIJsonEncoder<DetailRequest> = (
  req: DetailRequest
) => {
  return JSON.stringify(req);
};

export const decodeDetail: APIDecoder<DetailResponse> = (res: any) => {
  let msgInfo: MessageDetailInfo = null;
  const contents: DetailContent[] = [];
  const recvList: DetailReceiver[] = [];
  if (!!res.msgInfo) {
    // msgInfo
    msgInfo = {
      ...res.msgInfo,

      titleYn: res.msgInfo.titleYn === 'Y' ? true : false,
      attachmentYn: res.msgInfo.attachmentYn === 'Y' ? true : false,
      smsYn: res.msgInfo.smsYn === 'Y' ? true : false
    };

    // contents
    if (!!res.msgInfo.content && res.msgInfo.content.length > 0) {
      for (const content of res.msgInfo.content) {
        contents.push({
          ...content,

          activeYn: content.activeYn === 'Y' ? true : false
        });
      }
    }

    // receiver
    if (!!res.recvList && res.recvList.length > 0) {
      for (const recv of res.recvList) {
        recvList.push({
          ...recv,

          cancelYn: recv.cancelYn === 'Y' ? true : false,
          readYn: recv.readYn === 'Y' ? true : false
        });
      }
    }
  }

  return {
    responseCode: res.responseCode,
    responseMsg: res.responseMsg,

    msgInfo,
    contents,
    recvList
  } as DetailResponse;
};

export interface ReadRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  tokenKey: string;
  type: MessageType;

  msgId: number;
}

export interface ReadResponse extends MessageAPIResponse {}

export const encodeRead: APIJsonEncoder<ReadRequest> = (req: ReadRequest) => {
  return JSON.stringify(req);
};

export const decodeRead: APIDecoder<ReadResponse> = (res: any) => {
  return {
    responseCode: res.responseCode,
    responseMsg: res.responseMsg
  } as ReadResponse;
};

export interface ReadCheckResponse extends MessageAPIResponse {
  recvList: {
    userSeq: number;
    userName: string;
    cancelYn: boolean;
    readDate: string;
    readYn: boolean;
  }[];
}

export const decodeReadCheck: APIDecoder<ReadCheckResponse> = (res: any) => {
  return {
    responseCode: res.responseCode,
    responseMsg: res.responseMsg,

    recvList: []
  } as ReadCheckResponse;
};

export interface CancelRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  tokenKey: string;
  type: MessageType;

  msgId: number;
  recvUserList: { userSeq: number }[];
}

export interface CancelResponse extends MessageAPIResponse {}

export const encodeCancel: APIJsonEncoder<CancelRequest> = (
  req: CancelRequest
) => {
  return JSON.stringify(req);
};

export const decodeCancel: APIDecoder<CancelResponse> = (res: any) => {
  return {
    responseCode: res.responseCode,
    responseMsg: res.responseMsg
  } as CancelResponse;
};

export interface CancelReservationRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  tokenKey: string;
  type: MessageType;

  msgId: number;
}

export interface CancelReservationResponse extends MessageAPIResponse {}

export const encodeCancelReservation: APIJsonEncoder<CancelReservationRequest> = (
  req: CancelReservationRequest
) => {
  return JSON.stringify(req);
};

export const decodeCancelReservation: APIDecoder<CancelReservationResponse> = (
  res: any
) => {
  return {
    responseCode: res.responseCode,
    responseMsg: res.responseMsg
  } as CancelReservationResponse;
};

export interface RetrieveResourceFileRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  tokenKey: string;
  type: MessageType;

  msgId: number;
  resUrl: string;
}

export interface RetrieveResourceFileResponse extends MessageAPIResponse {}

export const encodeRetrieveResourceFile: APIJsonEncoder<RetrieveResourceFileRequest> = (
  req: RetrieveResourceFileRequest
) => {
  return JSON.stringify(req);
};

export const decodeRetrieveResourceFile: APIDecoder<RetrieveResourceFileResponse> = (
  res: any
) => {
  return {} as RetrieveResourceFileResponse;
};
