import axios from 'axios';
import { Cookies } from 'react-cookie';
import Router from 'next/router';
import moment from 'moment';
import { nSQL } from "@nano-sql/core";
import { eachSeries } from 'async';

import * as actionTypes from '../constants/actionTypes';
import * as currentState from '../reducers/initialState';

export const getEDOOperatorList = ({ dispatch, update = false }) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/edo/operators`;

	console.log("ACTION", "edo", "getEDOOperatorList()", { url });

	return new Promise((resolve, reject) => 
	{
		axios.post(url, {}, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			console.log("ACTION", "edo", "getEDOOperatorList()", "response", response.data);

			/*
			dispatch({ 
				type: actionTypes.EDO_OPERATORS_LIST, 
				data: { 
					list: [
						{
							"box_name": "ООО С К \"МОНОЛИТ-РУ\"", 
							"box_id": "7785f775578d4646aea1d323e703065d@diadoc.ru"
						}, {
							"box_name": "ООО СК \"МОНОЛИТ-РУ\" (роуминг, ООО «Такском» (Файлер))", 
							"box_id": "b8fbc5c395f54deabe6b8d84a10921f0@diadoc.ru"
						}
					]
				} 
			});
			*/

			dispatch({ 
				type: actionTypes.EDO_OPERATORS_LIST, 
				data: { 
					operators: response.data.box_edo, 
					message: response.data.message, 
				} 
			});

			resolve();
		})
		.catch((error) => 
		{
			console.error("ACTION", "edo", "getEDOOperatorList()", "ERROR");
			console.error(error);

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода GetEdoBox" } });
			window.dispatchEvent(eventMessage);

			dispatch({ 
				type: actionTypes.EDO_OPERATORS_LIST, 
				data: { 
					operators: [], 
					message: "Невозможно получить список операторов ЭДО", 
				} 
			});

			reject();
		});
	});
}

export const getEDOInvitesList = ({ dispatch, update = false }) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/edo/invites`;

	console.log("ACTION", "edo", "getEDOInvitesList()", { url });

	return new Promise((resolve, reject) => 
	{
		axios.post(url, {}, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			console.log("ACTION", "edo", "getEDOInvitesList()", "response", response.data);

			dispatch({ 
				type: actionTypes.EDO_INVITES_LIST, 
				data: { 
					invites: response.data.box_edo
				} 
			});

			resolve();
		})
		.catch((error) => 
		{
			console.error("ACTION", "edo", "getEDOInvitesList()", "ERROR");
			console.error(error);

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода GetInviteEdoBox" } });
			window.dispatchEvent(eventMessage);

			dispatch({ 
				type: actionTypes.EDO_INVITES_LIST, 
				data: { 
					invites: []
				} 
			});

			reject();
		});
	});
}

export const inviteToEDO = () => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/edo/invite/send`;

	console.log("ACTION", "edo", "inviteToEDO()", { url });

	return new Promise((resolve, reject) => 
	{
		axios.post(url, {}, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			console.log("ACTION", "edo", "inviteToEDO()", "response", response.data);

			resolve(response.data);
		})
		.catch((error) => 
		{
			console.error("ACTION", "edo", "inviteToEDO()", "ERROR");
			console.error(error);

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода InviteEdoBox" } });
			window.dispatchEvent(eventMessage);

			reject(error.data);
		});
	});
}

export const createEDOProject = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/edo/project`;

	console.log("ACTION", "edo", "createEDOProject()", { url });

	return new Promise((resolve, reject) => 
	{
		axios.post(url, payload, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			console.log("ACTION", "edo", "createEDOProject()", "response", response.data);

			resolve(response.data);
		})
		.catch((error) => 
		{
			console.error("ACTION", "edo", "createEDOProject()", "ERROR");
			console.error(error);

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода CreateEDOProject" } });
			window.dispatchEvent(eventMessage);

			reject(error.data);
		});
	});
}

export const docEDOSign = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/edo/sign`;

	if(parseInt(process.env.LOG, 10) === 1)
	{
		console.log("ACTION", "edo", "docEDOSign()", { url });
	}

	return new Promise((resolve, reject) => 
	{
		axios.post(url, payload, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			if(parseInt(process.env.LOG, 10) === 1)
			{
				console.log("ACTION", "edo", "docEDOSign()", "response", response.data);
			}

			resolve(response.data);
		})
		.catch((error) => 
		{
			if(parseInt(process.env.LOG, 10) === 1)
			{
				console.error("ACTION", "edo", "docEDOSign()", "ERROR");
				console.error(error);
			}

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода SignEDODocument" } });
			window.dispatchEvent(eventMessage);

			reject(error.data);
		});
	});
}

export const docEDOCancel = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/edo/cancel`;

	console.log("ACTION", "edo", "docEDOCancel()", { url });

	return new Promise((resolve, reject) => 
	{
		axios.post(url, payload, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			console.log("ACTION", "edo", "docEDOCancel()", "response", response.data);

			resolve(response.data);
		})
		.catch((error) => 
		{
			console.error("ACTION", "edo", "docEDOCancel()", "ERROR");
			console.error(error);

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода CancelDocument" } });
			window.dispatchEvent(eventMessage);

			reject(error.data);
		});
	});
}

export const docEDOConnect = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contract/sign/document/connect`;

	if(parseInt(process.env.LOG, 10) === 1)
	{
		console.log("ACTION", "edo", "docEDOConnect()", { url });
	}

	return new Promise((resolve, reject) => 
	{
		axios.post(url, payload, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			if(parseInt(process.env.LOG, 10) === 1)
			{
				console.log("ACTION", "edo", "docEDOConnect()", "response", response.data);
			}

			resolve(response.data);
		})
		.catch((error) => 
		{
			if(parseInt(process.env.LOG, 10) === 1)
			{
				console.error("ACTION", "edo", "docEDOConnect()", "ERROR");
				console.error(error);
			}

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода DocEdoConnect" } });
			window.dispatchEvent(eventMessage);

			reject(error.data);
		});
	});
}

export const docEDOStatus = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contract/sign/document/status`;

	if(parseInt(process.env.LOG, 10) === 1)
	{
		console.log("ACTION", "edo", "docEDOStatus()", { url });
	}

	return new Promise((resolve, reject) => 
	{
		axios.post(url, payload, {
			withCredentials: true, 
		})
		.then((response) => 
		{
			if(parseInt(process.env.LOG, 10) === 1)
			{
				console.log("ACTION", "edo", "docEDOStatus()", "response", response.data);
			}

			resolve(response.data);
		})
		.catch((error) => 
		{
			if(parseInt(process.env.LOG, 10) === 1)
			{
				console.error("ACTION", "edo", "docEDOStatus()", "ERROR");
				console.error(error);
			}

			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода GetEdoProjectID" } });
			window.dispatchEvent(eventMessage);

			reject(error.data);
		});
	});
}
