import {
  Component,
  OnInit,
  OnDestroy,
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Inject,
  ViewChild
} from '@angular/core';
import { UserInfo, GroupDetailData } from '@ucap/protocol-sync';
import {
  UserInfoSS,
  UserInfoF,
  UserInfoDN,
  DeptInfo
} from '@ucap/protocol-query';

import { Store, select } from '@ngrx/store';
import { takeUntil } from 'rxjs/operators';
import {
  CompanySelector,
  DepartmentSelector
} from '@ucap/ng-store-organization';
import { Subject, combineLatest } from 'rxjs';
import { AppAuthenticationService } from '@app/services/app-authentication.service';
import { SelectUserDialogType } from '@app/types';
import { RoomInfo, UserInfo as RoomUserInfo } from '@ucap/protocol-room';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { SelectUserSectionComponent } from '../../select-user.section.component';
import { GroupActions } from '@ucap/ng-store-group';
import { UserInfoTypes } from '../profile-list-item.component';

export interface CreateChatDialogData {
  type?: SelectUserDialogType;
  title: string;
  /** CASE :: EditMember */
  group?: GroupDetailData;
  /** CASE :: EventForward */
  ignoreRoom?: RoomInfo[];
  /** CASE :: EditChatMember */
  curRoomUser?: (
    | UserInfo
    | UserInfoSS
    | UserInfoF
    | UserInfoDN
    | RoomUserInfo
  )[];
}
export interface CreateChatDialogResult {}

@Component({
  selector: 'ucap-local-organization-create-chat.dialog',
  templateUrl: './create-chat.dialog.component.html',
  styleUrls: ['./create-chat.dialog.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CreateChatDialogComponent implements OnInit, OnDestroy {
  private ngOnDestroySubject = new Subject<boolean>();
  isLinear = false;
  firstFormGroup: FormGroup;
  secondFormGroup: FormGroup;
  selectedUserList: UserInfoTypes[] = [];
  SelectUserDialogType = SelectUserDialogType;

  constructor(
    public dialogRef: MatDialogRef<
      CreateChatDialogData,
      CreateChatDialogResult
    >,
    @Inject(MAT_DIALOG_DATA) public data: CreateChatDialogData,
    private changeDetectorRef: ChangeDetectorRef,
    private store: Store<any>,
    private appAuthenticationService: AppAuthenticationService
  ) {}

  @ViewChild('selectBoxUserComponent', { static: false })
  selectBoxUserComponent: SelectUserSectionComponent;

  ngOnInit(): void {}

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

  onClickCancel() {}
  onClickChoice(s: boolean) {}
  getBtnValid() {}
  getChipsRemoveYn(userInfo: UserInfo) {}
  onClickDeleteUser(userInfo: UserInfo) {}

  onChangeSelectedUserList(userList: UserInfoTypes[]) {
    this.selectedUserList = userList;
    this.changeDetectorRef.markForCheck();
  }
  onClickComplete(groupName: string) {
    switch (this.data.type) {
      case SelectUserDialogType.NewGroup:
        {
          const userSeqs: string[] = [];
          this.selectedUserList.map((user) =>
            userSeqs.push(user.seq.toString())
          );
          this.store.dispatch(
            GroupActions.create({
              groupName,
              targetUserSeqs: userSeqs
            })
          );
        }
        break;
      case SelectUserDialogType.NewChat:
        {
        }
        break;
      case SelectUserDialogType.EditChatMember:
        {
        }
        break;
      case SelectUserDialogType.EditMember:
        {
        }
        break;
      case SelectUserDialogType.MessageForward:
        {
        }
        break;
    }

    this.dialogRef.close();
  }
}
