/**
 * @flow
 */

import React, { Component } from 'react';
import {
  createReduxContainer,
} from 'react-navigation-redux-helpers';
import { BackHandler } from 'react-native';
import { connect } from 'react-redux';

import NavigationStack from './navigationStack';
import { StoreContext } from '../utils/constants';


const AppNavigation = createReduxContainer(NavigationStack);

const mapStateToProps = state => ({
  state: state.nav,
});

const ReduxNavigation = connect(mapStateToProps)(AppNavigation);


class Navigation extends Component {
  componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
  }

  componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
  }

  onBackPress = () => {
    console.log('onBackPress', this.props);
    //const { dispatch, navigationState } = this.props;
    //if (navigationState.stateForLoggedIn.index <= 1) {
    //  BackHandler.exitApp();
    //  return;
    //}
    //dispatch(NavigationActions.back());
    return true;
  }

  render() {
    return (
      <StoreContext.Provider value={{ theme: global.theme }}>
        <ReduxNavigation />
      </StoreContext.Provider>
    );
  }
}


export default Navigation;
