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

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

  @Output()
  massDetail = new EventEmitter<number>();

  content: string;
  eventMassSeq: number;
  detailButteonShow = true;

  moment = moment;

  constructor(private logger: NGXLogger) {}

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

      if (!!this.message.sentMessageJson.massSeq) {
        this.eventMassSeq = this.message.sentMessageJson.massSeq;
      } else {
        this.detailButteonShow = false;
      }
    } catch (e) {
      this.logger.error(e);
      this.detailButteonShow = false;
    }
  }

  onClickDetailView() {
    this.massDetail.emit(this.eventMassSeq);
  }
}
