import {
  Component,
  OnInit,
  Input,
  Output,
  EventEmitter,
  ViewChildren,
  ChangeDetectorRef
} from '@angular/core';
import {
  RoomInfo,
  UserInfoShort,
  UserInfo as RoomUserInfo,
  RoomType
} from '@ucap-webmessenger/protocol-room';
import { NGXLogger } from 'ngx-logger';
import { VersionInfo2Response } from '@ucap-webmessenger/api-public';
import { LoginResponse } from '@ucap-webmessenger/protocol-authentication';
import { TranslatePipe } from 'projects/ucap-webmessenger-ui/src/lib/pipes/translate.pipe';
import {
  TranslateService as uiTranslateService,
  StringUtil
} from '@ucap-webmessenger/ui';
import { TranslateService } from '@ngx-translate/core';
import { EventType } from '@ucap-webmessenger/protocol-event';

@Component({
  selector: 'ucap-room-list-item',
  templateUrl: './list-item.component.html',
  styleUrls: ['./list-item.component.scss']
})
export class ListItemComponent implements OnInit {
  @Input()
  loginRes: LoginResponse;
  @Input()
  roomInfo: RoomInfo;
  @Input()
  roomUserInfo: (RoomUserInfo | UserInfoShort)[];
  @Input()
  sessionVerinfo: VersionInfo2Response;
  @Input()
  checkable = false;
  @Input()
  isChecked = false;
  @Input()
  multiCheckable = true;

  @Output()
  checkRoom = new EventEmitter<{
    isChecked: boolean;
    roomInfo: RoomInfo;
  }>();

  imagePath: string;
  defaultPath = 'assets/images/img_nophoto_50.png';
  finalEventMessage: string;
  RoomType = RoomType;

  constructor(
    private logger: NGXLogger,
    private uiTranslateService: uiTranslateService,
    private translateService: TranslateService,
    private changeDetectorRef: ChangeDetectorRef
  ) {}

  ngOnInit() {
    if (this.roomInfo.isTimeRoom) {
      this.finalEventMessage = '비밀 대화방입니다.';
    } else if (
      this.roomInfo.finalEventType === EventType.NotificationForiOSCapture
    ) {
      this.finalEventMessage = this.translateService.instant(
        'chat.event.iosCapture',
        {
          requester: this.roomInfo.finalEventMessage
        }
      );
    } else {
      this.finalEventMessage = this.roomInfo.finalEventMessage;
    }

    switch (this.roomInfo.roomType) {
      case RoomType.Multi:
        this.defaultPath = 'assets/images/img_groupphoto_80.png';
        break;
      case RoomType.Mytalk:
        const me = this.roomUserInfo.filter(
          v => v.seq === this.loginRes.userSeq
        );
        if (!!me && me.length > 0) {
          this.imagePath = me[0].profileImageFile;
        }
        break;
      case RoomType.Single:
      case RoomType.Bot:
      case RoomType.Allim:
      case RoomType.Allim_Elephant:
      case RoomType.Allim_TMS:
        const others = this.roomUserInfo.filter(
          v => v.seq !== this.loginRes.userSeq
        );
        if (!!others && others.length > 0) {
          this.imagePath = others[0].profileImageFile;
        }
        break;
    }
  }

  get _roomUserInfos() {
    if (this.roomInfo.roomType === RoomType.Single) {
      return this.roomUserInfo.filter(roomUserInfo => {
        return this.loginRes.userSeq !== roomUserInfo.seq;
      });
    } else {
      return this.roomUserInfo
        .filter(roomUserInfo => {
          return (
            this.loginRes.userSeq !== roomUserInfo.seq &&
            roomUserInfo.isJoinRoom
          );
        })
        .sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
    }
  }

  getRoomNameByRoomUser(roomUserInfos: (RoomUserInfo | UserInfoShort)[]) {
    let roomName = new TranslatePipe(
      this.uiTranslateService,
      this.changeDetectorRef
    ).transform(roomUserInfos, 'name', ',');

    if (!roomName || roomName.trim().length === 0) {
      roomName = this.translateService.instant('chat.noRoomUser');
    }
    return roomName;
  }

  // getChecked(value: boolean, roomInfo: RoomInfo) {
  //   if (value && !this.multiCheckable) {
  //     if (this.selected === roomInfo.roomSeq) {
  //       return true;
  //     } else {
  //       return false;
  //     }
  //   }
  // }

  onChangeCheck(value: boolean, roomInfo: RoomInfo) {
    this.checkRoom.emit({
      isChecked: value,
      roomInfo
    });
  }
}
