<script>
import { mapState } from "vuex";
import Toast from "toasters";
import draggable from "vuedraggable";

export default {
	components: { draggable },
	data() {
		return {
			orderOfPlaylists: [],
			drag: false
		};
	},
	computed: {
		...mapState({
			station: state => state.station.station,
			myUserId: state => state.user.auth.userId
		}),
		playlists: {
			get() {
				return this.$store.state.user.playlists.playlists;
			},
			set(playlists) {
				this.$store.commit("user/playlists/setPlaylists", playlists);
			}
		},
		dragOptions() {
			return {
				animation: 200,
				group: "playlists",
				disabled: this.myUserId !== this.userId,
				ghostClass: "draggable-list-ghost"
			};
		}
	},
	mounted() {
		this.socket.on(
			"event:playlist.create",
			res => this.playlists.push(res.data.playlist),
			{ replaceable: true }
		);

		this.socket.on(
			"event:playlist.delete",
			res => {
				this.playlists.forEach((playlist, index) => {
					if (playlist._id === res.data.playlistId) {
						this.playlists.splice(index, 1);
					}
				});
			},
			{ replaceable: true }
		);

		this.socket.on(
			"event:playlist.addSong",
			res => {
				this.playlists.forEach((playlist, index) => {
					if (playlist._id === res.data.playlistId) {
						this.playlists[index].songs.push(res.data.song);
					}
				});
			},
			{ replaceable: true }
		);

		this.socket.on(
			"event:playlist.removeSong",
			res => {
				this.playlists.forEach((playlist, index) => {
					if (playlist._id === res.data.playlistId) {
						this.playlists[index].songs.forEach((song, index2) => {
							if (song.youtubeId === res.data.youtubeId) {
								this.playlists[index].songs.splice(index2, 1);
							}
						});
					}
				});
			},
			{ replaceable: true }
		);

		this.socket.on(
			"event:playlist.updateDisplayName",
			res => {
				this.playlists.forEach((playlist, index) => {
					if (playlist._id === res.data.playlistId) {
						this.playlists[index].displayName =
							res.data.displayName;
					}
				});
			},
			{ replaceable: true }
		);

		this.socket.on(
			"event:playlist.updatePrivacy",
			res => {
				this.playlists.forEach((playlist, index) => {
					if (playlist._id === res.data.playlist._id) {
						this.playlists[index].privacy =
							res.data.playlist.privacy;
					}
				});
			},
			{ replaceable: true }
		);

		this.socket.on(
			"event:user.orderOfPlaylists.changed",
			res => {
				const sortedPlaylists = [];

				this.playlists.forEach(playlist => {
					sortedPlaylists[
						res.data.order.indexOf(playlist._id)
					] = playlist;
				});

				this.playlists = sortedPlaylists;
				this.orderOfPlaylists = this.calculatePlaylistOrder();
			},
			{ replaceable: true }
		);
	},
	methods: {
		calculatePlaylistOrder() {
			const calculatedOrder = [];
			this.playlists.forEach(playlist =>
				calculatedOrder.push(playlist._id)
			);

			return calculatedOrder;
		},
		savePlaylistOrder() {
			const recalculatedOrder = this.calculatePlaylistOrder();
			if (
				JSON.stringify(this.orderOfPlaylists) ===
				JSON.stringify(recalculatedOrder)
			)
				return; // nothing has changed

			this.socket.dispatch(
				"users.updateOrderOfPlaylists",
				recalculatedOrder,
				res => {
					if (res.status === "error") return new Toast(res.message);

					this.orderOfPlaylists = this.calculatePlaylistOrder(); // new order in regards to the database
					return new Toast(res.message);
				}
			);
		}
	}
};
</script>
