
import axios from "axios";
import I18n from 'react-native-i18n';
import { store } from "../redux/store";
import { saveOnesignalIds, getNotificationUrl } from "./IlinkConstants";
import { fetchSaveOnesignalPlayerIdsPending, fetchSaveOnesignalPlayerIdsSuccess, fetchSaveOnesignalPlayerIdsError, fetchSaveOnesignalPlayerIdsReset } from "../redux/actions/NotificationAction";
import { fetchGetNotificationSuccess, fetchGetNotificationPending, fetchGetNotificationError } from "../redux/actions/NanoCreditAction";

export const saveOnesignalIdsAction = (isUser, data) => {

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

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

          axios({
               url: isUser ? `${saveOnesignalIds}/saveUser` : `${saveOnesignalIds}/saveAgent`,
               method: 'POST',
               data,
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchSaveOnesignalPlayerIdsSuccess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchSaveOnesignalPlayerIdsError(error.response));
                    else if (error.request)
                         dispatch(fetchSaveOnesignalPlayerIdsError(error.request))
                    else
                         dispatch(fetchSaveOnesignalPlayerIdsError(error.message))
               });
     }
}

export const saveOnesignalIdsReset = () => {
     return dispatch => {
          dispatch(fetchSaveOnesignalPlayerIdsReset());
     }
}


export const getNotificationAction = (data) => {

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

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

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

export const getNotificationReset = () => {
     return dispatch => {
          dispatch(fetchGetNotificationReset());
     }
}