
import axios from "axios";
import I18n from 'react-native-i18n';
import { store } from "../../redux/store";
import { fetchCautionCreditDemandUserError, fetchCautionCreditDemandUserPending, fetchCautionCreditDemandUserReset, fetchCautionCreditDemandUserSuccess, fetchGetDemandsGroupError, fetchGetDemandsGroupPending, fetchGetDemandsGroupReset, fetchGetDemandsGroupSuccess, fetchGetUniqueDemandsGroupError, fetchGetUniqueDemandsGroupPending, fetchGetUniqueDemandsGroupReset, fetchGetUniqueDemandsGroupSuccess, fetchGetUserGroupDetailError, fetchGetUserGroupDetailPending, fetchGetUserGroupDetailReset, fetchGetUserGroupDetailSuccess, fetchEpargnerArgentUserPending, fetchEpargnerArgentUserSuccess, fetchEpargnerArgentUserError, fetchEpargnerArgentUserReset, fetchCasserEpargneUserPending, fetchCasserEpargneUserSuccess, fetchCasserEpargneUserError, fetchCasserEpargneUserReset, fetchGetNanoCreditUserAccountPending, fetchGetNanoCreditUserAccountSuccess, fetchGetNanoCreditUserAccountError, fetchGetNanoCreditUserAccountReset, fetchGetNanoCreditUserHistoryPending, fetchGetNanoCreditUserHistorySuccess, fetchGetNanoCreditUserHistoryError, fetchGetNanoCreditUserHistoryReset } from "../../redux/actions/NanoCreditAction";
import { cautionCreditDemandtUrl, getCreditDemand, groupUrl, refundCreditDemandUrl, getNanoCreditUserHistoryUrl, epargnerArgentUrl, casserEpargneUrl, getNanoCreditAccount, getNanoCreditAgentHistoryUrl } from "../IlinkConstants";

export const getNanoCreditDemandsAction = (id) => {

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

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

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

export const getNanoCreditDemandsReset = () => {
     return dispatch => {
          dispatch(fetchGetDemandsGroupReset());
     }
}

export const getNanoCreditUniqueDemandsAction = (id) => {

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

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

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

export const getNanoCreditUniqueDemandsReset = () => {
     return dispatch => {
          dispatch(fetchGetUniqueDemandsGroupReset());
     }
}


export const getUserGroupDetailAction = (code_user) => {

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

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

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

export const getUserGroupDetailReset = () => {
     return dispatch => {
          dispatch(fetchGetUserGroupDetailReset());
     }
}


export const refundCreditDemandUserAction = (data) => {

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

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

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

export const refundCreditDemandUserReset = () => {
     return dispatch => {
          dispatch(fetchCautionCreditDemandUserReset());
     }
}


export const epargnerArgentUserAction = (data) => {

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

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

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

export const epargnerArgentUserReset = () => {
     return dispatch => {
          dispatch(fetchEpargnerArgentUserReset());
     }
}


export const casserEpargneUserAction = (data) => {

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

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

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

export const casserEpargneUserReset = () => {
     return dispatch => {
          dispatch(fetchCasserEpargneUserReset());
     }
}


export const getNanoCreditAccountAction = (idUser) => {

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

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

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

export const getNanoCreditAccountReset = () => {
     return dispatch => {
          dispatch(fetchGetNanoCreditUserAccountReset());
     }
}


export const getNanoCreditUserHistoryAction = (idUser, isUser) => {

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

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

          axios({
               url: `${isUser ? getNanoCreditUserHistoryUrl : getNanoCreditAgentHistoryUrl}/${idUser}`,
               method: 'GET',
               headers: {
                    'Authorization': authKey,
                    'X-Localization': I18n.currentLocale()
               },
          })
               .then(response => {
                    console.log(response);
                    dispatch(fetchGetNanoCreditUserHistorySuccess(response));
               })
               .catch(error => {
                    if (error.response)
                         dispatch(fetchGetNanoCreditUserHistoryError(error.response));
                    else if (error.request)
                         dispatch(fetchGetNanoCreditUserHistoryError(error.request))
                    else
                         dispatch(fetchGetNanoCreditUserHistoryError(error.message))
               });
     }
}

export const getNanoCreditUserHistoryReset = () => {
     return dispatch => {
          dispatch(fetchGetNanoCreditUserHistoryReset());
     }
}