import { Component, OnInit, Output, Input, EventEmitter } from '@angular/core';
import {
  Info,
  InfoResponse,
  FileEventJson
} from '@ucap-webmessenger/protocol-event';
import { StatusCode } from '@ucap-webmessenger/api';
import { FileType } from '@ucap-webmessenger/protocol-file';
import { NGXLogger } from 'ngx-logger';

@Component({
  selector: 'ucap-chat-message-box-file',
  templateUrl: './file.component.html',
  styleUrls: ['./file.component.scss']
})
export class FileComponent implements OnInit {
  @Input()
  message: Info<FileEventJson>;
  @Input()
  eventInfoStatus: InfoResponse;

  @Output()
  save = new EventEmitter<{ fileInfo: FileEventJson; type: string }>();
  @Output()
  fileViewer = new EventEmitter<FileEventJson>();

  fileInfo?: FileEventJson;
  errorMessage?: string;
  FileType = FileType;

  constructor(private logger: NGXLogger) {}

  ngOnInit() {
    if (StatusCode.Success === this.message.sentMessageJson.statusCode) {
      this.fileInfo = this.message.sentMessageJson;
    } else {
      this.errorMessage =
        this.message.sentMessageJson.errorMessage || '[Error] System Error!!';
    }
  }

  getExpiredFile() {
    if (
      !!this.eventInfoStatus &&
      this.eventInfoStatus.validFileBaseSeq <= this.message.seq
    ) {
      return false;
    } else {
      return true;
    }
  }

  onClickFileViewer(fileInfo: FileEventJson) {
    this.fileViewer.emit(fileInfo);
  }

  onSave(value: string) {
    this.save.emit({ fileInfo: this.fileInfo, type: value });
  }
}
