<template>
	<div v-if="episode" class="footer">
		<audio id="audio-player"
			autoplay
			preload
			:src="episode.episodeUrl"
			@durationchange="duration = audio.duration"
			@loadeddata="loading = false"
			@pause="paused = audio.paused"
			@play="paused = audio.paused"
			@seeked="currentTime = audio.currentTime"
			@timeupdate="currentTime = audio.currentTime"
			@volumechange="volume = audio.volume" />
		<NcLoadingIcon v-if="loading" />
		<ProgressBar v-if="!loading"
			:current-time="currentTime"
			:duration="duration" />
		<div v-if="!loading" class="player">
			<img :src="episode.episodeImage">
			<Infos />
			<Controls :paused="paused" />
			<Timer class="timer"
				:current-time="currentTime"
				:duration="duration" />
			<Volume class="volume" :volume="volume" />
		</div>
	</div>
</template>

<script>
import Controls from './Controls.vue'
import Infos from './Infos.vue'
import { NcLoadingIcon } from '@nextcloud/vue'
import ProgressBar from './ProgressBar.vue'
import Timer from './Timer.vue'
import Volume from './Volume.vue'

export default {
	name: 'Bar',
	components: {
		Controls,
		Infos,
		NcLoadingIcon,
		ProgressBar,
		Timer,
		Volume,
	},
	data() {
		return {
			currentTime: 0,
			duration: 0,
			loading: true,
			paused: true,
			volume: 1,
		}
	},
	computed: {
		audio() {
			return document.getElementById('audio-player')
		},
		episode() {
			return this.$store.state.player.episode
		},
	},
}
</script>

<style scoped>
	.footer {
		background-color: rgba(66, 66, 66, .9);
		bottom: 0;
		height: 6rem;
		right: 0;
		position: absolute;
		width: 100%;
		z-index: 2000;
	}

	.player {
		display: flex;
		gap: 1rem;
		height: calc(6rem - 6px);
		justify-content: space-between;
	}

	.timer {
		width: 25%;
	}

	.volume {
		width: 10%;
	}
</style>
