import _ from 'lodash';
// import Mixpanel from 'react-native-mixpanel';
import Mixpanel from 'react-native-mixpanel';
import {
	ADD_USER_IN_REGISTRATION,
	ADD_USER_IN_LOGIN,
	GET_CURRENT_USER,
	GET_LOGGED_USERS, // combined usernames and mobile numbers
	LOGOUT_SUCCESS, // remove current user
	UPDATE_REGISTRATION_INFO,
	RESET_ALL_USERS,
	UPDATE_IS_VERIFIED,
	// API
	REQUEST_VERIFICATION_SUCCESS,
	REQUEST_VERIFICATION_ERROR,
	REQUEST_VERIFICATION,
	VERIFY_CODE,
	VERIFY_CODE_SUCCESS,
	VERIFY_CODE_ERROR,
	REGISTER_BENEFICIARY,
	REGISTER_BENEFICIARY_SUCCESS,
	REGISTER_BENEFICIARY_ERROR,
	REQUEST_LOGIN,
	REQUEST_LOGIN_SUCCESS,
	REQUEST_LOGIN_ERROR,
} from '~/store/actionTypes';

const usersInit = {
	loggedIn: [], // list of users got logged in
	currentUser: {},
	phoneNumbers: [],
	usernames: [],
	meta: {
		loading: false,
		error: '',
	},
	registration: {},
	isVerified: false,
};

export const users = (state = usersInit, { type, payload }) => {
	switch (type) {
		case ADD_USER_IN_REGISTRATION:
			return { ...state, currentUser: payload, phoneNumbers: [...state.phoneNumbers, payload.mobileNumber] };
		case ADD_USER_IN_LOGIN:
			return { ...state, currentUser: payload, usernames: [...state.usernames, payload.username] };
		case GET_CURRENT_USER:
			return state.currentUser;
		case GET_LOGGED_USERS:
			return [state.usernames, state.phoneNumbers];
		case UPDATE_REGISTRATION_INFO: {
			const { registration, loggedIn, phoneNumbers } = state;
			const updatedRegistration = { ...registration, ...payload };
			// console.log('UPDATE_REGISTRATION_INFO: ', updatedRegistration);
			if (Object.keys(updatedRegistration).length >= 5) {
				// MixPanel
				// try {
				// 	Mixpanel.trackWithProperties('volunteer', updatedRegistration);
				// } catch (e) {
				// 	console.log('Mixpanel: ', e);
				// }
				return {
					...state,
					registration: {},
					currentUser: updatedRegistration,
					loggedIn: _.flatten([...loggedIn, updatedRegistration.mobileNumber]),
					phoneNumbers: _.flatten([...phoneNumbers, updatedRegistration.mobileNumber]),
				};
			}
			return { ...state, registration: updatedRegistration };
		}
		case REQUEST_VERIFICATION:
			return { ...state, meta: { ...state.meta, loading: true, error: '' } };
		case REQUEST_VERIFICATION_SUCCESS: {
			// console.log('REQUEST_VERIFICATION_SUCCESS', payload);
			return { ...state, meta: { ...state.meta, loading: false } };
		}
		case REQUEST_VERIFICATION_ERROR:
			return { ...state, meta: { ...state.meta, loading: false, error: payload.error } };
		case VERIFY_CODE:
			return { ...state, meta: { ...state.meta, loading: true, error: '' } };
		case VERIFY_CODE_SUCCESS: {
			// console.log('VERIFY_CODE_SUCCESS', payload);
			return { ...state, meta: { ...state.meta, loading: false } };
		}
		case VERIFY_CODE_ERROR:
			return { ...state, meta: { ...state.meta, loading: false, error: payload.error } };
		case REGISTER_BENEFICIARY: {
			return {
				...state,
				meta: { ...state.meta, loading: true, error: '' },
				registration: payload,
			};
		}
		case REGISTER_BENEFICIARY_SUCCESS: {
			Mixpanel.trackWithProperties('register', { ...state.registration });
			return {
				...state,
				meta: { ...state.meta, loading: false },
				currentUser: state.registration,
				registration: {},
			};
		}
		case REGISTER_BENEFICIARY_ERROR: {
			return {
				...state,
				meta: { ...state.meta, loading: false, error: payload.error },
				currentUser: {},
				registration: {},
			};
		}
		case REQUEST_LOGIN: {
			// Mixpanel.trackWithProperties('login', { ...payload });
			return {
				...state,
				meta: { ...state.meta, loading: true, error: '' },
			};
		}
		case REQUEST_LOGIN_SUCCESS: {
			return {
				...state,
				meta: { ...state.meta, loading: false },
				currentUser: payload,
			};
		}
		case REQUEST_LOGIN_ERROR: {
			return {
				...state,
				meta: { ...state.meta, loading: false, error: payload.error },
			};
		}
		case UPDATE_IS_VERIFIED:
			return { ...state, isVerified: payload };
		case LOGOUT_SUCCESS:
			return { ...state, currentUser: {} };
		case RESET_ALL_USERS: {
			// console.log('RESET_ALL_USERS');
			return { ...state, currentUser: {} };
		}
		default:
			return state;
	}
};
