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

export interface MassTalkSaveRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  token: string;
  content?: string;
  roomSeq?: string;
}

export interface MassTalkSaveResponse extends APIResponse {
  eventMassSeq?: string;
  roomSeq?: string;
  regDate?: string;
  content?: string;
  returnJson?: any;
}

const massTalkSaveEncodeMap = {
  userSeq: 'p_user_seq',
  deviceType: 'p_device_type',
  token: 'p_token',
  content: 'p_content',
  roomSeq: 'p_room_id'
};

export const encodeMassTalkSave: APIEncoder<MassTalkSaveRequest> = (
  req: MassTalkSaveRequest
) => {
  return ParameterUtil.encode(massTalkSaveEncodeMap, req);
};

export const decodeMassTalkSave: APIDecoder<MassTalkSaveResponse> = (
  res: any
) => {
  try {
    const json = JsonAnalization.receiveAnalization(res);
    return {
      statusCode: json.StatusCode,
      errorMessage: json.ErrorMessage,
      content: json.Content,
      eventMassSeq: json.EventMassSeq,
      regDate: json.RegDate,
      roomSeq: json.RoomID,
      returnJson: res
    } as MassTalkSaveResponse;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e
    } as MassTalkSaveResponse;
  }
};
