
import { createIdentificationUrl, validateIdentificationUrl, getNumberInformationUrl, getUserIdentifiedInformationUrl } from "./IlinkConstants";
import { store } from "../redux/store";
import axios from "axios";
import I18n from 'react-native-i18n'
import { fetchCreateIdentificationReset, fetchCreateIdentificationSuccess, fetchCreateIdentificationError, fetchCreateIdentificationPending, fetchGetNumberInformationPending, fetchGetNumberInformationSuccess, fetchGetNumberInformationError, fetchGetNumberInformationReset, fetchUserIdentificationPending, fetchUserIdentificationSuccess, fetchUserIdentificationError, fetchUserIdentificationReset, fetchValidateIndentificationPending, fetchValidateIndentificationSuccess, fetchValidateIndentificationError, fetchValidateIndentificationReset } from "../redux/actions/IdentificationAction";


export const createIndentificationAction = (data) => {

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

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

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

export const updateIndentificationAction = (data) => {

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

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

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

export const createIndentificationResetAction = () => {
     return dispatch => {
          dispatch(fetchCreateIdentificationReset());
     }
}

export const getNumberDetailAction = (number) => {

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

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

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

export const getNumberResetAction = () => {
     return dispatch => {
          dispatch(fetchGetNumberInformationReset());
     }
}

export const getUserIdentificationAction = (userID) => {

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

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

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

export const getUserIdentificationResetAction = () => {
     return dispatch => {
          dispatch(fetchUserIdentificationReset());
     }
}

export const validateIdentificationAction = (formData, idIdentification) => {

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

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

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

export const validateIdentificationResetAction = () => {
     return dispatch => {
          dispatch(fetchValidateIndentificationReset());
     }
}