import { DeviceType } from '@ucap-webmessenger/core';
import {
  APIRequest,
  APIResponse,
  APIEncoder,
  APIDecoder,
  ParameterUtil,
  JsonAnalization,
  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 {
    const json = JsonAnalization.receiveAnalization(res);
    return {
      statusCode: json.StatusCode,
      roomSeq: json.RoomID,
      fileName: json.FileName,
      fileExt: json.FileExt,
      fileType: json.FileType,
      thumbnailUrl: json.ThumbURL,
      attachmentSeq: json.AttSEQ,
      attachmentSize: json.AttSize,
      attachmentRegDate: json.AttRegDate,
      companyCode: json.CompanyCode,
      synapKey: json.SynapKey,
      returnJson: res
    } as FileTalkShareResponse;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e
    } as FileTalkShareResponse;
  }
};
