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 {updateMessages} from '../notification/notificationActions';
import {setDialogOpenFlag} from '../dialog/dialogActions';
import {recieveProfileInfo} from './profileSettingsActions';
import { fromWiaasProfileInfo } from '../../helpers/ProfileHelper';
import { toWiaasAddress } from '../../helpers/AddressHelper';

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}/wp-json/wiaas/customer/${idUser}/profile-addresses`,
                method: 'put',
                data: {
                    'profile_address': JSON.stringify(toWiaasAddress(profileAddress))
                }
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data)));
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                    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}/wp-json/wiaas/customer/${idUser}/profile-addresses/${idProfileAddress}`,
                method: 'delete'
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data)));
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
}

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

export const saveBillingAddress = (idUser, billingAddress) => {
    return dispatch => {
        dispatch(requestSaveBillingAddress());
        return client.fetch({
                url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/billing-addresses`,
                method: 'put',
                data: {
                    'billing_address': JSON.stringify(toWiaasAddress(billingAddress))
                }
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data)));
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                    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}/wp-json/wiaas/customer/${idUser}/billing-addresses/${idBillingAddress}`,
                method: 'delete'
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data.data)));
                    dispatch(updateMessages(response.data.messages, profileTexts.messages));
                    dispatch(setDialogOpenFlag(false));
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
}
