import { ChangeDetectionStrategy, Component, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { UnsubscribeDirective } from '@msfa-directives/unsubscribe.directive';
import { Feature } from '@msfa-enums/feature.enum';
import { RoleEnum } from '@msfa-enums/role.enum';
import { environment } from '@msfa-environment';
import { ContactInformation } from '@msfa-models/contact-information.model';
import { Role } from '@msfa-models/role.model';
import { UserService } from '@msfa-services/api/user.service';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { DeltagareCardService } from './deltagare-card.service';

@Component({
  selector: 'msfa-deltagare-details',
  templateUrl: './deltagare-card.component.html',
  styleUrls: ['./deltagare-card.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class DeltagareCardComponent extends UnsubscribeDirective implements OnDestroy {
  activeTab$: Observable<string> = this.deltagareCardService.activeTab$;
  genomforandereferens$: Observable<number> = this.activatedRoute.params.pipe(
    map(({ genomforandeReferens }) => +genomforandeReferens)
  );

  private _activeFeatures: Feature[] = environment.activeFeatures;
  private _userRoles: Role[] = this.userService.userRolesSnapshot;

  contactInformation$: Observable<ContactInformation> = this.deltagareCardService.contactInformation$;

  constructor(
    private activatedRoute: ActivatedRoute,
    private deltagareCardService: DeltagareCardService,
    private userService: UserService
  ) {
    super();
    super.unsubscribeOnDestroy(
      this.activatedRoute.params.subscribe(params => {
        this.deltagareCardService.setGenomforandereferens(params.genomforandeReferens as string);
      })
    );
  }

  get deltagareTjanstVisible(): boolean {
    return this._userRoles?.some(
      role => role.type === RoleEnum.MSFA_ReportAndPlanning || role.type === RoleEnum.MSFA_ReceiveDeltagare
    );
  }

  get handledarePickerVisible(): boolean {
    return this._userRoles?.some(role => role.type === RoleEnum.MSFA_ReceiveDeltagare);
  }

  get reportingTabVisible(): boolean {
    return (
      this._activeFeatures.includes(Feature.REPORTING) &&
      this._userRoles?.some(role => role.type === RoleEnum.MSFA_ReportAndPlanning)
    );
  }

  get experiencesVisible(): boolean {
    return this._userRoles?.some(role => role.type === RoleEnum.MSFA_ReportAndPlanning);
  }

  get sensitiveDataVisible(): boolean {
    return (
      this._activeFeatures.includes(Feature.DELTAGARE_SENSITIVE_INFORMATION) &&
      this._userRoles?.some(role => role.type === RoleEnum.MSFA_ReportAndPlanning)
    );
  }

  setActiveTab(tabId: string): void {
    this.deltagareCardService.setActiveTab(tabId);
  }
}
