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

export interface TransMassTalkSaveRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  token: string;
  original?: string;
  translation?: string;
  roomSeq?: string;
  locale: string;
}

export interface TransMassTalkSaveResponse extends APIResponse {
  roomSeq?: string;
  registrationDate?: string;
  translationSeq?: string;
  locale?: string;
  original?: string;
  translation?: string;
  returnJson?: any;
}

const transMassTalkSaveEncodeMap = {
  userSeq: 'p_user_seq',
  deviceType: 'p_device_type',
  token: 'p_token',
  original: 'p_original',
  translation: 'p_translation',
  roomSeq: 'p_room_id',
  locale: 'p_locale'
};

export const encodeTransMassTalkSave: APIEncoder<TransMassTalkSaveRequest> = (
  req: TransMassTalkSaveRequest
) => {
  return ParameterUtil.encode(transMassTalkSaveEncodeMap, req);
};

export const decodeTransMassTalkSave: APIDecoder<TransMassTalkSaveResponse> = (
  res: any
) => {
  try {
    return {
      statusCode: res.StatusCode,
      translationSeq: res.EventTransSEQ,
      roomSeq: res.RoomID,
      registrationDate: res.RegDate,
      locale: res.Locale,
      original: res.Original,
      translation: res.Translation,
      returnJson: JSON.stringify(res)
    } as TransMassTalkSaveResponse;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e
    } as TransMassTalkSaveResponse;
  }
};
