import axios from "axios"
import {
	IGetReportOrders,
	IGetHistorySpotOrders,
	IGetActiveSpotOrders,
	IInsertOrderToDB,
	IInsertBalanceToDB,
	IInsertNewItemToDB,
	IGetEditableOrder,
	IUpdateItemDetails,
	ICheckOrderStatus,
	IGetGroupItems,
} from "../types"
import {  SERVER_URI_NJ, CONSTANTS, } from "../constants"

// получение данных из БД
export const getReportOrdersAction = (
	objParamReport: IGetReportOrders
) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/reports/data-by-date/`, objParamReport)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_DATA_REPORT_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/reports/data-by-date`,
					},
				})
				console.log(error)
			})
	}
}

// получение данных о неоплаченных ордерах из БД
export const checkOrderStatusesAction = (
	objCheckStatus: ICheckOrderStatus
) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/reports/check-status/`, objCheckStatus)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "CHECK_STATUS_ORDERS_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/reports/check-status/`,
					},
				})
				console.log(error)
			})
	}
}

// получение ответа о сохранении ордера в БД
export const insertOrderToDBAction = (objInsertDB: IInsertOrderToDB) => {
	const postfix = objInsertDB.shortName.toLowerCase()
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/orders-${postfix}/save-user-orders/`, objInsertDB)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "INSERT_ORDER_TO_DB_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/save-user-orders/`,
					},
				})
				console.log(error)
			})
	}
}

// добавление нового P2P-сервиса, payType, currency
export const insertNewItemToDBAction = (objItem: IInsertNewItemToDB) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/reports/save-item/`, objItem)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "ADD_NEW_ITEM_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/reports/save-item`,
					},
				})
				console.log(error)
			})
	}
}

// получение ответа о сохранении баланса в БД
export const insertBalanceToDBAction = (
	objBalanceDB: IInsertBalanceToDB
) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/reports/save-balances/`, objBalanceDB)
			.then(response => {
				// console.log(response.data)
				dispatch({
					// type: "UPLOAD_ITEMS_ACTION",
					// result: response.data,
					type: "UPDATE_ITEM_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/reports/save-balances`,
					},
				})
				console.log(error)
			})
	}
}

// получение данных редактируемого ордера из БД
export const getEditableOrderAction = (objOrder: IGetEditableOrder) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/reports/edit-order/`, objOrder)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_EDITABLE_ORDER_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/reports/edit-order/`,
					},
				})
				console.log(error)
			})
	}
}

// обновление Items => orderDetails || payTypeDetails || exchangeItem || currencyItem
export const updateItemDetailsAction = (objItem: IUpdateItemDetails) => {
	// ветвление роутов
	let itemRoute = ""
	if (objItem.itemFlag === "orderItem") {
		itemRoute = "update-order"
	} else if (objItem.itemFlag === "paymentItem") {
		itemRoute = "update-paytype"
	} else if (objItem.itemFlag === "exchangeItem") {
		itemRoute = "update-exchange"
	} else if (objItem.itemFlag === "currencyItem") {
		itemRoute = "update-currency"
	} else if (objItem.itemFlag === "statusItem") {
		itemRoute = "update-status"
	}

	// console.log(itemRoute, objItem)
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/reports/${itemRoute}/`, objItem)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "UPDATE_ITEM_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/reports/update-item/`,
					},
				})
				console.log(error)
			})
	}
}

// Групповое получение allAdvs, allBalances, allOtcOrders
export const getGroupItemsAction = (objParams: IGetGroupItems) => {
	// ветвление роутов
	let itemRoute = ""
	if (objParams.typeItems === "allOtcOrders") {
		itemRoute = "all-otc-orders"
	} else if (objParams.typeItems === "allActiveSpotOrders") {
		itemRoute = "all-active-spot-orders"
	} else if (objParams.typeItems === "allHistorySpotOrders") {
		itemRoute = "all-history-spot-orders"
	} else if (objParams.typeItems === "allBalances") {
		itemRoute = "all-balances"
	} else if (objParams.typeItems === "allAdvs") {
		itemRoute = "all-advs"
	}

	// console.log(itemRoute, objParams)
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any, typeItems?: string }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/reports/${itemRoute}/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "GET_GROUP_ITEMS_ACTION",
					result: response.data,
					typeItems: objParams.typeItems,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/reports/${itemRoute}`,
					},
				})
				console.log(error)
			})
	}
}

// получение завершенных spot-ордеров и сохранение в БД
export const getHistorySpotOrdersAction = (
	objParams: IGetHistorySpotOrders
) => {
	const postfix = objParams.shortName.toLowerCase()
	const exchange = objParams.exchange
	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}/spot-orders-history/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "SPOT_ORDERS_HISTORY_ACTION",
					result: response.data,
					exchange,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/spot-orders-history/`,
					},
				})
				console.log(error)
			})
	}
}

// получение активных spot-ордеров
export const getActiveSpotOrdersAction = (
	objParams: IGetActiveSpotOrders
) => {
	const postfix = objParams.shortName.toLowerCase()
	const exchange = objParams.exchange
	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}/spot-orders-active/`, objParams)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "SPOT_ORDERS_ACTIVE_ACTION",
					result: response.data,
					exchange,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/orders-${postfix}/spot-orders-active/`,
					},
				})
				console.log(error)
			})
	}
}
