import {
  APIRequest,
  MessageAPIResponse,
  APIDecoder,
  APIJsonEncoder
} from '@ucap-webmessenger/api';
import { DeviceType } from '@ucap-webmessenger/core';
import { MessageType } from '../types/message.type';
import { MessageList } from '../models/message-list';
import { CategoryType } from '../types/category.type';
import { ContentType } from '../types/content.type';

export interface RetrieveRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  tokenKey: string;
  type: MessageType;
  pageSize: number;
  pageCount: number;
}
export interface RetrieveSearchRequest extends RetrieveRequest {
  searchTitle: string;
  searchName: string;
  searchContent: string;
}

export interface RetrieveResponse extends MessageAPIResponse {
  pageCount: number;
  pageSize: number;
  totalCount: number;

  messageList: MessageList[];
}

export const encodeRetrieve: APIJsonEncoder<RetrieveRequest> = (
  req: RetrieveRequest
) => {
  return JSON.stringify(req);
};
export const encodeRetrieveSearch: APIJsonEncoder<RetrieveResponse> = (
  req: RetrieveResponse
) => {
  return JSON.stringify(req);
};

export const decodeRetrieveSend: APIDecoder<RetrieveResponse> = (res: any) => {
  const messageList: MessageList[] = [];
  if (!!res.msgList && res.msgList.length > 0) {
    for (const msgList of res.msgList) {
      messageList.push({
        ...msgList,
        userCount:
          !msgList.userCount || msgList.userCount === null
            ? 0
            : msgList.userCount,
        userReadCount:
          !msgList.userReadCount || msgList.userReadCount === null
            ? 0
            : msgList.userReadCount,
        titleYn: msgList.titleYn === 'Y' ? true : false,
        attachmentYn: msgList.attachmentYn === 'Y' ? true : false
      } as MessageList);
    }
  }

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

    pageCount: res.pageCount,
    pageSize: res.pageSize,
    totalCount: res.totalCount,

    messageList
  } as RetrieveResponse;
};

export const decodeRetrieveReceive: APIDecoder<RetrieveResponse> = (
  res: any
) => {
  const messageList: MessageList[] = [];
  if (!!res.msgList && res.msgList.length > 0) {
    for (const msgList of res.msgList) {
      messageList.push({
        ...msgList,
        userCount:
          !msgList.userCount || msgList.userCount === null
            ? 0
            : msgList.userCount,
        userReadCount:
          !msgList.userReadCount || msgList.userReadCount === null
            ? 0
            : msgList.userReadCount,
        titleYn: msgList.titleYn === 'Y' ? true : false,
        attachmentYn: msgList.attachmentYn === 'Y' ? true : false,
        readYn: msgList.readYn === 'Y' ? true : false
      } as MessageList);
    }
  }

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

    pageCount: res.pageCount,
    pageSize: res.pageSize,
    totalCount: res.totalCount,

    messageList
  } as RetrieveResponse;
};

export const decodeRetrieveReservation: APIDecoder<RetrieveResponse> = (
  res: any
) => {
  const messageList: MessageList[] = [];
  if (!!res.msgList && res.msgList.length > 0) {
    for (const msgList of res.msgList) {
      messageList.push({
        ...msgList,

        userCount:
          !msgList.userCount || msgList.userCount === null
            ? 0
            : msgList.userCount,
        userReadCount:
          !msgList.userReadCount || msgList.userReadCount === null
            ? 0
            : msgList.userReadCount,
        titleYn: msgList.titleYn === 'Y' ? true : false,
        attachmentYn: msgList.attachmentYn === 'Y' ? true : false
      } as MessageList);
    }
  }

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

    pageCount: res.pageCount,
    pageSize: res.pageSize,
    totalCount: res.totalCount,

    messageList
  } as RetrieveResponse;
};

export const decodeRetrieveSearch: APIDecoder<RetrieveResponse> = (
  res: any
) => {
  const messageList: MessageList[] = [];
  if (!!res.msgList && res.msgList.length > 0) {
    for (const msgList of res.msgList) {
      messageList.push({
        ...msgList,

        userCount:
          !msgList.userCount || msgList.userCount === null
            ? 0
            : msgList.userCount,
        userReadCount:
          !msgList.userReadCount || msgList.userReadCount === null
            ? 0
            : msgList.userReadCount,
        titleYn: msgList.titleYn === 'Y' ? true : false,
        attachmentYn: msgList.attachmentYn === 'Y' ? true : false
      } as MessageList);
    }
  }

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

    pageCount: res.pageCount,
    pageSize: res.pageSize,
    totalCount: res.totalCount,

    messageList
  } as RetrieveResponse;
};
