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

import { AppKey } from '@app/types/app-key.type';
import { LoginSession } from '@app/models/login-session';
import { Subject } from 'rxjs';
import { Store, select } from '@ngrx/store';
import { takeUntil, take, map } from 'rxjs/operators';
import {
  LoginSelector,
  AuthorizationSelector
} from '@ucap/ng-store-authentication';
import { LoginResponse } from '@ucap/protocol-authentication';
import { QueryProtocolService } from '@ucap/ng-protocol-query';
import { UserInfoSS, AuthResponse } from '@ucap/protocol-query';

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

  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProfileSectionComponent implements OnInit, OnDestroy {
  @Input()
  set userSeq(seq: string) {
    this._userSeq = seq;
    this.getUserInfo(seq);
  }
  get userSeq(): string {
    return this._userSeq;
  }
  // tslint:disable-next-line: variable-name
  _userSeq: string;

  loginRes: LoginResponse;
  authRes: AuthResponse;
  isMe = false;

  profileUserInfo: UserInfoSS;

  private ngOnDestroySubject = new Subject<boolean>();

  constructor(
    private store: Store<any>,
    private queryProtocolService: QueryProtocolService,
    private changeDetectorRef: ChangeDetectorRef
  ) {}

  ngOnInit(): void {
    this.store
      .pipe(takeUntil(this.ngOnDestroySubject), select(LoginSelector.loginRes))
      .subscribe((loginRes) => {
        this.loginRes = loginRes;

        if (!!loginRes) {
          const seq = !!this.userSeq ? this.userSeq : loginRes.userSeq;
          this.getUserInfo(seq);
        }

        if (!!this.userSeq && this.userSeq === loginRes.userSeq) {
          this.isMe = true;
        } else {
          this.isMe = false;
        }
      });

    this.store
      .pipe(
        takeUntil(this.ngOnDestroySubject),
        select(AuthorizationSelector.authResponse)
      )
      .subscribe((authRes) => {
        this.authRes = authRes;
      });
  }

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

  getUserInfo(userSeq: string | number): void {
    if (!!userSeq && !!this.loginRes && !!this.loginRes.userInfo) {
      this.queryProtocolService
        .dataUser({
          divCd: 'OPENPROF',
          seq: Number(userSeq + ''),
          senderCompanyCode: this.loginRes.userInfo.companyCode,
          senderEmployeeType: this.loginRes.userInfo.employeeType
        })
        .pipe(
          take(1),
          map((res) => {
            if (this.loginRes.userSeq + '' === userSeq + '') {
              this.isMe = true;
            } else {
              this.isMe = false;
            }

            this.profileUserInfo = res.userInfo;
            this.changeDetectorRef.detectChanges();
          })
        )
        .subscribe();
    }
  }

  onToggleBuddy(param: { userInfo: UserInfoSS; isBuddy: boolean }) {
    console.log('onToggleBuddy : ', param);
  }
  onToggleFavorite(param: { userInfo: UserInfoSS; isFavorite: boolean }) {
    console.log('onToggleFavorite : ', param);
  }

  onOpenChat(userInfo: UserInfoSS) {
    console.log('onOpenChat : ', userInfo);
  }

  onSendCall(calleeNum: string) {
    console.log('onSendCall : ', calleeNum);
  }

  onSendSms(calleenum: string) {
    console.log('onSendSms : ', calleenum);
  }

  onCreateConference(userSeq: number) {
    console.log('onCreateConference : ', userSeq);
  }

  onSendMessage(userInfo: UserInfoSS) {
    console.log('onSendMessage : ', userInfo);
  }
}
