var AppDispatcher = require('../dispatcher/appDispatcher');
var UserConstants = require('../constants/userConstants');
var superagent = require('superagent');
var globals = require('../globals');
var NavigationActions = require('./navigationActions');

var UserActions = {

    registerUser: function(user) {
        AppDispatcher.handleAction({
            actionType: UserConstants.REGISTER_USER,
        user: user
        });

        superagent
            .post(globals.ApiUrl + '/user')
            .send(user)
            .withCredentials()
            .end(function(response){
                if(response.ok) {
                    UserActions.registrationSuccess(response.body);
                } else {
                    UserActions.registrationFailure(response.body);
                }
            });
    },
    registrationSuccess: function(user) {
        AppDispatcher.handleAction({
            actionType: UserConstants.REGISTRATION_SUCCESS,
            user: user
        });
    },
    registrationFailure: function(error) {
        AppDispatcher.handleAction({
            actionType: UserConstants.REGISTRATION_FAILURE,
            error: error
        });
    },
    userLogout:function() {
        AppDispatcher.handleAction({
            actionType: UserConstants.USER_LOGOUT
        });

        superagent
            .post(globals.ApiUrl + '/user/logout')
            .withCredentials()
            .end(function(response){
                UserActions.logoutDone();
            });
    },
    logoutDone: function() {
        AppDispatcher.handleAction({
            actionType: UserConstants.USER_LOGOUT_DONE
        });
    },
    userLogin: function(loginDetails) {
        AppDispatcher.handleAction({
            actionType: UserConstants.USER_LOGIN
        });



        superagent.post(globals.ApiUrl + '/user/login')
            .withCredentials()
            .send(loginDetails)
            .end(function(response) {
                if(response.unauthorized){
                    UserActions.loginFailure(response.body);
                } else {
                    UserActions.loginSuccess(response.body);  
                }
            });
    },
    loginSuccess: function(user) {
        AppDispatcher.handleAction({
            actionType: UserConstants.LOGIN_SUCCESS,
            user: user
        });
    },
    loginFailure: function(error) {
        AppDispatcher.handleAction({
            actionType: UserConstants.LOGIN_FAILURE,
            error: error
        });
    },
    checkLogin: function() {
        AppDispatcher.handleAction({
            actionType: UserConstants.CHECK_LOGIN
        });

        superagent
            .get(globals.ApiUrl + '/user')
            .withCredentials()
            .end(function(response){
                if(response.unauthorized){
                    UserActions.checkLoginArrived(null, true);
                } else {
                    UserActions.checkLoginArrived(response.body);
                }
            });
    },
    checkLoginArrived: function(user, error) {
        AppDispatcher.handleAction({
            actionType: UserConstants.CHECK_LOGIN_ARRIVED,
            user: user,
            error: error
        });
    }
};

module.exports = UserActions;
