import {Component, OnDestroy} from '@angular/core';
import {HeaderComponent} from '../../components/header/header.component';
import {TableModule} from 'primeng/table';
import {CookieService} from 'ngx-cookie-service';
import {AuthorService} from '../../services/author.service';
import {ReplaySubject, Subscription} from 'rxjs';
import {Post} from '../../models/post';
import {Author} from '../../models/author';
import {MessageService} from 'primeng/api';
import {DatePipe} from '@angular/common';
import {Button} from 'primeng/button';
import {DialogModule} from 'primeng/dialog';
import {InputTextModule} from 'primeng/inputtext';
import {PostHomeComponent} from '../../components/post-home/post-home.component';
import {PostService} from '../../services/post.service';
import {PostFormComponent} from "../../components/post-form/post-form.component";
import {FooterComponent} from '../../components/footer/footer.component';

@Component({
  selector: 'app-my-posts',
  standalone: true,
  imports: [
    HeaderComponent,
    TableModule,
    Button,
    DatePipe,
    DialogModule,
    InputTextModule,
    PostHomeComponent,
    PostFormComponent,
    FooterComponent
  ],
  templateUrl: './my-posts.component.html',
  styleUrl: './my-posts.component.css'
})
export class MyPostsComponent implements OnDestroy {
  subs: Subscription[] = [];
  previewDialogVisibility: boolean[] = [];
  updateDialogVisibility: boolean[] = [];
  deleteDialogVisibility: boolean[] = [];
  posts: Post[] = [];
  actualAuthor: Author | undefined;


  constructor(private cookieService: CookieService,
              private postService: PostService,
              private authorService: AuthorService,
              private messageService: MessageService) {
    this.actualAuthor = this.cookieService.get('author') ? JSON.parse(this.cookieService.get('author')) : undefined;
    this.updatePosts();
  }

  failureMessage(summary: string, detail: string): void {
    this.messageService.add({
      severity: 'error',
      summary: summary,
      detail: detail,
      life: 3000,
      closable: false
    });
  }

  updatePosts(): void {
    if (this.cookieService.get('token')) {
      this.authorService.getAuthorsPosts(this.actualAuthor?.id, this.cookieService.get('token')).subscribe({
          next: posts => this.posts = posts,
          error: error => this.failureMessage("Erreur", error.message),
        }
      )
    }
  }

  deletePost(id: bigint, rowIndex: number) {
    this.postService.deletePost(id, this.cookieService.get('token')).subscribe({
      next: (_) => {
        this.updatePosts()
        this.successMessage("Post supprimé", "Ce post a été supprimé avec succès")
      },
      error: error => this.failureMessage("Erreur", error.message),
    });
    this.closeDialog(this.deleteDialogVisibility, rowIndex)
  }

  successMessage(summary: string, detail: string): void {
    this.messageService.add({
      severity: 'success',
      summary: summary,
      detail: detail,
      life: 3000,
      closable: false
    });
  }

  openDialog(dialogBooleanTab: boolean[], index: number) {
    dialogBooleanTab[index] = true;
  }

  closeDialog(dialogBooleanTab: boolean[], index: number) {
    dialogBooleanTab[index] = false;
  }

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