import { Component, OnDestroy, Input } from '@angular/core';
import { DecimalPipe } from '@angular/common';
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 'src/app/core/models';
import { JumpsService } from 'src/app/core/services';
import { environment } from 'src/environments/environment';

@Component({
    selector: 'huapp-jump-preview',
    styleUrls: ['jump-preview.component.scss'],
    templateUrl: './jump-preview.component.html',
    standalone: true,
    imports: [
        DecimalPipe, RouterLink,
        MatDividerModule, MatButtonModule, MatCardModule,
        JumpMetaComponent
    ]
})
export class JumpPreviewComponent implements OnDestroy {
    @Input() jump!: Jump;
    @Input() isUser!: boolean;
    @Input() showMeta!: boolean;
    private _jump: Subscription = new Subscription();
    isDeleting = false;

    constructor(
        private router: Router,
        private jumpsService: JumpsService
    ) { }

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

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