import {
    API_SERVER
} from '../../config';
import HtmlClient from '../../helpers/HtmlClient';
import {
    REQUEST_SAVE_PROFILE_ADDRESS,
    REQUEST_REMOVE_PROFILE_ADDRESS,
    REQUEST_REMOVE_BILLING_ADDRESS,
    REQUEST_SAVE_BILLING_ADDRESS,
    profileTexts
} from '../../constants/profileSettingsConstants';
import {fetchProfileInfo} from './profileSettingsActions';
import {updateMessages} from '../notification/notificationActions';
import {setDialogOpenFlag} from '../dialog/dialogActions';

const client = new HtmlClient();

const requestSaveAddress = () => ({
    type: REQUEST_SAVE_PROFILE_ADDRESS
});

export const saveProfileAddress = (idUser, profileAddress) => {
    return dispatch => {
        dispatch(requestSaveAddress());
        return client.fetch({
                url: `${API_SERVER}/profileSettings/api/saveProfileAddress`,
                method: 'post',
                data: {profileAddress: JSON.stringify(profileAddress)}
            })
            .then(response => {
                if(response.data && response.data.messages){
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                    if(response.data.messages[0].code === 'success'){
                        dispatch(fetchProfileInfo(idUser));
                        dispatch(setDialogOpenFlag(false));
                    }
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
};

const requestRemoveAddress = () => ({
    type: REQUEST_REMOVE_PROFILE_ADDRESS
});

export const removeProfileAddress = (idUser, idProfileAddress) => {
    return dispatch => {
        dispatch(requestRemoveAddress());
        return client.fetch({
                url: `${API_SERVER}/profileSettings/api/removeProfileAddress`,
                method: 'post',
                data: {idProfileAddress}
            })
            .then(response => {
                if(response.data && response.data.messages){
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                    dispatch(fetchProfileInfo(idUser));
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
}

const requestSaveBillingAddress = () => ({
    type: REQUEST_SAVE_BILLING_ADDRESS
});

export const saveBillingAddress = (idUser, idCompany, billingAddress) => {
    return dispatch => {
        dispatch(requestSaveBillingAddress());
        return client.fetch({
                url: `${API_SERVER}/profileSettings/api/saveBillingAddress`,
                method: 'post',
                data: {idCompany, billingAddress: JSON.stringify(billingAddress)}
            })
            .then(response => {
                if(response.data && response.data.messages){
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                    if(response.data.messages[0].code === 'success'){
                        dispatch(fetchProfileInfo(idUser));
                        dispatch(setDialogOpenFlag(false));
                    }
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
};

const requestRemoveBillingAddress = () => ({
    type: REQUEST_REMOVE_BILLING_ADDRESS
});

export const removeBillingAddress = (idUser, idBillingAddress) => {
    return dispatch => {
        dispatch(requestRemoveBillingAddress());
        return client.fetch({
                url: `${API_SERVER}/profileSettings/api/removeBillingAddress`,
                method: 'post',
                data: {idBillingAddress}
            })
            .then(response => {
                if(response.data && response.data.messages){
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                    dispatch(fetchProfileInfo(idUser));
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
}
