import {
  Component,
  OnInit,
  OnDestroy,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Input,
  Output,
  EventEmitter,
  ViewChild,
  ElementRef
} from '@angular/core';

import { AppKey } from '@app/types/app-key.type';
import { LoginSession } from '@app/models/login-session';
import { Subject } from 'rxjs';
import { UserInfoSS, AuthResponse } from '@ucap/protocol-query';
import { OpenProfileOptions } from '@ucap/protocol-buddy';
import { FileUploadItem } from '@ucap/api';
import { FormControl } from '@angular/forms';
import { WorkStatusType } from '@ucap/protocol';
import { I18nService } from '@ucap/ng-i18n';

@Component({
  selector: 'app-group-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.scss'],

  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProfileComponent implements OnInit, OnDestroy {
  @Input()
  set userInfo(u: UserInfoSS) {
    this._userInfo = u;
  }
  get userInfo(): UserInfoSS {
    return this._userInfo;
  }
  _userInfo: UserInfoSS;
  @Input()
  isMe: boolean;
  @Input()
  isBuddy: boolean;
  @Input()
  isFavorite: boolean;
  @Input()
  myMadn?: string;
  @Input()
  canVideoConfernece: boolean;

  @Output()
  toggleFavorite = new EventEmitter<{
    userInfo: UserInfoSS;
    isFavorite: boolean;
  }>();
  @Output()
  toggleBuddy = new EventEmitter<{
    userInfo: UserInfoSS;
    isBuddy: boolean;
  }>();
  @Output()
  openChat = new EventEmitter<UserInfoSS>();
  @Output()
  sendCall = new EventEmitter<string>();
  @Output()
  sendSms = new EventEmitter<string>();
  @Output()
  createConference = new EventEmitter<number>();
  @Output()
  sendMessage = new EventEmitter<UserInfoSS>();

  ///////////////////////////////////////////////

  @Input()
  profileImageRoot: string;

  @Input()
  openProfileOptions?: OpenProfileOptions;
  @Input()
  useBuddyToggleButton: boolean;
  @Input()
  authInfo: AuthResponse;
  @Output()
  profileImageView = new EventEmitter<void>();
  @Output()
  uploadProfileImage = new EventEmitter<FileUploadItem>();
  @Output()
  updateIntro = new EventEmitter<string>();

  @ViewChild('profileImageFileInput', { static: false })
  profileImageFileInput: ElementRef<HTMLInputElement>;

  userIntroFormControl = new FormControl('');
  profileImageFileUploadItem: FileUploadItem;

  private ngOnDestroySubject = new Subject<boolean>();

  constructor(private i18nService: I18nService) {}

  ngOnInit(): void {
    console.log(this.isMe);
  }

  ngOnDestroy(): void {
    if (!!this.ngOnDestroySubject) {
      this.ngOnDestroySubject.complete();
    }
  }

  onToggleFavorit() {
    this.toggleFavorite.emit({
      userInfo: this.userInfo,
      isFavorite: !this.isFavorite
    });
  }

  onClickToggleBuddy() {
    this.toggleBuddy.emit({
      userInfo: this.userInfo,
      isBuddy: !this.isBuddy
    });
  }

  onClickActionButton(event: MouseEvent, type: string): void {
    event.preventDefault();
    event.stopPropagation();

    switch (type) {
      case 'CHAT':
        this.onClickOpenChat();
        break;
      case 'MESSAGE':
        this.onClickMessage();
        break;
      case 'SMS':
        this.onClickSMS();
        break;
      case 'CALL_MOBILE':
        this.onClickCall('MOBILE');
        break;
      case 'CALL_LINE':
        this.onClickCall('LINE');
        break;
      case 'CONFERENCE':
        this.onClickVideoConference();
        break;
    }
  }

  onClickOpenChat() {
    this.openChat.emit(this.userInfo);
  }

  onClickCall(type: string) {
    let calleeNumber = '';

    if (type === 'LINE') {
      calleeNumber = this.userInfo.lineNumber;
    } else {
      calleeNumber = this.userInfo.hpNumber;
    }
    this.sendCall.emit(calleeNumber);
  }

  onClickSMS() {
    this.sendSms.emit(this.userInfo.hpNumber);
  }

  onClickVideoConference() {
    this.createConference.emit(Number(this.userInfo.seq));
  }

  onClickMessage() {
    this.sendMessage.emit(this.userInfo);
  }

  isDisabledCallButton(type: string): boolean {
    if (!this.myMadn || this.myMadn.trim().length === 0) {
      if (type === 'LINE' || type === 'MOBILE') {
        return true;
      }
    }

    if (type === 'LINE') {
      if (
        !!this.userInfo &&
        !!this.userInfo.lineNumber &&
        this.userInfo.lineNumber.trim().length > 0
      ) {
        return false;
      } else {
        return true;
      }
    } else if (type === 'MOBILE') {
      if (
        !!this.userInfo &&
        !!this.userInfo.hpNumber &&
        this.userInfo.hpNumber.trim().length > 0
      ) {
        return false;
      } else {
        return true;
      }
    } else if (type === 'SMS') {
      // const smsUtils = new SmsUtils(
      //   this.sessionStorageService,
      //   this.nativeService
      // );
      // return !smsUtils.getAuthSms();
    }

    return true;
  }

  ///////////////////////////////////////////////

  onClickProfileImageView() {
    this.profileImageView.emit();
  }

  onApplyIntroMessage(intro: string) {
    if (intro.trim().length < 1) {
      this.updateIntro.emit(' ');
    } else {
      this.updateIntro.emit(intro);
    }
  }

  onChangeFileInput() {
    this.profileImageFileUploadItem = FileUploadItem.fromFiles(
      this.profileImageFileInput.nativeElement.files
    )[0];

    this.uploadProfileImage.emit(this.profileImageFileUploadItem);

    this.profileImageFileInput.nativeElement.value = '';
  }

  getWorkstatus(userInfo: UserInfoSS): string {
    let workstatus = '';
    if (!!userInfo && !!userInfo.workstatus) {
      switch (userInfo.workstatus) {
        case WorkStatusType.VacationAM:
          workstatus = '오전';
          break;
        case WorkStatusType.VacationPM:
          workstatus = '오후';
          break;
        case WorkStatusType.VacationAll:
          workstatus = '휴가';
          break;
        case WorkStatusType.LeaveOfAbsence:
          workstatus = '휴직';
          break;
        case WorkStatusType.LongtermRefresh:
          workstatus = '장기';
          break;
      }
    }

    return workstatus;
  }
  getWorkstatusStyle(userInfo: UserInfoSS): string {
    // morning-off: 오전 afternoon-off: 오후  day-off: 휴가 long-time: 장기  leave-of-absence: 휴직
    let style = '';
    if (!!userInfo && !!userInfo.workstatus) {
      switch (userInfo.workstatus) {
        case WorkStatusType.VacationAM:
          style = 'morning-off';
          break;
        case WorkStatusType.VacationPM:
          style = 'afternoon-off';
          break;
        case WorkStatusType.VacationAll:
          style = 'day-off';
          break;
        case WorkStatusType.LeaveOfAbsence:
          style = 'leave-of-absence';
          break;
        case WorkStatusType.LongtermRefresh:
          style = 'long-time';
          break;
      }
    }

    return style;
  }
}