
import axios from "axios";
import I18n from 'react-native-i18n';
import { fetchActiveCountryListPending, fetchActiveCountryListSucsess, fetchActiveCountryListError, fetchActiveCountryListReset, fetchPayCountryNetworkPending, fetchPayCountryNetworkSucsess, fetchPayCountryNetworkError, fetchPayCountryNetworkReset, fetchCountryByDialCodePending, fetchCountryByDialCodeSucsess, fetchCountryByDialCodeError, fetchCountryByDialCodeReset } from "../redux/actions/CountryAction";
import { store } from "../redux/store";
import { activeCountryUrl, payCountryNetworkUrl, otherPayCountryNetworkUrl } from "./IlinkConstants";

export const getActiveCountryAction = () => {

     const auth = store.getState().authKeyReducer;
     const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

     return dispatch => {
          dispatch(fetchActiveCountryListPending());

          axios({
               url: `${activeCountryUrl}`,
               method: 'GET',
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchActiveCountryListSucsess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchActiveCountryListError(error.response));
                    else if (error.request)
                         dispatch(fetchActiveCountryListError(error.request))
                    else
                         dispatch(fetchActiveCountryListError(error.message))
               });
     }
}

export const getActiveCountryReset = () => {
     return dispatch => {
          dispatch(fetchActiveCountryListReset());
     }
}

export const getPayCountryNetworkAction = (data) => {

     const auth = store.getState().authKeyReducer;
     const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

     return dispatch => {
          dispatch(fetchPayCountryNetworkPending());

          axios({
               url: `${payCountryNetworkUrl}`,
               method: 'POST',
               data,
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchPayCountryNetworkSucsess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchPayCountryNetworkError(error.response));
                    else if (error.request)
                         dispatch(fetchPayCountryNetworkError(error.request))
                    else
                         dispatch(fetchPayCountryNetworkError(error.message))
               });
     }
}

export const getOtherPayCountryNetworkAction = (data) => {

     const auth = store.getState().authKeyReducer;
     const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

     return dispatch => {
          dispatch(fetchPayCountryNetworkPending());

          axios({
               url: `${otherPayCountryNetworkUrl}`,
               method: 'POST',
               data,
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchPayCountryNetworkSucsess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchPayCountryNetworkError(error.response));
                    else if (error.request)
                         dispatch(fetchPayCountryNetworkError(error.request))
                    else
                         dispatch(fetchPayCountryNetworkError(error.message))
               });
     }
}

export const getPayCountryNetworkReset = () => {
     return dispatch => {
          dispatch(fetchPayCountryNetworkReset());
     }
}


export const getActiveCountryByDialCodeAction = (countryDialCode) => {

     const auth = store.getState().authKeyReducer;
     const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

     return dispatch => {
          dispatch(fetchCountryByDialCodePending());

          axios({
               url: `${activeCountryUrlGET}/${countryDialCode}`,
               method: 'GET',
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchCountryByDialCodeSucsess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchCountryByDialCodeError(error.response));
                    else if (error.request)
                         dispatch(fetchCountryByDialCodeError(error.request))
                    else
                         dispatch(fetchCountryByDialCodeError(error.message))
               });
     }
}

export const getActiveCountryByDialCodeReset = () => {
     return dispatch => {
          dispatch(fetchCountryByDialCodeReset());
     }
}