import { IMovie } from "@autoplex-api/request/dist/typings";
import jwtDecode from "jwt-decode";
import { MutationTree } from "vuex";
import { IState } from "./state";

/**
 * All available mutation types
 */
export enum Mutation {

	LockScroll = "LOCK_SCROLL",

	UserForget = "USER_FORGET",
	UserLoad   = "USER_LOAD",
	UserStore  = "USER_STORE",

	FreeMovies = "FREE_MOVIES",
	StoreMovies = "STORE_MOVIES",
}

/**
 * Type declarations for each mutation
 */
export type MutationsTypes<S = IState> = {
	[Mutation.UserForget]: (state: S) => void,
	[Mutation.UserLoad]  : (state: S, token: string) => boolean,
	[Mutation.UserStore] : (state: S, remember: boolean) => void,
	[Mutation.LockScroll]: (state: S, lock: boolean) => void,

	// Movie Store
	[Mutation.FreeMovies]: (state: S, movies: IMovie[]) => void,
	[Mutation.StoreMovies]: (state: S, movies: IMovie[]) => void
}

/**
 * The auth mutations
 */
export const mutations: MutationsTypes & MutationTree<IState> = {

	[Mutation.LockScroll](state: IState, lock: boolean) {
		if (lock) {
			// document.body.style.position = "fixed";
			// document.body.style.top = `-${window.scrollY}px`;
			document.body.style.paddingRight = `${window.innerWidth - document.body.clientWidth}px`;
			document.body.style.overflow = "hidden";
		} else {
			// const scrollY = document.body.style.top;
			// document.body.style.position = "";
			// document.body.style.top = "";
			document.body.style.paddingRight = "";
			document.body.style.overflow = "";
			// window.scrollTo(0, parseInt(scrollY || '0') * -1);
		}
	},

	/**
	 * Log the user out and remove the token from storage
	 */
	[Mutation.UserForget](state: IState) {
		state.user = null;
		sessionStorage.removeItem("jwt");
		localStorage.removeItem("jwt");
	},

	/**
	 * Try loading a user from the provided token
	 */
	[Mutation.UserLoad](state: IState, token: string) {
		try {
			let user: any = jwtDecode(token);
			state.user = {
				id     : user.id,
				name   : user.name,
				isAdmin: user.isAdmin,
				token  : token
			}
		} catch(e) {
			return false;
		}
		return true;
	},

	/**
	 * Store the user to remember them
	 */
	[Mutation.UserStore](state: IState, remember: boolean) {
		if (state.user == null) {
			sessionStorage.removeItem("jwt");
			localStorage.removeItem("jwt");
			return;
		}
		if (remember) {
			localStorage.setItem("jwt", state.user.token);
		} else {
			sessionStorage.setItem("jwt", state.user.token);
		}
	},

	// Movie Store ---------------------------------------------------------------------------------

	/**
	 * Free the cached movies from the store
	 */
	 [Mutation.FreeMovies](state, movies: IMovie[]) {
		for (let movie of movies) {
			if (!(movie.tmdbId in state.movies)) {
				console.warn("Attempted to free a movie not stored in the state!");
				continue;
			}
			state.movies[movie.tmdbId].refCount--;
			if (state.movies[movie.tmdbId].refCount <= 0) {
				delete state.movies[movie.tmdbId];
			}
		}
	},

	/**
	 * Insert movie references in the store for updates
	 */
	[Mutation.StoreMovies](state, movies: IMovie[]) {
		for (let movie of movies) {
			if (!(movie.tmdbId in state.movies)) {
				state.movies[movie.tmdbId] = {
					refCount: 0,
					movie
				};
			}
			state.movies[movie.tmdbId].refCount++;
		}
	}
}
