import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { UserInfo } from '@ucap-webmessenger/protocol-sync';
import {
  UserInfoSS,
  UserInfoF,
  UserInfoDN
} from '@ucap-webmessenger/protocol-query';
import { VersionInfo2Response } from '@ucap-webmessenger/api-public';
import { LoginResponse } from '@ucap-webmessenger/protocol-authentication';
import { SessionStorageService } from '@ucap-webmessenger/web-storage';
import { KEY_VER_INFO, KEY_LOGIN_RES_INFO } from '@app/types';

export interface SelectedUserListDialogData {
  title: string;
  /** 선택된 사용자의 리스트 */
  selectedUserList?: (UserInfo | UserInfoSS | UserInfoF | UserInfoDN)[];
}

export interface SelectedUserListDialogResult {
  choice: boolean;
  selectedUserList?: (UserInfo | UserInfoSS | UserInfoF | UserInfoDN)[];
}

@Component({
  selector: 'app-layout-messenger-selected-user-list',
  templateUrl: './selected-user-list.dialog.component.html',
  styleUrls: ['./selected-user-list.dialog.component.scss']
})
export class SelectedUserListDialogComponent implements OnInit {
  selectedUserList: (UserInfo | UserInfoSS | UserInfoF | UserInfoDN)[] = [];

  loginRes: LoginResponse;
  sessionVerinfo: VersionInfo2Response;

  constructor(
    public dialogRef: MatDialogRef<
      SelectedUserListDialogData,
      SelectedUserListDialogResult
    >,
    @Inject(MAT_DIALOG_DATA) public data: SelectedUserListDialogData,
    private sessionStorageService: SessionStorageService
  ) {
    this.loginRes = this.sessionStorageService.get<LoginResponse>(
      KEY_LOGIN_RES_INFO
    );
    this.sessionVerinfo = this.sessionStorageService.get<VersionInfo2Response>(
      KEY_VER_INFO
    );
  }

  ngOnInit(): void {
    this.selectedUserList = this.data.selectedUserList;
  }

  /** 전체선택 이벤트 */
  onCheckAllUser(value: boolean) {
    if (!!value) {
      this.selectedUserList = this.data.selectedUserList;
    } else {
      this.selectedUserList = [];
    }
  }

  /** 리스트 checkable 할 경우 checkbox 의 isChecked 를 관장하며 리스트의 전체선택 여부를 판단한다. */
  getCheckedUser(userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN) {
    if (!!this.selectedUserList && this.selectedUserList.length > 0) {
      return (
        this.selectedUserList.filter(item => item.seq === userInfo.seq).length >
        0
      );
    }
    return false;
  }

  /** 리스트가 checkable 할 경우 checkbox 의 change 이벤트를 상위 컴포넌트로 전달한다. */
  onCheckUser(params: {
    isChecked: boolean;
    userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN;
  }) {
    if (params.isChecked) {
      if (
        params.userInfo &&
        this.selectedUserList.filter(user => user.seq === params.userInfo.seq)
          .length === 0
      ) {
        this.selectedUserList = [...this.selectedUserList, params.userInfo];
      }
    } else {
      this.selectedUserList = this.selectedUserList.filter(
        user => user.seq !== params.userInfo.seq
      );
    }
  }
  onToggleUser(userInfo: UserInfo | UserInfoSS | UserInfoF | UserInfoDN) {
    if (userInfo.seq === this.loginRes.userSeq) {
      return;
    }

    if (
      this.selectedUserList.filter(user => user.seq === userInfo.seq).length ===
      0
    ) {
      this.selectedUserList = [...this.selectedUserList, userInfo];
    } else {
      this.selectedUserList = this.selectedUserList.filter(
        item => item.seq !== userInfo.seq
      );
    }
  }

  onClickOpenProfile(userSeq: number) {}

  onClickChoice(choice: boolean): void {
    this.dialogRef.close({
      choice,
      selectedUserList: this.selectedUserList
    });
  }
}
