import { createNativeStackNavigator } from '@react-navigation/native-stack';
import React from 'react';
import { getUniqueId } from 'react-native-device-info';
import { useSelector } from 'react-redux';
import { _map } from './RN-UI-LIB/src/utlis/common';
import Widget from './src/components/form';
import { setGlobalUserData } from './src/constants/Global';
import RealTemplate from './src/data/RealTemplateData.json';
import { useAppDispatch } from './src/hooks';
import { setDeviceId } from './src/reducer/userSlice';
import AllCasesMain from './src/screens/allCases';
import CaseDetails from './src/screens/caseDetails/CaseDetails';
import Login from './src/screens/login';
import OtpInput from './src/screens/login/OtpInput';
import TodoList from './src/screens/todoList/TodoList';
import { RootState } from './src/store/store';
import Profile from './src/screens/Profile';
import interactionsHandler from './src/screens/caseDetails/interactionsHandler';
import useFirestoreUpdates from './src/hooks/useFirestoreUpdates';
import crashlytics from '@react-native-firebase/crashlytics';
import { RouteProp } from '@react-navigation/native';
import { GenericType } from './src/common/GenericTypes';

const Stack = createNativeStackNavigator();

const ProtectedRouter = () => {
    const user = useSelector(
        (state: RootState) => state.user,
    );

    const {isLoggedIn, deviceId, sessionDetails} = user;

    // for setting user token in global.ts for api calling's
    setGlobalUserData(sessionDetails?.sessionToken, deviceId);
    interactionsHandler()
    const dispatch = useAppDispatch();

    // Firestore listener hook
    useFirestoreUpdates();

    if (!deviceId) {
        getUniqueId().then(id => dispatch(setDeviceId(id)));
    }

    const getScreenFocusListenerObj = ({route}: {route: RouteProp<GenericType>}) => ({
        focus: () => {
            crashlytics().log(JSON.stringify(route));
        }
      });

    return (
        <Stack.Navigator>
            {isLoggedIn ? (
                <>
                    <Stack.Screen
                        name="Home"
                        component={AllCasesMain}
                        options={{
                            header: () => null,
                        }}
                        listeners={getScreenFocusListenerObj}
                    />
                    <Stack.Screen
                        name="Profile"
                        component={Profile}
                        options={{
                            header: () => null,
                            animation: 'slide_from_right'
                        }}
                        listeners={getScreenFocusListenerObj}
                    />
                    <Stack.Screen
                        name="caseDetail"
                        component={CaseDetails}
                        options={{
                            header: () => null,
                        }}
                        listeners={getScreenFocusListenerObj}
                    />
                    {_map(RealTemplate.widget, key => (
                        <Stack.Screen
                            key={key}
                            name={key}
                            component={Widget}
                            options={{
                                header: () => null,
                                animation: 'slide_from_right'
                            }}
                            listeners={getScreenFocusListenerObj}
                        />
                    ))}
                    <Stack.Screen
                        name="TodoList"
                        component={TodoList}
                        options={{
                            header: () => null,
                            animation: 'slide_from_bottom'
                        }}
                        listeners={getScreenFocusListenerObj}
                    />
                </>
            ) : (
                <>
                    <Stack.Screen
                        name="Login"
                        component={Login}
                        options={{
                            header: () => null,
                        }}
                        listeners={getScreenFocusListenerObj}
                    />
                    <Stack.Screen
                        name="OTP"
                        component={OtpInput}
                        options={{
                            header: () => null,
                        }}
                        listeners={getScreenFocusListenerObj}
                    />
                </>
            )}
        </Stack.Navigator>
    );
};

export default ProtectedRouter;
