import { Component, OnInit, Input } from '@angular/core';
import { NGXLogger } from 'ngx-logger';
import {
  Info,
  EventType,
  EventJson,
  JoinEventJson,
  RenameRoomEventJson,
  NotificationForTimerRoomEventJson,
  GuideForRoomTimerChangedEventJson
} from '@ucap-webmessenger/protocol-event';

@Component({
  selector: 'ucap-chat-message-box-information',
  templateUrl: './information.component.html',
  styleUrls: ['./information.component.scss']
})
export class InformationComponent implements OnInit {
  @Input()
  message: Info<EventJson>;
  @Input()
  senderName?: string;

  contents: string;

  constructor(private logger: NGXLogger) {}

  ngOnInit() {
    switch (this.message.type) {
      case EventType.Join:
        {
          const m = this.message as Info<JoinEventJson>;

          const owner = m.sentMessageJson.owner + '님';
          const inviter: string[] = [];

          m.sentMessageJson.inviter.forEach((userName, idx) => {
            inviter.push(userName + '님');
          });

          this.contents = `${owner}이 ${inviter.join(',')}을 초대했습니다.`;
        }
        break;
      case EventType.Exit:
        {
          const m = this.message as Info<JoinEventJson>;
          this.contents = `${m.sentMessage}님이 퇴장하셨습니다.`;
        }
        break;
      case EventType.RenameRoom:
        {
          const m = this.message as Info<RenameRoomEventJson>;
          this.contents = `${m.sentMessageJson.requester}님이 대화방명을 '${m.sentMessageJson.roomName}'으로 변경하셨습니다.`;
        }
        break;
      case EventType.NotificationForTimerRoom:
        {
          const m = this.message as Info<NotificationForTimerRoomEventJson>;
          /**
           * 타이머대화방 알림으로서 채팅 이벤트에서 처리하지 않고, 대화방 글로벌에서 처리.
           */
          this.contents = m.sentMessage;
        }
        break;
      case EventType.GuideForRoomTimerChanged:
        {
          const m = this.message as Info<GuideForRoomTimerChangedEventJson>;
          this.contents = `${
            this.senderName
          }님이 타이머를 설정하였습니다.(${this.getCalcTimer(
            m.sentMessageJson.time * 1000
          )})`;
        }
        break;
    }
  }

  getCalcTimer(millisec: number) {
    switch (millisec) {
      case 5000:
        return '5 초';
      case 10000:
        return '10 초';
      case 30000:
        return '30 초';
      case 60000:
        return '1 분';
      case 300000:
        return '5 분';
      case 600000:
        return '10 분';
      case 1800000:
        return '30 분';
      case 3600000:
        return '1 시간';
      case 21600000:
        return '6 시간';
      case 43200000:
        return '12 시간';
      case 86400000:
        return '24 시간';
      default:
        return '0 초';
    }
  }
}
