import { Component, computed, inject, OnInit, signal } from '@angular/core';
import { ActivatedRoute, RouterOutlet } from '@angular/router';
import { User } from '@app/domain/users/user.model';
import { Location, NgTemplateOutlet } from '@angular/common';
import { UntilDestroy } from '@ngneat/until-destroy';
import { environment } from '@env/environment';
import { ChipsComponent } from '@app/shared/components/chips/chips.component';
import { ReseauxComponent } from '@app/shared/components/reseaux/reseaux.component';
import { UpdateUserComponent } from '@app/shared/features/update-user/update-user.component';
import { MyProfileProjectListComponent } from '@app/shared/components/my-profile-project-list/my-profile-project-list.component';
import { MyProfileUpdateFormComponent } from '@app/shared/components/my-profile-update-form/my-profile-update-form.component';
import { PdfViewerComponent } from '@app/shared/features/pdf-viewer/pdf-viewer.component';
import { ProfileFacade } from '@app/adapters/profiles/profile.facade';
import { UserFacade } from '@app/adapters/users/user.facade';
import { LoadingComponent } from '@app/shared/components/loading/loading.component';
import { SettingsComponent } from '@app/shared/features/settings/settings.component';
import { MyProfileMapComponent } from '@app/shared/features/my-profile-map/my-profile-map.component';

@Component({
  selector: 'app-my-profile',
  standalone: true,
  imports: [
    ChipsComponent,
    ReseauxComponent,
    UpdateUserComponent,
    MyProfileProjectListComponent,
    RouterOutlet,
    MyProfileUpdateFormComponent,
    PdfViewerComponent,
    LoadingComponent,
    SettingsComponent,
    NgTemplateOutlet,
    MyProfileMapComponent,
  ],
  providers: [UserFacade],
  templateUrl: './my-profile.component.html',
  styleUrl: './my-profile.component.scss',
})
@UntilDestroy()
export class MyProfileComponent implements OnInit {
  protected readonly environment = environment;
  protected menu = signal<string>('location');

  protected location = inject(Location);
  protected readonly route = inject(ActivatedRoute);

  protected extraData: { user: User } = this.route.snapshot.data['user'];

  private readonly userFacade = inject(UserFacade);
  protected user = this.userFacade.user;
  protected readonly userLoading = this.userFacade.loading;

  private readonly profileFacade = new ProfileFacade();
  protected profile = this.profileFacade.profile;
  protected readonly loading = this.profileFacade.loading;
  protected readonly error = this.profileFacade.error;

  protected isProfileVisible = computed<boolean>(() => {
    return this.user().isUserVisible! && this.profile().isProfileVisible!;
  });

  protected missingFields = computed<string[]>(() => {
    let fields: string[] = [];
    return fields.concat(this.user().missingFields!, this.profile().missingFields!);
  });

  ngOnInit(): void {
    if (this.extraData != undefined) {
      this.profileFacade.loadOneByUserId(this.extraData.user.id);
      this.userFacade.loadOne(this.extraData.user.id);
    }
  }
}
