<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)"
						:name="t('repod', 'Play')"
						:title="t('repod', 'Play')"
						@click="load(episode)">
						<template #icon>
							<PlayButton :size="20" />
						</template>
					</NcActionButton>
					<NcActionButton v-if="isCurrentEpisode(episode)"
						:name="t('repod', 'Stop')"
						:title="t('repod', 'Stop')"
						@click="load(null)">
						<template #icon>
							<StopButton :size="20" />
						</template>
					</NcActionButton>
				</template>
				<template #extra>
					<NcActions>
						<NcActionButton v-if="episode.duration && !hasEnded(episode)"
							:disabled="loadingAction"
							:name="t('repod', 'Mark as read')"
							:title="t('repod', 'Mark as read')"
							@click="markAs(episode, true)">
							<template #icon>
								<PlaylistPlay :size="20" />
							</template>
						</NcActionButton>
						<NcActionButton v-if="episode.duration && hasEnded(episode)"
							:disabled="loadingAction"
							:name="t('repod', 'Mark as unread')"
							:title="t('repod', 'Mark as unread')"
							@click="markAs(episode, false)">
							<template #icon>
								<PlaylistRemove :size="20" />
							</template>
						</NcActionButton>
						<NcActionLink v-if="episode.link"
							:href="episode.link"
							:name="t('repod', 'Episode webpage')"
							target="_blank"
							:title="t('repod', 'Episode webpage')">
							<template #icon>
								<OpenInNew :size="20" />
							</template>
						</NcActionLink>
						<NcActionLink v-if="episode.url"
							:href="episode.url"
							:name="t('repod', 'Download')"
							target="_blank"
							:title="t('repod', 'Download')">
							<template #icon>
								<Download :size="20" />
							</template>
						</NcActionLink>
					</NcActions>
				</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, NcActionLink, NcActions, NcAvatar, NcListItem, NcModal, NcProgressBar } from '@nextcloud/vue'
import { durationToSeconds, formatEpisodeTimestamp, formatLocaleDate } from '../../utils/time.js'
import Download from 'vue-material-design-icons/Download.vue'
import Loading from '../Atoms/Loading.vue'
import Modal from '../Atoms/Modal.vue'
import OpenInNew from 'vue-material-design-icons/OpenInNew.vue'
import PlayButton from 'vue-material-design-icons/Play.vue'
import PlaylistPlay from 'vue-material-design-icons/PlaylistPlay.vue'
import PlaylistRemove from 'vue-material-design-icons/PlaylistRemove.vue'
import StopButton from 'vue-material-design-icons/Stop.vue'
import axios from '@nextcloud/axios'
import { decodeUrl } from '../../utils/url.js'
import { generateUrl } from '@nextcloud/router'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'Episodes',
	components: {
		Download,
		Loading,
		Modal,
		NcActionButton,
		NcActionLink,
		NcActions,
		NcAvatar,
		NcListItem,
		NcModal,
		NcProgressBar,
		OpenInNew,
		PlayButton,
		PlaylistPlay,
		PlaylistRemove,
		StopButton,
	},
	data() {
		return {
			episodes: [],
			loading: true,
			loadingAction: false,
			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.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)
		},
		async markAs(episode, read) {
			try {
				this.loadingAction = true
				const action = {
					podcast: this.url,
					episode: episode.url,
					guid: episode.guid,
					action: 'play',
					timestamp: formatEpisodeTimestamp(new Date()),
					started: episode.action ? episode.action.started : 0,
					position: read ? durationToSeconds(episode.duration) : 0,
					total: durationToSeconds(episode.duration),
				}
				await axios.post(generateUrl('/apps/gpoddersync/episode_action/create'), [action])
				this.episodes = this.episodes.map((e) => {
					if (e.url === episode.url) {
						e.action = action
					}
					return e
				})
			} catch (e) {
				console.error(e)
				showError(t('repod', 'Could not change the status of the episode'))
			} finally {
				this.loadingAction = false
			}
		},
	},
}
</script>

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

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