import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Router } from '@angular/router';
import { IconType } from '@msfa-enums/icon-type.enum';
import { Employee } from '@msfa-models/employee.model';
import { Organization } from '@msfa-models/organization.model';
import { Role } from '@msfa-models/role.model';
import { UserService } from '@msfa-services/api/user.service';
import { RoleService } from '@msfa-services/role.service';
import { combineLatest, Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';

@Component({
  selector: 'msfa-my-account',
  templateUrl: './my-account.component.html',
  styleUrls: ['./my-account.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MyAccountComponent {
  user$: Observable<Employee> = this.userService.user$;
  selectedOrganization$: Observable<Organization> = this.userService.selectedOrganization$;
  organizations$: Observable<Organization[]> = this.userService.organizations$;
  otherOrganizations$: Observable<Organization[]> = combineLatest([
    this.organizations$,
    this.selectedOrganization$,
  ]).pipe(
    filter(([organizations, selectedOrganization]) => !!(organizations?.length && selectedOrganization)),
    map(([organizations, selectedOrganization]) =>
      organizations.filter(organization => organization.organizationNumber !== selectedOrganization.organizationNumber)
    )
  );
  allRoles: Role[] = this.roleService.allRoles;
  readonly IconType = IconType;

  constructor(private userService: UserService, private roleService: RoleService, private router: Router) {}

  loginWithOrganization(organization: Organization): void {
    this.userService.setSelectedOrganization(organization);
  }
}
