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

export interface TransMassTalkDownloadRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  token: string;
  eventTransSeq?: string;
}

export interface TransMassTalkDownloadResponse extends APIResponse {
  original?: string;
  translation?: string;
  locale?: string;
  userName?: string;
  regDate?: string;
  returnJson?: any;
}

const transMassTalkDownloadEncodeMap = {
  userSeq: 'p_user_seq',
  deviceType: 'p_device_type',
  token: 'p_token',
  eventTransSeq: 'p_event_trans_seq'
};

export const encodeTransMassTalkDownload: APIEncoder<TransMassTalkDownloadRequest> = (
  req: TransMassTalkDownloadRequest
) => {
  return ParameterUtil.encode(transMassTalkDownloadEncodeMap, req);
};

export const decodeTransMassTalkDownload: APIDecoder<TransMassTalkDownloadResponse> = (
  res: any
) => {
  try {
    return {
      statusCode: res.StatusCode,
      original: res.Original,
      translation: res.Translation,
      locale: res.Locale,
      userName: res.UserName,
      regDate: res.RegDate,
      returnJson: JSON.stringify(res)
    } as TransMassTalkDownloadResponse;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e
    } as TransMassTalkDownloadResponse;
  }
};
