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 { concatSeries, eachSeries } from 'async';
import fileDownload from 'js-file-download';

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

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

	console.log("ACTION", "sign", "signCheckCreatePrintForm()", { url });

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

			resolve(response.data);
		})
		.catch((error) => 
		{
			console.error("ACTION", "sign", "signCheckCreatePrintForm()", "ERROR");
			console.error(error);
			
			const eventMessage = new CustomEvent("_message", { detail: { type: "error", title: "Ошибка", content: "Ошибка при вызове метода CheckCreatePrintForm" } });
			window.dispatchEvent(eventMessage);

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

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

	console.log("ACTION", "sign", "signGetGUIDEntity()", { url });

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

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

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

			resolve(response.data);
		});
	});
}

export const signCheckPowerAttorneyClient = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contract/sign/attorney/check`;

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

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

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

export const signGetPowerAttorneyClient = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contract/sign/attorney/get`;

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

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

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

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

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

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

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

export const signDownloadFile = ({ payload, filename }) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contract/sign/document/download`;

	return new Promise((resolve, reject) => 
	{
		axios.get(url, {
			params: payload, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.error("ACTION", "sign", "signDownloadFile()", "ERROR");
			console.error(error);

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

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

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

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

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

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

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

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

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

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

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

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

export const signUploadPaperDocument = (contract_number, deal_id, files) => 
{
	console.log({ contract_number, files });
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contract/sign/document/upload`;

	return new Promise((resolve, reject) => 
	{
		let data = new FormData();
		data.append('contract_number', contract_number);
		data.append('deal_id', deal_id);

		for(let i in files)
		{
			data.append('files', files[i]);
		}

		axios.post(url, data, {
			headers: {
				"Content-Type": "multipart/form-data", 
			}, 
			withCredentials: true, 
		})
		.then((response) => 
		{
			if(parseInt(process.env.LOG, 10) === 1) { console.log("ACTION", "sign", "signUploadPaperDocument()", "response", response.data); }

			setTimeout(() => 
			{
				resolve();
			}, 2000);
		})
		.catch((error) => 
		{
			console.error("ACTION", "sign", "signUploadPaperDocument()", "ERROR");
			console.error(error);

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

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

export const signCheckCancelDocument = (payload) => 
{
	const url = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/contract/sign/cancel/check`;

	console.log("ACTION", "sign", "signCheckCancelDocument()", { url });

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

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

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

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

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

	console.log("ACTION", "sign", "signCancelDocument()", { url });

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

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

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

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