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

import { UserInfoSS, AuthResponse } from '@ucap-webmessenger/protocol-query';
import { OpenProfileOptions } from '@ucap-webmessenger/protocol-buddy';
import { FileUploadItem } from '@ucap-webmessenger/api';
import { FormControl } from '@angular/forms';
import { SmsUtils } from '@ucap-webmessenger/daesang';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import { NativeService, UCAP_NATIVE_SERVICE } from '@ucap-webmessenger/native';
import { WorkStatusType } from '@ucap-webmessenger/protocol-status';

@Component({
  selector: 'ucap-profile-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.scss']
})
export class ProfileComponent implements OnInit {
  @Input()
  profileImageRoot: string;
  @Input()
  editableProfileImage: boolean;
  @Input()
  isMe: boolean;
  @Input()
  isBuddy: boolean;
  @Input()
  isFavorit: boolean;
  @Input()
  userInfo: UserInfoSS;
  @Input()
  myMadn?: string;
  @Input()
  openProfileOptions?: OpenProfileOptions;
  @Input()
  useBuddyToggleButton: boolean;
  @Input()
  authInfo: AuthResponse;

  @Output()
  openChat = new EventEmitter<UserInfoSS>();
  @Output()
  sendMessage = new EventEmitter<UserInfoSS>();
  @Output()
  sendCall = new EventEmitter<string>();
  @Output()
  sendSms = new EventEmitter<string>();
  @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>();
  @Output()
  close = new EventEmitter();

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

  userIntroFormControl = new FormControl('');

  profileImageFileUploadItem: FileUploadItem;

  constructor(
    private sessionStorageService: SessionStorageService,
    @Inject(UCAP_NATIVE_SERVICE) private nativeService: NativeService
  ) {}

  ngOnInit() {}

  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() {}

  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) {
    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;
  }

  onClose() {
    this.close.emit();
  }
}
