import {
    commissionAmount,
    getIlinkBankListUrl,
    getOperatorListUrl,
    getQRCodeDetail,
    linkBankAccountUrl,
    linkCardUrl,
    passwordValidationUrl,
    payBillUrl,
    searchUserHomeUrl,
    searchUserUrl,
    walletActionUrl,
    walletDetailUrl,
    walletUserSimpleActionUrl
} from "./IlinkConstants";
import {
    fetchGetBankIlinkError,
    fetchGetBankIlinkPending,
    fetchGetBankIlinkReset,
    fetchGetBankIlinkSuccess,
    fetchGetListOperatorError,
    fetchGetListOperatorPending,
    fetchGetListOperatorReset,
    fetchGetListOperatorSuccess,
    fetchGetQRCodeDetailError,
    fetchGetQRCodeDetailPending,
    fetchGetQRCodeDetailReset,
    fetchGetQRCodeDetailSuccess,
    fetchLinkCardError,
    fetchLinkCardPending,
    fetchLinkCardReset,
    fetchLinkCardSuccess,
    fetchPasswordValidationError,
    fetchPasswordValidationPending,
    fetchPasswordValidationReset,
    fetchPasswordValidationSuccess,
    fetchPayBillError,
    fetchPayBillPending,
    fetchPayBillReset,
    fetchPayBillSuccess,
    fetchReattachAccountError,
    fetchReattachAccountPending,
    fetchReattachAccountReset,
    fetchReattachAccountSuccess,
    fetchSearchUserError,
    fetchSearchUserPending,
    fetchSearchUserReset,
    fetchSearchUserSuccess,
    fetchWalleGetCommissionError,
    fetchWalletGetCommissionPending,
    fetchWalletGetCommissionSuccess,
    fetchWalletListDetailError,
    fetchWalletListDetailPending,
    fetchWalletListDetailReset,
    fetchWalletListDetailSuccess,
    fetchWalletListError,
    fetchWalletListPending,
    fetchWalletListReset,
    fetchWalletListSuccess,
    walletGetCommissionReset
} from "../redux/actions/WalletActions";
import {store} from "../redux/store";
import axios from "axios";
import I18n from 'react-native-i18n'

export const getWalletActivated = (userID) => {
    console.log("URL REQUEST", `${walletActionUrl}/${userID}/activated`);
    const auth = store.getState().authKeyReducer;
    const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';

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

        axios({
            url: `${walletActionUrl}/${userID}/activated`,
            method: 'GET',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchWalletListSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchWalletListError(error.response));
                else if (error.request)
                    dispatch(fetchWalletListError(error.request))
                else
                    dispatch(fetchWalletListError(error.message))
            });
    }
}
export const resetWalletListReducer = () => {
    return dispatch => {
        dispatch(fetchWalletListReset());
    }
}

export const saveTransfertAction = (data, pay = false) => {

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

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

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

export const getWalletDetailActivated = (id, isAgentCall) => {

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

    console.log("isAgentCall", isAgentCall);
    let url = null;
    if (isAgentCall === null)
        url = `${walletUserSimpleActionUrl}/${id}`;
    else if (isAgentCall === false)
        url = `${walletDetailUrl}/${id}`;
    else
        url = `${walletActionUrl}/${id}/activated`;


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

        axios({
            url,
            method: 'GET',
            headers: {
                'Authorization': authKey
            }
        })
            .then(response => {
                console.log("RESPONSE", response);
                dispatch(fetchWalletListDetailSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchWalletListDetailError(error.response));
                else if (error.request)
                    dispatch(fetchWalletListDetailError(error.request))
                else
                    dispatch(fetchWalletListDetailError(error.message))
            });
    }
}

export const resetWalletListDetailReducer = () => {
    return dispatch => {
        dispatch(fetchWalletListDetailReset());
    }
}

export const getWalletCommissionAmount = (data) => {

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

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

        axios({
            url: `${commissionAmount}`,
            method: 'POST',
            headers: {
                'Authorization': authKey
            },
            data
        })
            .then(response => {
                console.log(response);
                dispatch(fetchWalletGetCommissionSuccess(response));
            })
            .catch(error => {
                console.log(error);
                dispatch(fetchWalleGetCommissionError(error.message));
                if (error.response)
                    dispatch(fetchWalleGetCommissionError(error.response));
                else if (error.request)
                    dispatch(fetchWalleGetCommissionError(error.request))
                else
                    dispatch(fetchWalleGetCommissionError(error.message))
            });
    }
}

export const walletCommissionAmountReset = () => {
    return dispatch => {
        dispatch(walletGetCommissionReset());
    }
}

