import axios from "axios"
import { SERVER_URI_NJ, CONSTANTS } from "../constants"
import { IUpdateUser } from "../types"

// создание нового пользователя
const createNewUserAction = (objNewUser: {
	inputEmail: string,
	inputPassword: string,
}) => {
	// console.log(objNewUser)
	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}/users/createUser`, objNewUser)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "CREATE_NEW_USER_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/users/createUser`,
					},
				})
				console.log(error)
			})
	}
}

// получение данных с сервера по логину юзера
const loginUserAction = (objUser: { login: string, pass: string }) => {
	// console.log(objUser)
	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}/users/enterUser`, objUser)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "LOGIN_USER_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/users/enterUsers`,
					},
				})
				console.log(error)
			})
	}
}

// обработчик обновления user (изменение пароля)
const updateUserAction = (objUser: IUpdateUser) => {
	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
			.put(`${SERVER_URI_NJ}/users/updateUser`, objUser)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "UPDATE_USER_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/users/updateUser/`,
					},
				})
				console.log(error)
			})
	}
}

// сброс пароля
const resetPasswordAction = (userLogin: any) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		console.log(userLogin)
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		axios
			.post(`${SERVER_URI_NJ}/users/reset-pass/`, { userLogin })
			.then(response => {
				console.log(response.data)
				// dispatch({
				// 	type: "RESET_PASSWORD_ACTION",
				// 	result: response.data,
				// })
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/users/reset-pass/`,
					},
				})
				console.log(error)
			})
	}
}

// выход из сессии-(й) (сброс токенов)
const logoutUserAction = (token: string) => {
	return (dispatch: {
		(arg0: { type: string }): void,
		(arg0: { type: string, result: any }): void,
		(arg0: { type: string, error: any }): void,
	}) => {
		dispatch({
			type: "LOAD_REQUESTED_DATA_ACTION",
		})
		const axiosConfig = {
			// withCredentials: false,
			headers: {
				// 'access-control-allow-credentials': true,
				// 'Access-Control-Allow-Origin': '*',
				// 'content-type':'application/json;charset=UTF-8',
				Authorization: `Bearer ${token}`,
			},
		}
		axios
			// .get(`${SERVER_URI_NJ}/users/logoutall`, axiosConfig)
			.get(`${SERVER_URI_NJ}/users/logout`, axiosConfig)
			.then(response => {
				// console.log(response.data)
				dispatch({
					type: "LOGOUT_USER_ACTION",
					result: response.data,
				})
			})
			.catch(error => {
				dispatch({
					type: "LOAD_FAILURE_DATA_ACTION",
					result: {
						error,
						message: `${CONSTANTS.ERROR_REQUEST}/users/logout`,
					},
				})
				console.log(error)
			})
	}
}

export {
	loginUserAction,
	logoutUserAction,
	createNewUserAction,
	updateUserAction,
	resetPasswordAction,
}
