<template>
	<div>
		<Loading v-if="loading" />
		<ul v-if="!loading">
			<NcListItem v-for="episode in filteredEpisodes"
				:key="episode.guid"
				:active="isCurrentEpisode(episode)"
				:class="hasEnded(episode) ? 'ended': ''"
				:details="formatLocaleDate(new Date(episode.pubDate?.date))"
				:force-display-actions="true"
				:name="episode.name"
				:title="episode.description"
				@click="modalEpisode = episode">
				<template #actions>
					<NcActionButton v-if="!isCurrentEpisode(episode)" @click="load(episode)">
						<template #icon>
							<PlayButton :size="20" />
						</template>
						{{ t('repod', 'Play') }}
					</NcActionButton>
					<NcActionButton v-if="isCurrentEpisode(episode)" @click="load(null)">
						<template #icon>
							<StopButton :size="20" />
						</template>
						{{ t('repod', 'Stop') }}
					</NcActionButton>
				</template>
				<template #icon>
					<NcAvatar :display-name="episode.name"
						:is-no-user="true"
						:url="episode.image" />
				</template>
				<template #indicator>
					<NcProgressBar v-if="isListening(episode)"
						class="progress"
						:value="episode.action.position * 100 / episode.action.total" />
				</template>
				<template #subname>
					{{ episode.duration }}
				</template>
			</NcListItem>
		</ul>
		<NcModal v-if="modalEpisode" @close="modalEpisode = null">
			<Modal :description="modalEpisode.description"
				:image="modalEpisode.image"
				:link="modalEpisode.link"
				:name="modalEpisode.name"
				:size="modalEpisode.size"
				:title="modalEpisode.title"
				:url="modalEpisode.url" />
		</NcModal>
	</div>
</template>

<script>
import { NcActionButton, NcAvatar, NcListItem, NcModal, NcProgressBar } from '@nextcloud/vue'
import Loading from '../Atoms/Loading.vue'
import Modal from '../Atoms/Modal.vue'
import PlayButton from 'vue-material-design-icons/Play.vue'
import StopButton from 'vue-material-design-icons/Stop.vue'
import axios from '@nextcloud/axios'
import { decodeUrl } from '../../utils/url.js'
import { formatLocaleDate } from '../../utils/time.js'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'Episodes',
	components: {
		Loading,
		Modal,
		NcActionButton,
		NcAvatar,
		NcListItem,
		NcModal,
		NcProgressBar,
		PlayButton,
		StopButton,
	},
	data() {
		return {
			episodes: [],
			loading: true,
			modalEpisode: null,
		}
	},
	computed: {
		currentEpisode() {
			return this.$store.state.player.episode
		},
		filters() {
			return this.$store.state.settings.filters
		},
		filteredEpisodes() {
			return this.episodes.filter((episode) => {
				if (!this.filters.listened && this.hasEnded(episode)) {
					return false
				}

				if (!this.filters.listening && this.isListening(episode)) {
					return false
				}

				if (!this.filters.unlistened && !this.isListening(episode)) {
					return false
				}

				return true
			})
		},
		url() {
			return decodeUrl(this.$route.params.url)
		},
	},
	async mounted() {
		try {
			this.loading = true
			const episodes = await axios.get(generateUrl('/apps/repod/episodes/list?url={url}', { url: this.url }))
			this.episodes = [...episodes.data].sort((a, b) => new Date(b.pubDate?.date) - new Date(a.pubDate?.date))
		} catch (e) {
			console.error(e)
			showError(t('repod', 'Could not fetch episodes'))
		} finally {
			this.loading = false
		}
	},
	methods: {
		formatLocaleDate,
		hasEnded(episode) {
			return episode.action && (episode.action.action === 'DELETE' || (
				episode.action.position > 0
				&& episode.action.total > 0
				&& episode.action.position >= episode.action.total
			))
		},
		isCurrentEpisode(episode) {
			return this.currentEpisode && this.currentEpisode.url === episode.url
		},
		isListening(episode) {
			return episode.action && episode.action.action === 'PLAY' && !this.hasEnded(episode)
		},
		load(episode) {
			this.$store.dispatch('player/load', episode)
		},
	},
}
</script>

<style scoped>
	.ended {
		opacity: .4;
	}

	.progress {
		margin-top: .4rem;
	}
</style>
