/**
 * 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, Font } from 'expo';
import { connect } from 'react-redux';
import { AppReduxState } from '../../../redux/reducers';
import { bindActionCreators } from 'redux';

import { checkUser } from '../../../redux/actions/Authentication';
import { NavigationStackScreenProps } from 'react-navigation-stack';

interface Iprops extends NavigationStackScreenProps {
    actions: any;
}

interface Istate {
}

class AuthCheck extends React.Component<Iprops,Istate> {

    async componentDidMount() {
        await Font.loadAsync({
            Roboto: require("native-base/Fonts/Roboto.ttf"),
            Roboto_medium: require("native-base/Fonts/Roboto_medium.ttf"),
        });
        const { actions, navigation } = this.props;
        const { userLoggedIn } = await actions.checkUser();
        if(userLoggedIn) {
            navigation.navigate('App');
        } else {
            navigation.navigate('Auth');
        }
    }

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

const mapStateToProps = (state:AppReduxState) => {
    return{
    };
  }
  const matchDispatchToProps = (dispatch:any) => ({
      actions: bindActionCreators({ checkUser },dispatch)
  })

export default connect(mapStateToProps, matchDispatchToProps)(AuthCheck);