import { Title } from '@angular/platform-browser';
import { trigger, state, style, animate, transition } from '@angular/animations';
import { Component, DestroyRef, inject, OnInit, OnDestroy } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { DatePipe } from '@angular/common';
import { ActivatedRoute } from '@angular/router';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';
import { MatIconModule } from '@angular/material/icon';
import { Observable, Subscription } from 'rxjs';

import { ListErrorsComponent } from '@components/shared';
import { Errors, Article, ArticlePageData } from '@models';

@Component({
    selector: 'app-page',
    imports: [DatePipe, MatCardModule, MatDividerModule, MatIconModule, ListErrorsComponent],
    templateUrl: './page.component.html',
    styleUrl: './page.component.scss',
    animations: [
        trigger('flyInOut', [
            state('in', style({ transform: 'translateY(0)' })),
            transition('void => *', [style({ transform: 'translateY(-100%)' }), animate(200)]),
            transition('* => void', [style({ transform: 'translateY(100%)' }), animate(200)]),
        ]),
    ],
})
export class PageComponent implements OnInit, OnDestroy {
    private route = inject(ActivatedRoute);
    private titleService = inject(Title);

    private _data: Subscription = new Subscription();
    public title = 'Shop bientôt disponible!';
    public description = "Encore un peu de patience, notre shop sera mis en ligne d'ici peu.";
    public errors: Errors = { errors: {} };
    public destroyRef = inject(DestroyRef);
    public article: Article = {} as Article;

    ngOnInit() {
        const data$: Observable<{ pageData: ArticlePageData }> = this.route.data as Observable<{
            pageData: ArticlePageData;
        }>;
        this._data = data$
            .pipe(takeUntilDestroyed(this.destroyRef))
            .subscribe((data: { pageData: ArticlePageData }) => {
                this.article = data.pageData.article;
                this.title = this.article.title;
                this.titleService.setTitle(this.article.title);
                console.log(this.article);
            });
    }

    ngOnDestroy() {
        this._data.unsubscribe();
    }

    getArticleTags() {
        return this.article.tagList;
    }
}
