
import { transactionUrl, transactionIlinkUrl, getHyperviseurHistoriqueUrl, getSuperviseurHistoriqueUrl } from "./IlinkConstants";
import { store } from "../redux/store";
import axios from "axios";
import I18n from 'react-native-i18n'
import { fetchWalletHistoryPending, fetchWalletHistorySuccess, fetchWalletHistoryError, fetchWalletHistoryReset, fetchWalletHistoryHyperSuperPending, fetchWalletHistoryHyperSuperSuccess, fetchWalletHistoryHyperSuperError, fetchWalletHistoryHyperSuperReset } from "../redux/actions/WalletActions";

export const getWalletTransactionHistory = (walletID, isiLinkWorldWallet) => {

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


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

          axios({
               url: isiLinkWorldWallet ? `${transactionIlinkUrl}/agent/${walletID}` : `${transactionUrl}/${walletID}`,
               method: 'GET',
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchWalletHistorySuccess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchWalletHistoryError(error.response));
                    else if (error.request)
                         dispatch(fetchWalletHistoryError(error.request))
                    else
                         dispatch(fetchWalletHistoryError(error.message))
               });
     }
}
export const getWalletTransactionHistoryUser = (walletID) => {

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


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

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

export const getWalletTransactionHistoryReset = () => {
     return dispatch => {
          dispatch(fetchWalletHistoryReset());
     }
}

export const getHyperSuperTransactionHistoryAction = (walletID, isHyper) => {

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


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

          axios({
               url: isHyper ? `${getHyperviseurHistoriqueUrl}/${walletID}` : `${getSuperviseurHistoriqueUrl}/${walletID}`,
               method: 'GET',
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               }
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchWalletHistoryHyperSuperSuccess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchWalletHistoryHyperSuperError(error.response));
                    else if (error.request)
                         dispatch(fetchWalletHistoryHyperSuperError(error.request))
                    else
                         dispatch(fetchWalletHistoryHyperSuperError(error.message))
               });
     }
}

export const getHyperSuperTransactionHistoryReset = () => {
     return dispatch => {
          dispatch(fetchWalletHistoryHyperSuperReset());
     }
}