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 {
  private ngOnDestroySubject = new Subject<boolean>();

  @Input()
  profileImageRoot: string;

  @Input()
  isMe: boolean;
  @Input()
  isBuddy: boolean;
  @Input()
  isFavorit: boolean;

  @Input()
  set userInfo(u: UserInfoSS) {
    this._userInfo = u;
  }
  get userInfo(): UserInfoSS {
    return this._userInfo;
  }
  _userInfo: UserInfoSS;
  @Input()
  myMadn?: string;
  @Input()
  openProfileOptions?: OpenProfileOptions;
  @Input()
  useBuddyToggleButton: boolean;
  @Input()
  authInfo: AuthResponse;

  @Output()
  profileImageView = new EventEmitter<void>();
  @Output()
  openChat = new EventEmitter<UserInfoSS>();
  @Output()
  sendMessage = new EventEmitter<UserInfoSS>();
  @Output()
  sendCall = new EventEmitter<string>();
  @Output()
  sendSms = new EventEmitter<string>();
  @Output()
  createConference = new EventEmitter<number>();
  @Output()
  toggleFavorit = new EventEmitter<{
    userInfo: UserInfoSS;
    isFavorit: boolean;
  }>();
  @Output()
  toggleBuddy = new EventEmitter<{
    userInfo: UserInfoSS;
    isBuddy: boolean;
  }>();
  @Output()
  uploadProfileImage = new EventEmitter<FileUploadItem>();
  @Output()
  updateIntro = new EventEmitter<string>();

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

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

  constructor(private i18nService: I18nService) {}

  ngOnInit(): void {}

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

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

  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.employeeNum);
  }

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

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

  onToggleFavorit() {
    this.isFavorit = !this.isFavorit;

    this.toggleFavorit.emit({
      userInfo: this.userInfo,
      isFavorit: this.isFavorit
    });
  }

  onClickAddBuddy() {
    this.toggleBuddy.emit({
      userInfo: this.userInfo,
      isBuddy: true
    });
  }

  onClickDelBuddy() {
    this.toggleBuddy.emit({
      userInfo: this.userInfo,
      isBuddy: false
    });
  }
  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;
  }

  getDisabledBtn(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;
  }

  getShowBuddyToggleBtn(type: 'DEL' | 'ADD'): boolean {
    let rtn = false;
    if (!this.useBuddyToggleButton) {
      return false;
    }

    if (type === 'ADD') {
      if (!this.isBuddy) {
        rtn = true;
      }
    } else if (type === 'DEL') {
      if (!!this.isBuddy) {
        rtn = true;
      }
    }
    return rtn;
  }
}
