1 import { VideoPlaylist, VideoPlaylistElement } from '../../../../../shared/models'
3 export class PlaylistTracker {
4 private currentPlaylistElement: VideoPlaylistElement
7 private readonly playlist: VideoPlaylist,
8 private readonly playlistElements: VideoPlaylistElement[]
17 getPlaylistElements () {
18 return this.playlistElements
21 hasNextPlaylistElement (position?: number) {
22 return !!this.getNextPlaylistElement(position)
25 getNextPlaylistElement (position?: number): VideoPlaylistElement {
26 if (!position) position = this.currentPlaylistElement.position + 1
28 if (position > this.playlist.videosLength) {
32 const next = this.playlistElements.find(e => e.position === position)
34 if (!next || !next.video) {
35 return this.getNextPlaylistElement(position + 1)
41 hasPreviousPlaylistElement (position?: number) {
42 return !!this.getPreviousPlaylistElement(position)
45 getPreviousPlaylistElement (position?: number): VideoPlaylistElement {
46 if (!position) position = this.currentPlaylistElement.position - 1
52 const prev = this.playlistElements.find(e => e.position === position)
54 if (!prev || !prev.video) {
55 return this.getNextPlaylistElement(position - 1)
62 const next = this.getNextPlaylistElement()
65 return next.video.name
68 setPosition (position: number) {
69 this.currentPlaylistElement = this.playlistElements.find(e => e.position === position)
70 if (!this.currentPlaylistElement || !this.currentPlaylistElement.video) {
71 console.error('Current playlist element is not valid.', this.currentPlaylistElement)
72 this.currentPlaylistElement = this.getNextPlaylistElement()
75 if (!this.currentPlaylistElement) {
76 throw new Error('This playlist does not have any valid element')
80 setCurrentElement (playlistElement: VideoPlaylistElement) {
81 this.currentPlaylistElement = playlistElement
84 getCurrentElement () {
85 return this.currentPlaylistElement
88 getCurrentPosition () {
89 if (!this.currentPlaylistElement) return -1
91 return this.currentPlaylistElement.position