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';

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}/profileSettings/api/getProfileInfo`,
                method: 'post',
                data: {idUser}
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveProfileInfo(response.data));
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
};

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

export const saveProfileInfo = (idUser, profile) => {
    return dispatch => {
        dispatch(requestSaveProfile());
        return client.fetch({
                url: `${API_SERVER}/profileSettings/api/saveProfileInfo`,
                method: 'post',
                data: {idUser, profile: JSON.stringify(profile)}
            })
            .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 requestSaveCompany = () => ({
    type: REQUEST_SAVE_COMPANY
});

export const saveCompanyInfo = (idUser, companyInfo) => {
    return dispatch => {
        dispatch(requestSaveCompany());
        return client.fetch({
                url: `${API_SERVER}/profileSettings/api/saveCompanyInfo`,
                method: 'post',
                data: {companyInfo: JSON.stringify(companyInfo)}
            })
            .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 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}/profileSettings/api/getCoutnries`,
                method: 'get'
            })
            .then(response => {
                if(response.data){
                    dispatch(recieveCountries(response.data));
                }
            })
            .catch(error => {
                client.onError(error, dispatch);
            });
    }
};
