import {Component, OnDestroy} from '@angular/core';
import {HeaderComponent} from '../../components/header/header.component';
import {ActivatedRoute, Router} from '@angular/router';
import {CookieService} from 'ngx-cookie-service';
import {Author} from '../../models/author';
import {Subscription} from 'rxjs';
import {AuthorService} from '../../services/author.service';
import {JsonPipe} from '@angular/common';
import {AvatarModule} from 'primeng/avatar';
import {CardModule} from 'primeng/card';
import {Button} from 'primeng/button';

@Component({
  selector: 'app-profile',
  standalone: true,
  imports: [
    HeaderComponent,
    JsonPipe,
    AvatarModule,
    CardModule,
    Button
  ],
  templateUrl: './profile.component.html',
  styleUrl: './profile.component.css'
})
export class ProfileComponent implements OnDestroy{
  actualAuthor: Author | undefined;
  concernedAuthor: Author | undefined;
  subs: Subscription[] = [];
  constructor(private route: ActivatedRoute,
              private authorService: AuthorService,
              private cookieService: CookieService) {
    this.route.paramMap.subscribe(params => {
      this.subs.push(this.authorService.getAuthor(params.get('authorId')).subscribe(author => {
        this.concernedAuthor = author;
      }));
    })
    if (this.cookieService.get('author')) {
      this.actualAuthor = JSON.parse(this.cookieService.get("author"));
    }
  }

  ngOnDestroy(): void {
    this.subs.forEach(sub => sub.unsubscribe());
  }

}
