'use strict';

var createStore = require('fluxible-app/utils/createStore');
var AuthStore = require('./AuthStore');
var navigateAction = require('flux-router-component').navigateAction;
var debug = require('debug')('App:ApplicationStore');

var routes = require('../configs/routes');

var ROUTE_LOGIN = 'pro_home';
var ROUTE_AFTER_LOGIN = 'pro_dashboard';

module.exports = createStore({
    storeName: 'ApplicationStore',
    handlers: {
        'CHANGE_ROUTE_SUCCESS': 'onChangeRouteSuccess',
        'RECEIVE_LOGIN_SUCCESS': 'onAuthChange',
        'RECEIVE_LOGOUT_SUCCESS': 'onAuthChange',
    },
    dehydrate: function () {
        return {
            currentPath: this.currentPath,
            currentParams: this.currentParams,
            currentRouteName: this.currentRouteName,
        }
    },
    rehydrate: function (state) {
        this.currentPath = state.currentPath;
        this.currentParams = state.currentParams;
        this.currentRouteName = state.currentRouteName;
    },
    onChangeRouteSuccess: function (payload) {
        if (payload.path != this.currentPath) {
            debug('routing from '+this.currentPath+' to '+payload.path);
            this.currentPath = payload.path;
            this.currentParams = payload.params;
            this.currentRouteName = payload.name;
            this.applyAuthRules(true);
        } else {
            debug('no change in path, ignoring route change');
        }
    },
    onAuthChange: function (payload) {
        this.dispatcher.waitFor([AuthStore], this.applyAuthRules.bind(this, false));
    },
    applyAuthRules: function (alwaysEmitChange) {
        var currentRoute = this.getCurrentRoute(),
            oldRouteName = this.getCurrentRouteName(),
            isAuthenticated = !!this.dispatcher.getStore(AuthStore).getUser();

        if (isAuthenticated && currentRoute && currentRoute.config.leaveAfterAuth) {
            debug('user is authenticated, redirecting user to after login page');
            this.redirectToRoute(ROUTE_AFTER_LOGIN);
        }

        if (!isAuthenticated && currentRoute && currentRoute.config.authRequired) {
            debug('user is not authenticated, redirecting user to login page');
            this.redirectToRoute(ROUTE_LOGIN);
        }

        if (alwaysEmitChange || oldRouteName != this.getCurrentRouteName()) {
            this.emitChange();
        }
    },
    redirectToRoute: function (routeName) {
        this.currentRouteName = routeName;
        this.currentPath = routes[routeName].path;
        this.currentParams = {};
    },
    getCurrentRoute: function () {
        if (!this.currentRouteName) return;

        return {
            name: this.currentRouteName,
            path: this.currentPath,
            params: this.currentParams,
            config: routes[this.currentRouteName],
            navigate: {
                path: this.currentPath,
            }
        }
    },
    getCurrentRouteName: function () {
        return this.currentRouteName;
    }
});

@@ -77,42 +77,165 @@
 var 
-AuthStore = require('./AuthStore')
+makeHandlers = require('../lib/fluxible/makeHandlers');%0Avar AuthStore = require('./AuthStore');%0Avar AuthEvents = require('../constants/AuthConstants').Events
 ;%0Ava
@@ -557,170 +557,197 @@
 rs: 
-%7B%0A        'CHANGE_ROUTE_SUCCESS': 'onChangeRouteSuccess',%0A        'RECEIVE_LOGIN_SUCCESS': 'onAuthChange',%0A        'RECEIVE_LOGOUT_SUCCESS': 'onAuthChange',
+makeHandlers(%7B%0A        'onChangeRouteSuccess': 'CHANGE_ROUTE_SUCCESS',%0A        'onAuthChange': %5B%0A            AuthEvents.LOGIN_SUCCESS,%0A            AuthEvents.LOGOUT_SUCCESS%0A        %5D
 %0A    %7D
+)
 ,%0A  

Use constants in ApplicationStore