import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { View, StyleSheet, AppState, ActivityIndicator } from 'react-native';
import NavigationViewContainer from './navigation/NavigationViewContainer';
import { Map, List } from 'immutable';
import NavigationModal from '../components/NavigationModal';
import * as snapshotUtil from '../utils/snapshot';
import { NavigationActions } from 'react-navigation';
import {
  initializeSessionState,
  activate,
  deactivate,
  resetSessionStateFromSnapshot,
} from '../modules/session/SessionState';
import { resetCurrentUser } from './user/UserState';
import store from '../redux/store';
import Orientation from 'react-native-orientation';

const styles = StyleSheet.create({
  centered: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

const mapStateToProps = state => ({
  isReady: state.getIn(['session', 'isReady']),
  currentUser: state.getIn(['user', 'currentUser']),
  currentPage: state.getIn(['navigatorState', 'index']),
  pages: state.getIn(['navigatorState', 'routes']),
});

const mapDispatchToProps = dispatch => ({
  initializeSessionState: () => dispatch(initializeSessionState()),
  resetCurrentUser: () => dispatch(resetCurrentUser()),
  activate: () => dispatch(activate()),
  deactivate: () => dispatch(deactivate()),
  resetSessionStateFromSnapshot: snapshot => dispatch(resetSessionStateFromSnapshot(snapshot)),
  navigate: route => dispatch(NavigationActions.navigate({ routeName: route })),
  resetRoute: () => dispatch(NavigationActions.reset({
    index: 0,
    actions: [
      NavigationActions.navigate({ routeName: 'Home' }),
    ],
  })),
});

@connect(mapStateToProps, mapDispatchToProps)
export default class AppViewContainer extends Component {

  static propTypes = {
    isReady: PropTypes.bool.isRequired,
    initializeSessionState: PropTypes.func.isRequired,
    activate: PropTypes.func.isRequired,
    deactivate: PropTypes.func.isRequired,
    resetSessionStateFromSnapshot: PropTypes.func.isRequired,
    navigate: PropTypes.func.isRequired,
    resetCurrentUser: PropTypes.func.isRequired,
    resetRoute: PropTypes.func.isRequired,
    currentUser: PropTypes.instanceOf(Map),
    currentPage: PropTypes.number,
    pages: PropTypes.instanceOf(List),
  };

  state = {
    currentState: AppState.currentState,
    previousState: null,
  };

  componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);

    /* Haetaan viimeisin tila */
    snapshotUtil.resetSnapshot()
    .then((snapshot) => {
      /* Jos viimeisin tila löytyi */
      if (snapshot) {
        this.props.resetSessionStateFromSnapshot(snapshot);
        this.props.activate();
      }
      /* Ei löytynyt. Aloitetaan alusta */
      else {
        this.props.initializeSessionState();
      }

      /* Tallennetaan uusin tila aina kun statea päivitetään */
      store.subscribe(() => {
        snapshotUtil.saveSnapshot(store.getState());
      });
    });
  }

  _handleAppStateChange = (appState) => {
    const previous = this.state.currentState;

    this.setState({ currentState: appState, previousState: previous });

    if (this.state.currentState === 'active') {
      this.props.activate();
    } else if (this.state.currentState === 'inactive' || this.state.currentState === 'background') {
      this.props.deactivate();
    }
  };

  shouldRenderNavigationModal = () => {
    const route = this.props.pages.get(this.props.currentPage).get('routeName');

    return route !== 'Settings' && route !== 'Home' && route !== 'Ending' && route !== 'Login';
  };

  renderNavigationModal = () => this.shouldRenderNavigationModal() ? (
    <NavigationModal
      resetRoute={this.props.resetRoute}
      resetCurrentUser={this.props.resetCurrentUser}
      phase={this.props.pages.get(this.props.currentPage - 1).get('key') === 'Mood' ? 'moods' : 'other'}
      shouldSave={this.props.currentPage !== 1}
      currentUser={this.props.currentUser}
      quit={() => this.props.navigate('Ending')}
    />
    ) : null;

  render() {
    if (!this.props.isReady) {
      return (
        <View style={styles.centered}>
          <ActivityIndicator />
        </View>
      );
    }

    return (
      <View style={{ flex: 1 }}>
        <NavigationViewContainer />
        {this.renderNavigationModal()}
      </View>
    );
  }
}

@@ -275,203 +275,49 @@
 ort 
-%7B Map, List %7D from 'immutable';%0Aimport NavigationModal from '../components/NavigationModal';%0Aimport * as snapshotUtil from '../utils/snapshot';%0Aimport %7B NavigationActions %7D from 'react-navigation
+* as snapshotUtil from '../utils/snapshot
 ';%0Ai
@@ -462,139 +462,34 @@
 ort 
-%7B resetCurrentUser %7D from './user/UserState';%0Aimport store from '../redux/store';%0Aimport Orientation from 'react-native-orientation
+store from '../redux/store
 ';%0A%0A
@@ -981,64 +981,8 @@
 )),%0A
-  resetCurrentUser: () =%3E dispatch(resetCurrentUser()),%0A
   ac
@@ -1161,244 +1161,8 @@
 )),%0A
-  navigate: route =%3E dispatch(NavigationActions.navigate(%7B routeName: route %7D)),%0A  resetRoute: () =%3E dispatch(NavigationActions.reset(%7B%0A    index: 0,%0A    actions: %5B%0A      NavigationActions.navigate(%7B routeName: 'Home' %7D),%0A    %5D,%0A  %7D)),%0A
 %7D);%0A
@@ -1535,259 +1535,8 @@
 ed,%0A
-    navigate: PropTypes.func.isRequired,%0A    resetCurrentUser: PropTypes.func.isRequired,%0A    resetRoute: PropTypes.func.isRequired,%0A    currentUser: PropTypes.instanceOf(Map),%0A    currentPage: PropTypes.number,%0A    pages: PropTypes.instanceOf(List),%0A
   %7D;
@@ -1855,16 +1855,25 @@
     if (
+false && 
 snapshot
@@ -2642,685 +2642,8 @@
 %7D;%0A%0A
-  shouldRenderNavigationModal = () =%3E %7B%0A    const route = this.props.pages.get(this.props.currentPage).get('routeName');%0A%0A    return route !== 'Settings' && route !== 'Home' && route !== 'Ending' && route !== 'Login';%0A  %7D;%0A%0A  renderNavigationModal = () =%3E this.shouldRenderNavigationModal() ? (%0A    %3CNavigationModal%0A      resetRoute=%7Bthis.props.resetRoute%7D%0A      resetCurrentUser=%7Bthis.props.resetCurrentUser%7D%0A      phase=%7Bthis.props.pages.get(this.props.currentPage - 1).get('key') === 'Mood' ? 'moods' : 'other'%7D%0A      shouldSave=%7Bthis.props.currentPage !== 1%7D%0A      currentUser=%7Bthis.props.currentUser%7D%0A      quit=%7B() =%3E this.props.navigate('Ending')%7D%0A    /%3E%0A    ) : null;%0A%0A
   re
@@ -2886,47 +2886,8 @@
  /%3E%0A
-        %7Bthis.renderNavigationModal()%7D%0A
     

Remove navigation modal and useless props from AppView