import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { UserCollectionsModalComponent } from '../../modals/list-selection-modal/user-collections-modal.component'; import { MatDialog } from '@angular/material/dialog'; import { trigger, transition, style, animate, state, } from '@angular/animations'; import { Entry } from '../../../models/entry.model'; import { LibraryService } from '../../../../core/services/library.service'; import { Router } from '@angular/router'; import { ConfirmModalComponent } from '../../modals/confirm-modal/confirm-modal.component'; @Component({ selector: 'app-main-card', templateUrl: './main-card.component.html', styleUrls: ['./main-card.component.css'], animations: [ trigger('fadeInOut', [ transition('void => topToBottom', [ style({ opacity: 0, transform: 'translateY(-50%)' }), animate( '300ms ease-out', style({ opacity: 1, transform: 'translateY(0)' }) ), ]), transition('void => bottomToTop', [ style({ opacity: 0, transform: 'translateY(50%)' }), animate( '400ms ease-out', style({ opacity: 1, transform: 'translateY(0)' }) ), ]), transition('void => leftToRight', [ style({ opacity: 0, transform: 'translateX(-50%)' }), animate( '500ms ease-out', style({ opacity: 1, transform: 'translateX(0)' }) ), ]), transition('void => rightToLeft', [ style({ opacity: 0, transform: 'translateX(50%)' }), animate( '500ms ease-out', style({ opacity: 1, transform: 'translateX(0)' }) ), ]), transition('* => void', [ animate( '400ms ease-in', style({ opacity: 0, transform: 'translateY(-50px)' }) ), ]), ]), trigger('animateDestroy', [ state('void', style({ opacity: '0' })), transition('* => void', animate('200ms ease-in')), state('void', style({ opacity: '0' })), transition('void => *', animate('200ms ease-in')), ]), ], }) export class MainCardComponent implements OnInit { @Output() onEntryRemove = new EventEmitter(); @Output() onEntryUpdate = new EventEmitter(); @Input() entry!: Entry; lockActions: boolean = false; animationDirection = 'topToBottom'; constructor( private libraryService: LibraryService, public dialog: MatDialog, private router: Router ) {} ngOnInit(): void { if (!this.entry.isInLibrary) { this.libraryService.isInLibrary(this.entry.entryId.tmdb_id).subscribe({ next: (e) => { if (e != null) { e.isInLibrary = true; this.entry = e; } }, error: (error) => {}, }); } this.setRandomDirection(); } setRandomDirection() { const directions = [ 'topToBottom', 'bottomToTop', 'leftToRight', 'rightToLeft', ]; this.animationDirection = directions[Math.floor(Math.random() * directions.length)]; } get releaseYear(): number { return new Date(this.entry.release_date).getFullYear(); } get formattedRate(): string { return this.entry.entryRating.tmdb_rating.toFixed(1); } addEntry(tmdb_id: string, is_tvserie: boolean) { // console.log('Adding Entry To Library.'); this.lockActions = true; let obs = this.libraryService.addEntry(tmdb_id, is_tvserie).subscribe({ next: (entry) => { if (entry != null) { // TODO success notification // console.log(`Entry added to library`, entry); entry.isInLibrary = true; this.entry = entry; this.libraryService.refrechEntries().subscribe(); this.onEntryUpdate.emit(this.entry.id); this.lockActions = false; obs.unsubscribe(); } }, error: (error) => { // TODO error notification console.error('Error adding entry:', error); this.lockActions = false; }, }); } removeEntry(id: number) { this.lockActions = true; const dialogRef = this.dialog.open(ConfirmModalComponent, { width: '400px', data: { title: 'Confirmation', message: `Are you sure you want to remove ${this.entry.title} from the library?`, type: 'danger', }, }); dialogRef.afterClosed().subscribe((result) => { if (!result || result.confirmed != true) { this.lockActions = false; return; } this.libraryService.deleteEntry(id).subscribe({ next: (response) => { console.log('Entry successfully deleted.', response); // ? emit to dynamic entry list this.entry.id = 0; this.entry.isInLibrary = false; this.onEntryRemove.emit(this.entry.id); this.libraryService.refrechEntries().subscribe(); // todo entry removed notification }, error: (error) => { // TODO show notification console.error('Error deleting entry:', error); }, complete: () => { this.lockActions = false; }, }); }); } showAddToListModal() { if (!this.entry) return; this.lockActions = true; const dialogRef = this.dialog.open(UserCollectionsModalComponent, { width: '400px', height: '55%', data: { entryId: this.entry.id, }, }); dialogRef.afterClosed().subscribe((result) => { this.lockActions = false; // console.log('Modal Closed: ', result); }); } cardClick() { if (this.entry.isInLibrary) this.router.navigate([`/entries/${this.entry.id}`]); else { let type = this.entry.is_tvseries ? 'tv' : 'movie'; window.open( `https://www.themoviedb.org/${type}/` + this.entry.entryId.tmdb_id, '_blank' ); } } getTmdbLink() { let type = this.entry.is_tvseries ? 'tv' : 'movie'; return `https://www.themoviedb.org/${type}/${this.entry.entryId.tmdb_id}`; } isCertainRoute(): boolean { return ( this.router.isActive('/search', true) || this.router.isActive('/popular', true) || this.router.isActive('/trending', true) ); } get posterPath() { // * Not in library, use TMDB poster if (!this.entry.isInLibrary) return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path; // * Is in library: use a local poster if (this.entry.medias) { const selectedPosters = this.entry.medias.filter( (m) => m.type == 'poster' && m.is_selected ); if ( selectedPosters.length && selectedPosters[0] && selectedPosters[0].path ) { const name = selectedPosters[0].path.split('.')[0]; const extention = selectedPosters[0].path.split('.')[1]; return `http://localhost:4000/api/v1/media/entries/posters/${name}_preview.${extention}`; } } // * No local poster available, Use TMDB poster if (this.entry.poster_path) return `https://image.tmdb.org/t/p/w300` + this.entry.poster_path; // * No poster found, use default poster image as a last stand return 'assets/images/default/default-poster.png'; } get runtime() { if (this.entry.runtime == null) return ''; return this.entry.runtime; } }