import { Component, OnDestroy, Input, inject } from '@angular/core';
import { Router, RouterLink } from '@angular/router';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';
import { Observable, Subscription } from 'rxjs';

import { JumpMetaComponent } from './jump-meta.component';
import { Jump } from '@models';
import { JumpsService } from '@services';

@Component({
    selector: 'app-jump-preview',
    imports: [RouterLink, MatDividerModule, MatButtonModule, MatCardModule, JumpMetaComponent],
    styleUrl: './jump-preview.component.scss',
    templateUrl: './jump-preview.component.html',
})
export class JumpPreviewComponent implements OnDestroy {
    private router = inject(Router);
    private jumpsService = inject(JumpsService);

    @Input() jump!: Jump;
    @Input() isUser!: boolean;
    @Input() showMeta!: boolean;
    private _jump: Subscription = new Subscription();
    isDeleting = false;

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

    deleteJump() {
        this.isDeleting = true;
        const jump$: Observable<boolean> = this.jumpsService.destroy(this.jump.slug);
        this._jump = jump$.subscribe({
            next: () => {
                this.router.navigateByUrl('/');
            },
        });
    }
}
