import {
    LOGOUT,
    LOGIN_SUCCESS,
    LOGIN_FAIL,
    REQUEST_FORGOT_PASSWORD,
    FORGOT_PASSWORD,
    RECIEVE_MODULES,
    PASSWORD_CHANGED,
    SET_COMPANY_ADMIN_FLAG
} from '../../constants/authConstants';

function auth(state = {}, action) {
    switch (action.type) {
        case LOGIN_SUCCESS:
            return Object.assign({}, state, {
                isLoggedIn: action.isLoggedIn,
                userInfo: action.userInfo
            });
        case LOGOUT:
            return Object.assign({}, state, {
                isLoggedIn: action.isLoggedIn,
                errorMessage: action.errorMessage,
                messageColor: 'success'
            });
        case LOGIN_FAIL:
            return Object.assign({}, state, {
                isLoggedIn: action.isLoggedIn,
                errorMessage: action.errorMessage,
                messageColor: 'danger'
            });
        case REQUEST_FORGOT_PASSWORD:
            return Object.assign({}, state, {
                errorMessage: action.errorMessage,
                messageColor: 'success'
            });
        case FORGOT_PASSWORD:
            return Object.assign({}, state, {
                errorMessage: action.errorMessage,
                messageColor: action.messageColor
            });
        case RECIEVE_MODULES:
            return Object.assign({}, state, {
                modules: action.modules
            });
        case PASSWORD_CHANGED:
            return Object.assign({}, state, {
                errorMessage: action.errorMessage,
                messageColor: action.messageColor,
                isPasswordChanged: action.isPasswordChanged
            });
        case SET_COMPANY_ADMIN_FLAG:
            return Object.assign({}, state, {
                isCompanyAdmin: action.isCompanyAdmin
            });
        default:
            return state;
    }
}

export default auth;
