import { Component, computed, effect, inject, OnInit } from '@angular/core';
import { ActivatedRoute, RouterLink } from '@angular/router';
import { UpperCasePipe } from '@angular/common';
import { User } from '@app/domain/users/user.model';
import { ChipsComponent } from '@app/shared/components/chips/chips.component';
import { ReseauxComponent } from '@app/shared/components/reseaux/reseaux.component';
import { UntilDestroy } from '@ngneat/until-destroy';
import { ProjectListComponent } from '@app/shared/components/project-list/project-list.component';
import { environment } from '@env/environment';
import { WebShareService } from '@app/infrastructure/shareData/web-share.service';
import { ProfileFacade } from '@app/adapters/profiles/profile.facade';
import { ProfileViewModel } from '@app/adapters/profiles/profile.presenter.model';
import { ActionType } from '@app/domain/action-type.util';
import { UserFacade } from '@app/adapters/users/user.facade';

@Component({
  selector: 'app-profile-detail',
  standalone: true,
  imports: [ChipsComponent, ReseauxComponent, RouterLink, UpperCasePipe, ProjectListComponent],
  providers: [UserFacade],
  templateUrl: './profile-detail.component.html',
  styleUrl: './profile-detail.component.scss',
})
@UntilDestroy()
export class ProfileDetailComponent implements OnInit {
  private readonly webShare = inject(WebShareService);
  private readonly profileFacade = inject(ProfileFacade);
  private readonly userFacade = inject(UserFacade);
  protected readonly environment = environment;
  protected readonly ActionType = ActionType;
  private readonly route = inject(ActivatedRoute);

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

  slug = computed(() => this.route.snapshot.params['name'] ?? '');

  protected user = this.userFacade.user;
  protected readonly userLoading = this.userFacade.loading;
  protected readonly userError = this.userFacade.error;

  protected profile = this.profileFacade.profile;
  protected readonly profileLoading = this.profileFacade.loading;
  protected readonly profileError = this.profileFacade.error;

  constructor() {
    effect(() => {
      if (!this.profileLoading().isLoading) {
        switch (this.profileLoading().action) {
          case ActionType.READ:
            if (!this.profileError().hasError) {
              this.profile = this.profileFacade.profile;
            }
            break;
        }
      }
      if (!this.userLoading().isLoading) {
        switch (this.userLoading().action) {
          case ActionType.READ:
            if (!this.userError().hasError) {
              this.user = this.userFacade.user;
            }
            break;
        }
      }
    });
  }

  ngOnInit() {
    if (this.extraData === undefined) {
      const extractSlug = this.slug().split('-');
      const profileId = extractSlug[extractSlug.length - 1];
      const userId = extractSlug[extractSlug.length - 2];
      this.profileFacade.loadOne(profileId);
      this.userFacade.loadOne(userId);
    } else {
      this.profile.set(this.extraData.profile);
      this.user.set(this.extraData.user);
    }
  }

  async onShare() {
    if (!this.profile) return;
    const fullUrl = `${window.location.origin}/profiles/${this.slug()}`;

    await this.webShare.share({
      title: `Découvrez le profil de ${this.profile.name}`,
      text: `Jette un œil à ce profil intéressant sur notre application !`,
      url: fullUrl,
    });
  }
}
