import {
    API_SERVER
} from '../../config';
import HtmlClient from '../../helpers/HtmlClient';
import {
    REQUEST_PROFILE_INFO,
    RECIEVE_PROFILE_INFO,
    REQUEST_SAVE_PROFILE,
    REQUEST_SAVE_COMPANY,
    REQUEST_COUNTRIES,
    RECIEVE_COUNTRIES,
    profileTexts
} from '../../constants/profileSettingsConstants';
import {updateMessages} from '../notification/notificationActions';
import { fromWiaasProfileInfo} from '../../helpers/ProfileHelper';
import {fromWiaasCountryList} from '../../helpers/CountryHelper';

const client = new HtmlClient();

const requestProfileInfo = () => ({
    type: REQUEST_PROFILE_INFO,
    isLoading: true
});

export const recieveProfileInfo = (json) => ({
    type: RECIEVE_PROFILE_INFO,
    isLoading: false,
    profileInfo: json
});

export const fetchProfileInfo = (idUser) => {
    return dispatch => {
        dispatch(requestProfileInfo());
        return client.fetch({
                url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`,
                method: 'get'
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveProfileInfo(fromWiaasProfileInfo(response.data)));
                }
            });
    }
};

const requestSaveProfile = () => ({
    type: REQUEST_SAVE_PROFILE
});

export const saveProfileInfo = (idUser, profile) => {
    return dispatch => {
        dispatch(requestSaveProfile());
        const parsedFullName = profile.name.trim().split(' ');
        return client.fetch({
                url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/personal-info`,
                method: 'PUT',
                data: {
                    'first_name': parsedFullName[0],
                    'last_name': parsedFullName[1],
                    'phone': profile.phone
                }
            })
            .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 requestSaveCompany = () => ({
    type: REQUEST_SAVE_COMPANY
});

export const saveCompanyInfo = (idUser, companyInfo) => {
    return dispatch => {
        dispatch(requestSaveCompany());
        return client.fetch({
                url: `${API_SERVER}/wp-json/wiaas/customer/${idUser}/company-info`,
                method: 'put',
                data: {
                    'vat_code': companyInfo.vatCode,
                   'company_name': companyInfo.companyName
                }
            })
            .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 requestCountries = () => ({
    type: REQUEST_COUNTRIES,
    areCountriesLoading: true
});

const recieveCountries = (json) => ({
    type: RECIEVE_COUNTRIES,
    areCountriesLoading: false,
    countries: json
});

export const fetchCountries = () => {
    return dispatch => {
        dispatch(requestCountries());
        return client.fetch({
                url: `${API_SERVER}/wp-json/wiaas/user/get-countries`,
                method: 'get'
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveCountries(response.data.map(country => fromWiaasCountryList(country))));
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
};