export const linkCardAction = (data, userID) => {

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

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

        axios({
            url: `${linkCardUrl}/${userID}`,
            data,
            method: 'POST',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchLinkCardSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchLinkCardError(error.response));
                else if (error.request)
                    dispatch(fetchLinkCardError(error.request))
                else
                    dispatch(fetchLinkCardError(error.message))
            });
    }
};
export const linkCardReset = () => {
    return dispatch => {
        dispatch(fetchLinkCardReset());
    }
};


export const getOperatorListAction = (typeOperator, idNetworkWallet) => {

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

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

        axios({
            url: `${getOperatorListUrl}/${typeOperator}/${idNetworkWallet}`,
            method: 'GET',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchGetListOperatorSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchGetListOperatorError(error.response));
                else if (error.request)
                    dispatch(fetchGetListOperatorError(error.request))
                else
                    dispatch(fetchGetListOperatorError(error.message))
            });
    }
}
export const getOperatorListReset = () => {
    return dispatch => {
        dispatch(fetchGetListOperatorReset());
    }
}

export const payBillAction = (data) => {

    const auth = store.getState().authKeyReducer;
    const authKey = auth !== null ? `${auth.authKey.token_type} ${auth.authKey.access_token}` : '';
    console.log("Data send", data);
    return dispatch => {
        dispatch(fetchPayBillPending());

        axios({
            url: payBillUrl,
            method: 'POST',
            data,
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchPayBillSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchPayBillError(error.response));
                else if (error.request)
                    dispatch(fetchPayBillError(error.request))
                else
                    dispatch(fetchPayBillError(error.message))
            });
    }
};
export const payBillReset = () => {
    return dispatch => {
        dispatch(fetchPayBillReset());
    }
};


export const getIlinkBankAction = (idWalletNetwork) => {

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

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

        axios({
            url: `${getIlinkBankListUrl}/${idWalletNetwork}`,
            method: 'GET',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchGetBankIlinkSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchGetBankIlinkError(error.response));
                else if (error.request)
                    dispatch(fetchGetBankIlinkError(error.request))
                else
                    dispatch(fetchGetBankIlinkError(error.message))
            });
    }
};
export const getIlinkBankReset = () => {
    return dispatch => {
        dispatch(fetchGetBankIlinkReset());
    }
};

export const reattachAccountAction = (data) => {

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

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

        axios({
            url: `${linkBankAccountUrl}`,
            data,
            method: 'POST',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchReattachAccountSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchReattachAccountError(error.response));
                else if (error.request)
                    dispatch(fetchReattachAccountError(error.request))
                else
                    dispatch(fetchReattachAccountError(error.message))
            });
    }
};
export const reattachAccountReset = () => {
    return dispatch => {
        dispatch(fetchReattachAccountReset());
    }
};

export const searchUserAction = (data, searchFromMap = false) => {

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

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

        axios({
            url: searchFromMap ? searchUserHomeUrl : `${searchUserUrl}?name=${data}`,
            method: searchFromMap ? 'POST' : 'GET',
            data,
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchSearchUserSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchSearchUserError(error.response));
                else if (error.request)
                    dispatch(fetchSearchUserError(error.request));
                else
                    dispatch(fetchSearchUserError(error.message));
            });
    }
}
export const searchUserReset = () => {
    return dispatch => {
        dispatch(fetchSearchUserReset());
    }
};

export const getQRCodeDetailAction = (id) => {

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

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

        axios({
            url: `${getQRCodeDetail}/${id}`,
            method: 'GET',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            }
        })
            .then(response => {
                console.log(response);
                dispatch(fetchGetQRCodeDetailSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchGetQRCodeDetailError(error.response));
                else if (error.request)
                    dispatch(fetchGetQRCodeDetailError(error.request));
                else
                    dispatch(fetchGetQRCodeDetailError(error.message));
            });
    }
}
export const getQRCodeDetailReset = () => {
    return dispatch => {
        dispatch(fetchGetQRCodeDetailReset());
    }
};

export const passwordValidationAction = (data) => {

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

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

        axios({
            url: `${passwordValidationUrl}`,
            method: 'POST',
            headers: {
                'Authorization': authKey,
                'X-Localization': I18n.currentLocale()
            },
            data
        })
            .then(response => {
                console.log(response);
                dispatch(fetchPasswordValidationSuccess(response));
            })
            .catch(error => {
                if (error.response)
                    dispatch(fetchPasswordValidationError(error.response));
                else if (error.request)
                    dispatch(fetchPasswordValidationError(error.request));
                else
                    dispatch(fetchPasswordValidationError(error.message));
            });
    }
}
export const passwordValidationReset = () => {
    return dispatch => {
        dispatch(fetchPasswordValidationReset());
    }
};