import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { Season } from '../../models/season.model'; import { SeasonService } from '../../../core/services/season.service'; import { trigger, state, style, transition, animate, } from '@angular/animations'; import { Episode } from '../../models/episode.models'; import { EpisodeSeenService } from '../../../core/services/episode-seen.service'; @Component({ selector: 'app-season-main-card', templateUrl: './season-main-card.component.html', styleUrl: './season-main-card.component.css', animations: [ trigger('fadeInOut', [ transition(':enter', [ // void => * style({ opacity: 0 }), animate('0.5s ease-in', style({ opacity: 1 })), ]), transition(':leave', [ // * => void animate('0.5s ease-out', style({ opacity: 0 })), ]), ]), ], }) export class SeasonMainCardComponent implements OnInit { @Output() episodeWatchedEvent = new EventEmitter(); @Input() season: Season = {} as Season; isChecked: boolean = false; isLocked: boolean = false; constructor( private seasonService: SeasonService, private episodeSeenService: EpisodeSeenService ) {} ngOnInit(): void { // TODO: GET SEASON EPISODES this.seasonService.getSeasonEpisodes(this.season.id).subscribe({ next: (episodes) => { console.log(`Loaded Season Episodes: `, episodes); this.season.episodes = episodes; }, error: (error) => { console.log(`Couldn't fetch season episodes data:`, error); // TODO show notification }, }); } get posterPath() { if (this.season.poster_path == null) return 'assets/images/default/default-poster.png'; return 'http://image.tmdb.org/t/p/w154/' + this.season.poster_path; } get episodesWatchedPercentage() { let count = 0; if (!this.season.episodes) return 0; this.season.episodes.forEach((ep) => { if (ep.date_first_seen != null) count++; }); if (this.season.episode_count == null) return 0; let value = (count * 100) / this.season.episode_count; return value; } get episodesWatchedCount() { let count = 0; if (!this.season.episodes) return 0; this.season.episodes.forEach((ep) => { if (ep.date_first_seen != null) count++; }); return count; } episodeMarkSeenClick() { if (this.isLocked) return; this.isLocked = true; // starts looping from the last available episode for (let i = this.season.episode_count; i >= 0; i--) { // ? untils it finds that the previous episode episode is marked as seen // ? > in that case it chooses the current one to mark if (i == 0 || this.season.episodes[i - 1].date_first_seen != null) { let currentEpisode = this.season.episodes[i]; // todo: show episode seen modal this.episodeSeenService .createEpisodeSeen( this.season.entry_id, currentEpisode.id, new Date(), 'Me, My Self & I' ) .subscribe({ next: (episodeSeen) => { console.log(`Episode marked as seen`, episodeSeen); this.season.episodes[i].date_first_seen = new Date(); this.season.episodes[i].date_last_seen = new Date(); this.episodeWatchedEvent.emit(); // TODO show notification }, error: (error) => { console.log(`Couldn't mark episode as seen:`, error); // TODO show notification }, complete: () => { this.isLocked = false; }, }); break; } } } }