<template>
	<div>
		<Loading v-if="loading" />
		<AdaptativeList v-if="!loading">
			<NcListItem v-for="episode in episodes"
				:key="episode.guid"
				:active="isCurrentEpisode(episode)"
				:class="episode.action && episode.action.position >= episode.action.total ? 'ended': ''"
				:details="moment(episode.pubDate.date).fromNow()"
				:force-display-actions="true"
				:name="episode.name"
				:title="episode.description"
				@click="modalEpisode = episode">
				<template #icon>
					<NcAvatar :display-name="episode.name"
						:is-no-user="true"
						:url="episode.image" />
				</template>
				<template #subname>
					{{ episode.duration > 0 ? formatTimer(new Date(episode.duration*1000)) : '' }}
				</template>
				<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>
			</NcListItem>
		</AdaptativeList>
		<NcModal v-if="modalEpisode"
			@close="closeModal">
			<Modal :description="modalEpisode.description"
				:image="modalEpisode.image"
				:link="modalEpisode.link"
				:name="modalEpisode.name"
				:title="title"
				:url="modalEpisode.url" />
		</NcModal>
	</div>
</template>

<script>
import { NcActionButton, NcAvatar, NcListItem, NcModal } from '@nextcloud/vue'
import { decodeUrl, encodeUrl } from '../../utils/url.js'
import AdaptativeList from '../Atoms/AdaptativeList.vue'
import Loading from '../Atoms/Loading.vue'
import Modal from './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 { formatTimer } from '../../utils/time.js'
import { generateUrl } from '@nextcloud/router'
import moment from '@nextcloud/moment'
import { showError } from '@nextcloud/dialogs'

export default {
	name: 'Episodes',
	components: {
		AdaptativeList,
		Loading,
		Modal,
		NcActionButton,
		NcAvatar,
		NcListItem,
		NcModal,
		PlayButton,
		StopButton,
	},
	props: {
		title: {
			type: String,
			required: true,
		},
	},
	data() {
		return {
			episodes: [],
			loading: true,
			modalEpisode: null,
		}
	},
	computed: {
		currentEpisode() {
			return this.$store.state.player.episode
		},
		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
		} catch (e) {
			console.error(e)
			showError(t('repod', 'Could not fetch episodes'))
		} finally {
			this.loading = false
		}
	},
	methods: {
		formatTimer,
		moment,
		isCurrentEpisode(episode) {
			return this.currentEpisode && this.currentEpisode.url === episode.url
		},
		load(episode) {
			this.$store.dispatch('player/load', episode)
		},
		closeModal() {
			this.modalEpisode = null
			this.$router.push(encodeUrl(this.url))
		},
	},
}
</script>

<style scoped>
	.ended {
		opacity: .5;
	}
</style>
