/**
 * React Native Full App (https://store.enappd.com/product/react-native-full-app)
 *
 * Copyright © 2019-present Enappd. All rights reserved.
 *
 * This source code is licensed as per the terms found in the
 * LICENSE.md file in the root directory of this source tree.
 */
import React from 'react';
import { Container, Content } from 'native-base';
import { AppLoading } from 'expo';
import * as Font from 'expo-font';
import Firebase, { withFirebase } from '../../../firebase';
import { NavigationStackScreenProps } from 'react-navigation-stack';

interface Iprops extends NavigationStackScreenProps {
    firebase: Firebase
}

class AuthCheck extends React.Component<Iprops> {

    async componentDidMount() {
        await Font.loadAsync({
            Roboto: require('../../../assets/Fonts/Roboto.ttf'),
            Roboto_medium: require('../../../assets/Fonts/Roboto_medium.ttf')
        });

        this.props.firebase.auth.onAuthStateChanged(authUser => {
            const { navigate } = this.props.navigation;
            const nextRoute = authUser ? 'App' : 'Auth';
            navigate(nextRoute);
        });
      }

    render() {
        return (
            <Container>
                <Content>
                    <AppLoading />
                </Content>
            </Container>
        )   
    }
}

export default withFirebase(AuthCheck);
