import {
    REQUEST_ORDER_INFO,
    RECEIVE_ORDER_INFO,
    RECEIVE_CUSTOMER_DOCUMENTS,
    RECEIVE_VALIDATION_COMMENTS,
    RECEIVE_CUSTOMER_ACCEPTANCE,
    RECEIVE_IS_COMPONENT_DISABLED,
    RECEIVE_IS_NEXT_STEP_WANTED,
    SET_EARLIEST_INSTALLATION_DATE,
    SET_CONFIRMATION_DATES,
    RECEIVE_INSTALLATION_COMPANIES,
    RECEIVE_ALL_SHIPPING_DATES_CONFIRMED,
    SET_SUPPORT_MESSAGE,
    SET_SCHEDULING_DISABLED_FLAG
} from '../../constants/ordersConstants';

const moduleReducers = {};

moduleReducers[REQUEST_ORDER_INFO] = (state, action) => {
    return Object.assign({}, state, {
        isLoading: action.isLoading
    });
};

moduleReducers[RECEIVE_ORDER_INFO] = (state, action) => {
    return Object.assign({}, state, {
        orderInfo: action.orderInfo,
        isLoading: action.isLoading
    });
};

moduleReducers[RECEIVE_CUSTOMER_DOCUMENTS] = (state, action) => {
    return Object.assign({}, state, {
        customerDocuments: action.customerDocuments
    });
};

moduleReducers[RECEIVE_VALIDATION_COMMENTS] = (state, action) => {
    const oldComments = Object.assign({}, state.validationComments);
    const newComments = Object.assign(oldComments, action.validationComments);

    return Object.assign({}, state, {
        validationComments: newComments
    });
};

moduleReducers[RECEIVE_CUSTOMER_ACCEPTANCE] = (state, action) => {
    const oldAcceptance = Object.assign({}, state.customerAcceptance);
    const newAcceptance = Object.assign(oldAcceptance, action.customerAcceptance);

    return Object.assign({}, state, {
        customerAcceptance: newAcceptance
    });
};

moduleReducers[RECEIVE_IS_COMPONENT_DISABLED] = (state, action) => {
    const newState = {isComponentDisabled : {}};
    newState.isComponentDisabled.installationScheduling = state.isComponentDisabled && state.isComponentDisabled.installationScheduling
                                                            ? state.isComponentDisabled.installationScheduling
                                                            : {};
    Object.assign(newState.isComponentDisabled.installationScheduling, action.isComponentDisabled.installationScheduling);

    return Object.assign({}, state, newState);
};

moduleReducers[RECEIVE_IS_NEXT_STEP_WANTED] = (state, action) => {
    const newState = {isNextStepWanted : {}};
    newState.isNextStepWanted.installationScheduling = state.isNextStepWanted && state.isNextStepWanted.installationScheduling
                                                            ? state.isNextStepWanted.installationScheduling
                                                            : {};
    Object.assign(newState.isNextStepWanted.installationScheduling, action.isNextStepWanted.installationScheduling);

    return Object.assign({}, state, newState);
};

moduleReducers[SET_EARLIEST_INSTALLATION_DATE] = (state, action) => {
    return Object.assign({}, state, {
        earliestInstallDate: action.earliestInstallDate
    });
};

moduleReducers[SET_CONFIRMATION_DATES] = (state, action) => {
    const oldDates = Object.assign({}, state.confirmationDates);
    const newDates = Object.assign(oldDates, action.confirmationDates);

    return Object.assign({}, state, {
        confirmationDates: newDates
    });
};

moduleReducers[RECEIVE_INSTALLATION_COMPANIES] = (state, action) => {
    return Object.assign({}, state, {
        installCompanies: action.installCompanies
    });
};

moduleReducers[SET_SUPPORT_MESSAGE] = (state, action) => {
    return Object.assign({}, state, {
        supportText: action.supportText
    });
}

moduleReducers[SET_SCHEDULING_DISABLED_FLAG] = (state, action) => {
    return Object.assign({}, state, {
        isSchedulingDisabled: action.isSchedulingDisabled
    });
}

moduleReducers[RECEIVE_ALL_SHIPPING_DATES_CONFIRMED] = (state, action) => {
    return Object.assign({}, state, {
        areAllShippingDatesConfirmed: action.areAllShippingDatesConfirmed
    });
}

const processReducer = (state = {}, action) => {
    return moduleReducers[action.type]
        ? moduleReducers[action.type](state, action)
        : state;
};

export default processReducer;
