<template>
	<div>
		<div class="container">
			<input
				v-model="searchQuery"
				type="text"
				class="input"
				placeholder="Search for Songs"
			/>
			<br />
			<br />
			<table class="table is-striped">
				<thead>
					<tr>
						<td>Thumbnail</td>
						<td>Title</td>
						<td>Artists</td>
						<td>Genres</td>
						<td>ID / YouTube ID</td>
						<td>Requested By</td>
						<td>Options</td>
					</tr>
				</thead>
				<tbody>
					<tr v-for="(song, index) in filteredSongs" :key="index">
						<td>
							<img
								class="song-thumbnail"
								:src="song.thumbnail"
								onerror="this.src='/assets/notes-transparent.png'"
							/>
						</td>
						<td>
							<strong>{{ song.title }}</strong>
						</td>
						<td>{{ song.artists.join(", ") }}</td>
						<td>{{ song.genres.join(", ") }}</td>
						<td>
							{{ song._id }}
							<br />
							<a
								:href="
									'https://www.youtube.com/watch?v=' +
										`${song.songId}`
								"
								target="_blank"
							>
								{{ song.songId }}</a
							>
						</td>
						<td>
							<user-id-to-username
								:userId="song.requestedBy"
								:link="true"
							/>
						</td>
						<td class="optionsColumn">
							<button
								class="button is-primary"
								@click="edit(song, index)"
							>
								<i class="material-icons">edit</i>
							</button>
							<button
								class="button is-success"
								@click="add(song)"
							>
								<i class="material-icons">add</i>
							</button>
							<button
								class="button is-danger"
								@click="remove(song._id, index)"
							>
								<i class="material-icons">cancel</i>
							</button>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<nav class="pagination">
			<a
				v-if="position > 1"
				class="button"
				href="#"
				@click="getSet(position - 1)"
			>
				<i class="material-icons">navigate_before</i>
			</a>
			<a
				v-if="maxPosition > position"
				class="button"
				href="#"
				@click="getSet(position + 1)"
			>
				<i class="material-icons">navigate_next</i>
			</a>
		</nav>
		<edit-song v-if="modals.editSong" />
	</div>
</template>

<script>
import { mapState, mapActions } from "vuex";

import { Toast } from "vue-roaster";

import EditSong from "../Modals/EditSong.vue";
import UserIdToUsername from "../UserIdToUsername.vue";

import io from "../../io";

export default {
	components: { EditSong, UserIdToUsername },
	data() {
		return {
			position: 1,
			maxPosition: 1,
			searchQuery: "",
			songs: []
		};
	},
	computed: {
		filteredSongs() {
			return this.songs;
			// return this.songs.filter(song => song.indexOf(song.searchQuery) !== -1);
		},
		...mapState("modals", {
			modals: state => state.modals.admin
		})
	},
	// watch: {
	//   "modals.editSong": function(value) {
	//     console.log(value);
	//     if (value === false) this.stopVideo();
	//   }
	// },
	methods: {
		getSet(position) {
			const _this = this;
			this.socket.emit("queueSongs.getSet", position, data => {
				_this.songs = data;
				this.position = position;
			});
		},
		edit(song, index) {
			const newSong = {};
			Object.keys(song).forEach(n => {
				newSong[n] = song[n];
			});

			this.editSong({ index, song: newSong, type: "queueSongs" });
			this.openModal({ sector: "admin", modal: "editSong" });
		},
		add(song) {
			this.socket.emit("songs.add", song, res => {
				if (res.status === "success")
					Toast.methods.addToast(res.message, 2000);
				else Toast.methods.addToast(res.message, 4000);
			});
		},
		remove(id) {
			this.socket.emit("queueSongs.remove", id, res => {
				if (res.status === "success")
					Toast.methods.addToast(res.message, 2000);
				else Toast.methods.addToast(res.message, 4000);
			});
		},
		init() {
			const _this = this;
			_this.socket.emit("queueSongs.index", data => {
				_this.songs = data.songs;
				_this.maxPosition = Math.round(data.maxLength / 50);
			});
			_this.socket.emit("apis.joinAdminRoom", "queue", () => {});
		},
		...mapActions("admin/songs", ["stopVideo", "editSong"]),
		...mapActions("modals", ["openModal"])
	},
	mounted() {
		const _this = this;
		io.getSocket(socket => {
			_this.socket = socket;

			_this.socket.on("event:admin.queueSong.added", queueSong => {
				_this.songs.push(queueSong);
			});
			_this.socket.on("event:admin.queueSong.removed", songId => {
				_this.songs = _this.songs.filter(song => {
					return song._id !== songId;
				});
			});
			_this.socket.on("event:admin.queueSong.updated", updatedSong => {
				for (let i = 0; i < _this.songs.length; i += 1) {
					const song = _this.songs[i];
					if (song._id === updatedSong._id) {
						_this.songs.$set(i, updatedSong);
					}
				}
			});

			if (_this.socket.connected) {
				_this.init();
			}
			io.onConnect(() => {
				_this.init();
			});
		});
	}
};
</script>

<style lang="scss" scoped>
.optionsColumn {
	width: 140px;
	button {
		width: 35px;
	}
}

.song-thumbnail {
	display: block;
	max-width: 50px;
	margin: 0 auto;
}

td {
	vertical-align: middle;
}

.is-primary:focus {
	background-color: #029ce3 !important;
}
</style>
