import _ from 'lodash';
import {
	SUBMIT_LOGIN_SUCCESS,
	LOGOUT_SUCCESS,
	GET_USER_PROFILE_SUCCESS,
	SET_USER_PROFILE_SUCCESS,
	GET_ALL_SUBSCRIPTIONS_SUCCESS,
	GET_USER_SUBSCRIPTION_SUCCESS,
	GET_USER_USAGE,
	SHOW_USER_STORAGE_FULL,
	HIDE_USER_STORAGE_FULL,
	GET_USER_PROFILE,
	GET_ALL_SUBSCRIPTIONS,
	GET_USER_SUBSCRIPTION,
	// Statistic related updates
	GET_FRAMES_SUCCESS,
	UNPAIR_FRAME_SUCCESS,
	UPDATE_CHECK_STORAGE_POPUP_SHOWN,
	DONT_DISPLAY_UPLOAD_DIALOG,
	DO_DISPLAY_UPLOAD_DIALOG,
	DISPLAY_LANGUAGE_CHANGED_POPUP,
	HIDE_LANGUAGE_CHANGED_POPUP,
	SET_FROM_LOGIN_SCREEN,
	UNSET_FROM_LOGIN_SCREEN,
	SUBMIT_CHANGE_USER_SETTINGS_SUCCESS,
	SHOW_LANGUAGE_DISCREPANCY_POPUP,
	HIDE_LANGUAGE_DISCREPANCY_POPUP,
} from '~/store/actionTypes';
import { MAX_STANDARD_STORAGE } from '~/store/frameConstants';


const userInit = {
	checkUserStorage: false,
	showStorageFull: false,
	isFullStoragePopup: false,
	displayUploadDialogue: true,
	showLanguageChangedDialog: false,
	enableLanguageDiscrepancyPopup: false,
	showLanguageDiscrepancy: false,
	todo: {
		avatar: {
			avatarChanged: true,
			tutorialDismissed: false,
		},
	},
	meta: {
		loading: false,
		error: '',
	},
};

const checkUserStorage = (state) => {
	const storage = _.get(state, 'profile.stats.storage', 0);
	const totalStorage = _.get(state, 'profile.permissions.max_storage', MAX_STANDARD_STORAGE);
	return {
		...state,
		checkUserStorage: false,
		showStorageFull: (storage >= totalStorage),
	};
};

export const user = (state = userInit, { type, payload }) => {
	switch (type) {
		case SUBMIT_LOGIN_SUCCESS:
			// expected data: token, username, email, firstName, lastName, userProfileId
			return { ...state, ...payload.user };
		case GET_ALL_SUBSCRIPTIONS:
		case GET_USER_SUBSCRIPTION:
		case GET_USER_PROFILE:
			return { ...state, meta: { ...state.meta, loading: true, error: '' } };
		case SUBMIT_CHANGE_USER_SETTINGS_SUCCESS:
			if (_.has(payload, 'data.locale')) {
				const newState = {
					...checkUserStorage(state),
					firstName: state.profile.first_name,
					lastName: state.profile.last_name,
					email: state.profile.old_email,
					meta: { loading: true, error: '' },
					profile: {
						...state.profile,
						locale: _.get(payload, 'data.locale'),
					},
				};
				return newState;
			}
			return state;
		case GET_USER_PROFILE_SUCCESS:
			return {
				...checkUserStorage({ ...state, profile: payload.profile }),
				firstName: payload.profile.first_name,
				lastName: payload.profile.last_name,
				email: payload.profile.old_email,
				meta: { loading: false, error: '' },
			};
		case SET_USER_PROFILE_SUCCESS:
			return {
				...checkUserStorage({ ...state, profile: payload.profile }),
				firstName: payload.profile.first_name,
				lastName: payload.profile.last_name,
				email: payload.profile.old_email,
				meta: { loading: false, error: '' },
			};
		case GET_ALL_SUBSCRIPTIONS_SUCCESS:
			return { ...state, subscriptions: payload.subscriptions };
		case GET_USER_SUBSCRIPTION_SUCCESS:
			return { ...state, userSubscription: payload.subscription };
		case GET_USER_USAGE:
			return { ...state, checkUserStorage: payload.checkUserStorage };
		case SHOW_USER_STORAGE_FULL:
			return {
				...state,
				// checkUserStorage: true,
				// showStorageFull: true,
				isFullStoragePopup: false,
			};
		case HIDE_USER_STORAGE_FULL:
			// return { ...state, checkUserStorage: true };
			return { ...state, checkUserStorage: true, showStorageFull: false };

		// Statistic related updates
		case GET_FRAMES_SUCCESS:
			return { ...state, stats: { ...state.stats, frames: payload.frames.length } };
		case UNPAIR_FRAME_SUCCESS:
			return { ...state, stats: { ...state.stats, frames: state.stats.frames - 1 } };
		case UPDATE_CHECK_STORAGE_POPUP_SHOWN:
			return { ...state, isFullStoragePopup: payload };
		case LOGOUT_SUCCESS:
			// persistor.purge();
			return { ...userInit, enableLanguageDiscrepancyPopup: state.enableLanguageDiscrepancyPopup };
		case DONT_DISPLAY_UPLOAD_DIALOG:
			return { ...state, displayUploadDialogue: false };
		case DO_DISPLAY_UPLOAD_DIALOG:
			return { ...state, displayUploadDialogue: true };
		case DISPLAY_LANGUAGE_CHANGED_POPUP:
			return { ...state, showLanguageChangedDialog: true };
		case HIDE_LANGUAGE_CHANGED_POPUP:
			return { ...state, showLanguageChangedDialog: false };
		case SET_FROM_LOGIN_SCREEN:
			return { ...state, fromLoginScreen: true };
		case UNSET_FROM_LOGIN_SCREEN:
			return { ...state, fromLoginScreen: false };
		case SHOW_LANGUAGE_DISCREPANCY_POPUP:
			return { ...state, showLanguageDiscrepancy: true };
		case HIDE_LANGUAGE_DISCREPANCY_POPUP:
			return { ...state, showLanguageDiscrepancy: false };
		default:
			return state;
	}
};
