import axios from "axios"
import { SERVER_URI_NJ, CONSTANTS } from "../constants"
import {
	IGetP2POrders,
	IGetMerchantOrders,
	IGetMerchantAdvs,
	IGetEditableAdv,
	IUpdateAdv,
	IChangeAdvStatus,
	IGetBalances,
	ITransferAsset,
	IGetMerchantDetails,
} from "../types"

// универсальная функция получение spot-, p2p-, fut-, locked- и earn- балансов
export const getBalancesAction = (objParams: IGetBalances) => {
	const postfix = objParams.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/get-balances/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_BALANCES_ACTION",
					result: response.data,
					exchange: objParams.exchange,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/get-balances`,
					},
				})
				console.log(error)
			})
	}
}

// Перевод Крипты р2р <---> spot
export const transferAssetAction = (objParams: ITransferAsset) => {
	const postfix = objParams.shortName.toLowerCase()
	// toNameAccount === "P2P" or "SPOT"
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/transferAsset/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "TRANSFER_ASSET_ACTION",
					result: response.data,
					exchange: objParams.exchange,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/transferAsset`,
					},
				})
				console.log(error)
			})
	}
}

// получение данных с p2p-ордерами
export const getP2POrdersAction = (objParamOrders: IGetP2POrders) => {
	const postfix = objParamOrders.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		// console.log(objParamOrders)
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/p2p/`, objParamOrders)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: `GET_P2P_ORDERS_ACTION`,
					result: response.data,
					exchange: objParamOrders.exchangeName,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/p2p/`,
					},
				})
				console.log(error)
			})
	}
}

// универсальная функция получения данных с merchant-ордерами
export const getMerchantOrdersAction = (
	objParamOrders: IGetMerchantOrders
) => {
	const postfix = objParamOrders.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		// console.log(objParamOrders)
		axios
			.post(
				`${SERVER_URI_NJ}/orders-${postfix}/merchant-orders/`,
				objParamOrders
			)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_MERCHANT_ORDERS_ACTION",
					result: response.data,
					exchange: objParamOrders.exchangeName,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/merchant-orders/`,
					},
				})
				console.log(error)
			})
	}
}

// получение данных с merchant-объявлениями
export const getMerchantAdvsAction = (objParamAdvs: IGetMerchantAdvs) => {
	const postfix = objParamAdvs.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		// console.log(objParamAdvs)
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/merchant-advs/`, objParamAdvs)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_MERCHANT_ADVS_ACTION",
					result: response.data,
					exchange: objParamAdvs.exchangeName,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/merchant-advs/`,
					},
				})
				console.log(error)
			})
	}
}

// получение данных редактируемого объявления
export const getEditableAdvAction = (objParams: IGetEditableAdv) => {
	const postfix = objParams.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/adv-edit/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_EDITABLE_ADV_ACTION",
					result: response.data,
					exchange: objParams.exchangeName,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/adv-edit/`,
					},
				})
				console.log(error)
			})
	}
}

// обновление данных редактируемого объявления
export const updateAdvAction = (objParams: IUpdateAdv) => {
	const postfix = objParams.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		// console.log(objParams)
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/adv-update/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "UPDATE_ADV_ACTION",
					result: response.data,
					exchange: objParams.exchangeName,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/adv-update/`,
					},
				})
				console.log(error)
			})
	}
}

// изменение статуса объявления (вкл/выкл)
export const changeAdvStatusAction = (objAdv: IChangeAdvStatus) => {
	const postfix = objAdv.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/adv-status/`, objAdv)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "CHANGE_ADV_STATUS_ACTION",
					result: response.data,
					exchange: objAdv.exchangeName,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/adv-status/`,
					},
				})
				console.log(error)
			})
	}
}

// получение данных с избранного мерчанта и его объявлений
export const getMerchantDetailsAction = (
	objMerchant: IGetMerchantDetails
) => {
	const postfix = objMerchant.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, exchange?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.get(
				`${SERVER_URI_NJ}/orders-${postfix}/merchant-details/${objMerchant.merchantId}`
			)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_MERCHANT_DETAILS_ACTION",
					result: response.data,
					exchange: objMerchant.exchangeName,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/merchant-details/`,
					},
				})
				console.log(error)
			})
	}
}
