import _ from 'lodash';
import React from 'react';
import {
	createSwitchNavigator,
	createStackNavigator,
	StackViewTransitionConfigs,
	createAppContainer,
} from 'react-navigation';
import { Platform } from 'react-native';
import { Header } from 'nixplay-ui-kit';
import * as containers from '~cont/*/index.js'; // eslint-disable-line import/no-unresolved, import/extensions
// import { store } from '~/store/store';

// const fetchUser = async () => {
// 	try {
// 		const { users } = await store.getState();
// 		const { currentUser } = users;
// 		// console.log('USER>isStandarduser: ', currentUser.regType);
// 		return (_.has(currentUser, 'regType') && _.isEqual(parseInt(currentUser.regType, 0), 1));
// 	} catch (e) {
// 		return false;
// 	}
// };

// const isStandarduser = fetchUser();
// console.log('isStandarduser: ', isStandarduser);

const headerSetting = {
	headerMode: 'screen',
	defaultNavigationOptions: {
		header: (props) => (<Header {...props} />),
		animationEnabled: false,
	},
};

const IOS_MODAL_ROUTES = [
	'TermsAndConditions',
	// 'SelectToPrint',
];

const dynamicModalTransition = (transitionProps, prevTransitionProps) => {
	const isModal = IOS_MODAL_ROUTES.some(screenName =>
		screenName === transitionProps.scene.route.routeName ||
			(prevTransitionProps && screenName === prevTransitionProps.scene.route.routeName));
	return (Platform.OS === 'android') ? {
		screenInterpolator: sceneProps => {
			const { layout, position, scene } = sceneProps;
			const { index } = scene;

			const translateX = position.interpolate({
				inputRange: [index - 1, index, index + 1],
				outputRange: [layout.initWidth, 0, 0],
			});

			const translateY = position.interpolate({
				inputRange: [index - 1, index, index + 1],
				outputRange: [layout.initWidth, 0, 0],
			});

			const opacity = position.interpolate({
				inputRange: [
					index - 1,
					index - 0.99,
					index,
					index + 0.99,
					index + 1,
				],
				outputRange: [0, 1, 1, 0.3, 0],
			});

			return isModal
				? { opacity, transform: [{ translateY }] }
				: { opacity, transform: [{ translateX }] };
		},
	} : StackViewTransitionConfigs.defaultTransitionConfig(
		transitionProps,
		prevTransitionProps,
		isModal,
	);
};

export const RootStack = createSwitchNavigator(
	{
		SplashScreen: containers.SplashScreen,
		NonAuthedFlow: createStackNavigator({
			OnboardingIntro: containers.OnboardingIntro,
			WebView: containers.WebView,
			SignUpStep1: containers.SignUpStep1,
			VerifyMobileNumber: containers.VerifyMobileNumber,
			SetPassword: containers.SetPassword,
			SetBasicInformation: containers.SetBasicInformation,
			SetLifeStatus: containers.SetLifeStatus,
			ProvinceSelection: containers.ProvinceSelection,
			RegionSelection: containers.RegionSelection,
			MunicipalitySelection: containers.MunicipalitySelection,
			BarangaySelection: containers.BarangaySelection,
			HouseholdSelection: containers.HouseholdSelection,
			ExtendDamageSelection: containers.ExtendDamageSelection,
			ApplyProgramSelection: containers.ApplyProgramSelection,
			GenderSelection: containers.GenderSelection,
			SuffixSelection: containers.SuffixSelection,
			EthnicSelection: containers.EthnicSelection,
			OccupationSelection: containers.OccupationSelection,
			SectorSelection: containers.SectorSelection,
			MonthIncomeSelection: containers.MonthIncomeSelection,
			ReliefSelection: containers.ReliefSelection,
			NatIDSelection: containers.NatIDSelection,
			HealthSelection: containers.HealthSelection,
			EducationSelection: containers.EducationSelection,
			ProgramSelection: containers.ProgramSelection,
			// REGISTER
			Registration: containers.Registration,
			RegisterBeneficiary: containers.RegisterBeneficiary,
		}, {
			...headerSetting,
			initialRouteName: 'OnboardingIntro',
		}),
		AuthedFlow: createStackNavigator({
			ReleaseNotes: containers.ReleaseNotes,
			BasicUserDashboard: containers.BasicUserDashboard,
			SocialWorkerDashboard: containers.SocialWorkerDashboard,
			BeneficiaryList: containers.BeneficiaryList,
			ScanQR: containers.ScanQR,
			AddBeneficiaryRecord: containers.AddBeneficiaryRecord,
			ProvinceSelection: containers.ProvinceSelection,
			RegionSelection: containers.RegionSelection,
			MunicipalitySelection: containers.MunicipalitySelection,
			BarangaySelection: containers.BarangaySelection,
			HouseholdSelection: containers.HouseholdSelection,
			ExtendDamageSelection: containers.ExtendDamageSelection,
			GenderSelection: containers.GenderSelection,
			SuffixSelection: containers.SuffixSelection,
			EthnicSelection: containers.EthnicSelection,
			OccupationSelection: containers.OccupationSelection,
			SectorSelection: containers.SectorSelection,
			MonthIncomeSelection: containers.MonthIncomeSelection,
			ReliefSelection: containers.ReliefSelection,
			NatIDSelection: containers.NatIDSelection,
			HealthSelection: containers.HealthSelection,
			EducationSelection: containers.EducationSelection,
			ProgramSelection: containers.ProgramSelection,
			ShowQRCode: containers.ShowQRCode,
			AddFamilyMember: containers.AddFamilyMember,
			FamilyPositionSelection: containers.FamilyPositionSelection,
			ShowFamilyMemberList: containers.ShowFamilyMemberList,
			EditBeneficiaryRecord: containers.EditBeneficiaryRecord,
			EditFamilyMember: containers.EditFamilyMember,
			PhotoList: containers.PhotoList,
			// REGISTER
			Registration: containers.Registration,
			RegisterBeneficiary: containers.RegisterBeneficiary,
			// DASHBOARD
			SWAssessorDashboard: containers.SWAssessorDashboard,
			SWValidatorDashboard: containers.SWValidatorDashboard,
			SWTeacherDashboard: containers.SWTeacherDashboard,
			ReliefMultiSelection: containers.ReliefMultiSelection,
			// SCAN
			SetScanTagAssistance: containers.SetScanTagAssistance,
			VerifyBenefitRefID: containers.VerifyBenefitRefID,
			DisbursementModeSelection: containers.DisbursementModeSelection,
		}, {
			...headerSetting,
			initialRouteName: 'BasicUserDashboard',
			transitionConfig: dynamicModalTransition,
		}),
	},
	{
		initialRouteName: 'SplashScreen',
	},
);


export const Root = createAppContainer(RootStack);
