import _ from 'lodash';
import {
	GET_GOOGLE_TOKEN,
	SET_GOOGLE_INFO,
	GET_GOOGLE_TOKEN_SUCCESS,
	GET_GOOGLE_TOKEN_ERROR,
	LOGOUT_FROM_GOOGLE_SUCCESS,
	LOGOUT_FROM_GOOGLE_ERROR,
	GET_GOOGLE_ALBUMS,
	GET_GOOGLE_ALBUMS_SUCCESS,
	GET_GOOGLE_ALBUMS_ERROR,
	GET_GOOGLE_CATEGORIES,
	GET_GOOGLE_CATEGORIES_SUCCESS,
	GET_GOOGLE_CATEGORIES_ERROR,
	GET_DYNAMIC_PLAYLISTS_SUCCESS,
	GET_DYNAMIC_PLAYLISTS_ERROR,
	GET_GOOGLE_ALBUM_CONTENT_SUCCESS,
} from '~/store/actionTypes';
import { updateItemInArray } from './helper';

export const initialState = {
	auth: {},
	allPhotos: {},
	albums: [],
	categories: [],
	playlists: [],
	meta: {
		loading: {
			albums: false,
			categories: false,
		},
	},
};

function getAlbumsWithSynced(googleId, googlePlaylists, albums) {
	const validGooglePlaylists = googlePlaylists.filter((item) => item.googleId === googleId);
	const finalAlbums = albums.map((item) => {
		if (_.findIndex(validGooglePlaylists, { albumId: item.id }) > -1) {
			return { ...item, alreadySynced: true };
		}
		return item;
	});
	return finalAlbums;
}

function getCategoriesWithSynced(googleId, googlePlaylists, categories) {
	const validGooglePlaylists = googlePlaylists.filter((item) => item.googleId === googleId);
	const finalCategories = categories.map((item) => {
		if (_.findIndex(
			validGooglePlaylists,
			{ filters: { contentFilter: { includedContentCategories: [item.id] } } },
		) > -1) {
			return { ...item, alreadySynced: true };
		}
		return item;
	});
	return finalCategories;
}

function getLibraryWithSynced(googleId, googlePlaylists, allPhotos) {
	const alreadySynced = _.findIndex(googlePlaylists, { filters: { mediaTypeFilter: { mediaTypes: ['PHOTO'] } }, googleId }) > -1;
	return { ...allPhotos, alreadySynced };
}

export const google = (state = initialState, { type, payload }) => {
	switch (type) {
		case SET_GOOGLE_INFO:
			return { ...state, auth: payload };
		case GET_GOOGLE_TOKEN:
			return { ...state, auth: _.omit(state.auth, 'error') };
		case GET_GOOGLE_TOKEN_SUCCESS:
			if (payload.googleToken.valid && payload.googleToken.googleId === state.auth.email) {
				return state;
			} else if (payload.error) {
				return { ...state, auth: payload };
			}
			return initialState;
		case LOGOUT_FROM_GOOGLE_SUCCESS:
		case LOGOUT_FROM_GOOGLE_ERROR:
		case GET_GOOGLE_TOKEN_ERROR:
			return initialState;
		case GET_GOOGLE_ALBUMS:
			return { ...state, meta: { loading: { ...state.meta.loading, albums: true } } };
		case GET_GOOGLE_CATEGORIES:
			return { ...state, meta: { loading: { ...state.meta.loading, categories: true } } };
		case GET_GOOGLE_ALBUMS_ERROR:
			return { ...state, meta: { loading: { ...state.meta.loading, albums: false } } };
		case GET_GOOGLE_CATEGORIES_ERROR:
			return { ...state, meta: { loading: { ...state.meta.loading, categories: false } } };
		case GET_GOOGLE_ALBUMS_SUCCESS:
			return {
				...state,
				meta: { loading: { ...state.meta.loading, albums: false } },
				albums: getAlbumsWithSynced(state.auth.email, state.playlists, payload),
			};
		case GET_GOOGLE_CATEGORIES_SUCCESS:
			return {
				...state,
				meta: { loading: { ...state.meta.loading, categories: false } },
				categories: getCategoriesWithSynced(state.auth.email, state.playlists, payload.categories),
				allPhotos: getLibraryWithSynced(state.auth.email, state.playlists, payload.allPhotos),
			};
		case GET_DYNAMIC_PLAYLISTS_SUCCESS:
			return {
				...state,
				albums: getAlbumsWithSynced(state.auth.email, payload.google, state.albums),
				categories: getCategoriesWithSynced(state.auth.email, payload.google, state.categories),
				allPhotos: getLibraryWithSynced(state.auth.email, state.playlists, payload.allPhotos),
				playlists: payload.google,
			};
		case GET_GOOGLE_ALBUM_CONTENT_SUCCESS:
			return {
				...state,
				albums: updateItemInArray(state.albums, 'id', payload.id, (a) => ({ ...a, mediaItems: payload.mediaItems })),
			};
		case GET_DYNAMIC_PLAYLISTS_ERROR:
		default:
			return state;
	}
};
