import { StatusCode, JsonAnalization } from '@ucap-webmessenger/api';
import { FileType } from '@ucap-webmessenger/protocol-file';
import { EventJsonDecoder } from './event-json';

export interface FileEventJson {
  statusCode?: StatusCode;
  errorMessage?: string;
  roomSeq?: number;
  fileName?: string;
  fileExt?: string;
  fileType?: FileType;
  thumbUrl?: string;
  attachmentSeq?: number;
  attachmentSize?: number;
  attachmentRegDate?: string;
  imageWidth?: number;
  imageHeight?: number;
  companyCode?: string;
  voiceTime?: string;
  synappKey?: string;
}

export const decodeFileEventJson: EventJsonDecoder<FileEventJson> = (
  message: string
) => {
  try {
    const json = JsonAnalization.receiveAnalization(message);

    return {
      statusCode: json.StatusCode,
      errorMessage: json.ErrorMessage,
      roomSeq: json.RoomID,
      fileName: json.FileName,
      fileExt: json.FileExt,
      fileType: json.FileType,
      thumbUrl: json.ThumbURL,
      attachmentSeq: json.AttSEQ,
      attachmentSize: json.AttSize,
      attachmentRegDate: json.AttRegDate,
      imageWidth: json.ImageWidth,
      imageHeight: json.ImageHeight,
      companyCode: json.CompanyCode,
      voiceTime: json.VoiceTime,
      synappKey: json.SynappKey
    } as FileEventJson;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e.toString()
    } as FileEventJson;
  }
};
