import { DeviceType } from '@ucap-webmessenger/core';
import {
  APIRequest,
  APIResponse,
  APIEncoder,
  APIDecoder,
  ParameterUtil,
  JsonAnalization,
  StatusCode
} from '@ucap-webmessenger/api';

export interface MassTalkDownloadRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  token: string;
  eventMassSeq?: number;
}

export interface MassTalkDownloadResponse extends APIResponse {
  content?: string;
  userName?: string;
  regDate?: string;
}

const massTalkDownloadEncodeMap = {
  userSeq: 'p_user_seq',
  deviceType: 'p_device_type',
  token: 'p_token',
  eventMassSeq: 'p_event_mass_seq'
};

export const encodeMassTalkDownload: APIEncoder<MassTalkDownloadRequest> = (
  req: MassTalkDownloadRequest
) => {
  return ParameterUtil.encode(massTalkDownloadEncodeMap, req);
};

export const decodeMassTalkDownload: APIDecoder<MassTalkDownloadResponse> = (
  res: any
) => {
  try {
    const json = JsonAnalization.receiveAnalization(res);
    return {
      statusCode: json.StatusCode,
      errorMessage: json.ErrorMessage,
      content: json.Content,
      userName: json.UserName,
      regDate: json.RegDate
    } as MassTalkDownloadResponse;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e
    } as MassTalkDownloadResponse;
  }
};
