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

export interface FileTalkShareRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  token: string;
  attachmentsSeq?: string;
  roomSeq?: string;
  synapKey?: string;
}

export interface FileTalkShareResponse extends APIResponse {
  roomSeq?: string;
  fileName?: string;
  fileExt?: string;
  fileType?: string;
  thumbnailUrl?: string;
  attachmentSeq?: string;
  attachmentSize?: string;
  attachmentRegDate?: string;
  companyCode?: string;
  synapKey?: string;
  returnJson?: any;
}

const fileTalkShareEncodeMap = {
  userSeq: 'p_user_seq',
  deviceType: 'p_device_type',
  token: 'p_token',
  attachmentsSeq: 'p_att_seq',
  roomSeq: 'p_room_id',
  synapKey: 'p_synap_key'
};

export const encodeFileTalkShare: APIEncoder<FileTalkShareRequest> = (
  req: FileTalkShareRequest
) => {
  return ParameterUtil.encode(fileTalkShareEncodeMap, req);
};

export const decodeFileTalkShare: APIDecoder<FileTalkShareResponse> = (
  res: any
) => {
  try {
    return {
      statusCode: res.StatusCode,
      roomSeq: res.RoomID,
      fileName: res.FileName,
      fileExt: res.FileExt,
      fileType: res.FileType,
      thumbnailUrl: res.ThumbURL,
      attachmentSeq: res.AttSEQ,
      attachmentSize: res.AttSize,
      attachmentRegDate: res.AttRegDate,
      companyCode: res.CompanyCode,
      synapKey: res.SynapKey,
      returnJson: JSON.stringify(res)
    } as FileTalkShareResponse;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e
    } as FileTalkShareResponse;
  }
};
