var AppDispatcher = require('../dispatcher/appDispatcher');
var EventEmitter = require('events').EventEmitter;
var UserConstants = require('../constants/userConstants');
var _ = require('underscore');
var CartActions = require('../actions/cartActions');
var InitializationActions = require('../actions/initializationActions');
var _registrationState = {};
var _loginState = {};

var handleRegistrationSuccess = function(user) {
    _registrationState = {
        performed: true,
        success: true
    };

    handleLoginSuccess(user);
};

var handleRegistrationFailure = function(error) {
    _registrationState = {
        performed: true,
        success: false,
        error: error
    };

};

var handleLoginSuccess = function(user) {
    _loginState = {
        loggedIn: true,
        user: user
    };

    refreshCart();

};

var handleLoginFailure = function(error) {
    _loginState = {
        loggedIn: false,
        error: error
    };

};

var handleCheckLoginArrived = function(user, error) {
    if (user) {
        _loginState = {
            loggedIn: true,
            user: user
        };
    } else {
        _loginState = {
            loggedIn: false
        };
    }
};

var handleLogoutDone = function() {
    _registrationState = {};
    _loginState = {
        loggedIn: false
    }

    refreshCart();
};

var refreshCart = function() {

    setTimeout(function() {
        // needed for cart reset
        InitializationActions.initialize();
        setTimeout(function() {
            // reload the items
            CartActions.load();
        }, 0);
    }, 0);

}

// Extend SectionStore with EventEmitter to add eventing capabilities
var UserStore = _.extend({}, EventEmitter.prototype, {

    getRegistrationState: function() {
        //return _categoryDetails;
        return _registrationState;
    },
    getLoginState: function() {
        return _loginState;
    },
    // Emit Change event
    emitChange: function() {
        this.emit('change');
    },

    // Add change listener
    addChangeListener: function(callback) {
        this.on('change', callback);
    },

    // Remove change listener
    removeChangeListener: function(callback) {
        this.removeListener('change', callback);
    }
});


// Register callback with AppDispatcher
AppDispatcher.register(function(payload) {
    var action = payload.action;
    var text;

    switch (action.actionType) {


        case UserConstants.REGISTRATION_SUCCESS:
            handleRegistrationSuccess(action.user);
            break;
        case UserConstants.REGISTRATION_FAILURE:
            handleRegistrationFailure(action.error);
            break;
        case UserConstants.LOGIN_SUCCESS:
            handleLoginSuccess(action.user);
            break;
        case UserConstants.LOGIN_FAILURE:
            handleLoginFailure(action.error);
            break;
        case UserConstants.CHECK_LOGIN_ARRIVED:
            handleCheckLoginArrived(action.user, action.error);
            break;
        case UserConstants.USER_LOGOUT_DONE:
            handleLogoutDone();
            break;
        default:
            return true;
    }

    // If action was responded to, emit change event
    UserStore.emitChange();
    return true;

});

module.exports = UserStore;
