import {
	CHANGE_APP_ROOT,
	CHANGE_APP_ROOT_SUCCESS,
	UPDATE_APP_STATE,
	UPDATE_SHOW_NOTIFICATION,
	GET_WIFISSID_SUCCESS,
	GET_WIFISSID_ERROR,
	ACCEPT_TNC_LICENSE,
	ACCEPT_TNC_LICENSE_SUCCESS,
	ACCEPT_TNC_LICENSE_ERROR,
	INC_SHARED_PLAYLIST_TUTORIAL,
	START_REORDERING_PHOTOS_IN_PLAYLIST,
	DONE_REORDERING_PHOTOS_IN_PLAYLIST,
	SET_LOCATION_SETTING,
	HAS_LOGGED_IN_ONCE,
	SET_FROM_SPLASH_SCREEN,
	UNSET_FROM_SPLASH_SCREEN,
	UPDATE_LOCATION_COORDINATES,
} from '~/store/actionTypes';
import { SHARED_PLAYLIST_ONBOARDING_PRESPLASH, SHOP_LOCATIONS } from '~/store/contentTypes';


// const validRoots = ['splash', 'login', 'main', 'home', 'shop'];
const initialState = {
	root: 'splash',
	appStatus: 'active',
	showNotification: false,
	ssid: '',
	config: {},
	location: SHOP_LOCATIONS,
	// 0 - App not yet opened
	// 1 - App opened but Photos tab not visited
	// 2 - Photos tab visited (terminal state)
	sharedPlaylistOnboardingTutorialState: SHARED_PLAYLIST_ONBOARDING_PRESPLASH,
	hasLoggedInOnce: false,
	fromSplashScreen: false,
};

export const app = (state = initialState, action) => {
	switch (action.type) {
		case CHANGE_APP_ROOT:
			return { ...state, root: action.payload.root };
		case CHANGE_APP_ROOT_SUCCESS:
			return { ...state, root: action.payload.root };
		case UPDATE_APP_STATE:
			return { ...state, appStatus: action.payload };
		case UPDATE_SHOW_NOTIFICATION:
			return { ...state, showNotification: action.payload };
		case GET_WIFISSID_SUCCESS:
			return { ...state, ssid: action.payload.ssid };
		case GET_WIFISSID_ERROR:
			return { ...state, ssid: '' };
		case ACCEPT_TNC_LICENSE:
			return { ...state, config: { user_accepted_tnc: true } };
		case ACCEPT_TNC_LICENSE_SUCCESS:
			return state;
		case ACCEPT_TNC_LICENSE_ERROR:
			return state;
		case INC_SHARED_PLAYLIST_TUTORIAL:
			return { ...state, sharedPlaylistOnboardingTutorialState: state.sharedPlaylistOnboardingTutorialState + 1 };
		case START_REORDERING_PHOTOS_IN_PLAYLIST:
			return { ...state, reorderingPhotosInPlaylistState: 1 };
		case DONE_REORDERING_PHOTOS_IN_PLAYLIST:
			return { ...state, reorderingPhotosInPlaylistState: 0 };
		case SET_LOCATION_SETTING:
			return { ...state, location: action.payload.location };
		case HAS_LOGGED_IN_ONCE:
			return { ...state, hasLoggedInOnce: true };
		case SET_FROM_SPLASH_SCREEN:
			return { ...state, fromSplashScreen: true };
		case UNSET_FROM_SPLASH_SCREEN:
			return { ...state, fromSplashScreen: false };
		case UPDATE_LOCATION_COORDINATES: {
			return {
				...state,
				config: action.payload,
			};
		}
		default:
			return state;
	}
};
