import axios from 'axios';
import { Cookies } from 'react-cookie';
import Router from 'next/router';
import moment from 'moment';
import fileDownload from 'js-file-download';

import * as actionTypes from '../constants/actionTypes';

if(process.browser)
{
	FormData.prototype.appendObject = function(obj, namespace) 
	{
		let keyName;
		for (var key in obj) 
		{
			if (obj.hasOwnProperty(key)) 
			{
				keyName = [namespace, '[', key, ']'].join('');
				this.append(keyName, obj[key]);
			}
		}
	};	
}

export const getImage = ({ id }) => 
{
	console.log("getImage\n\n");
	console.log("getImage", `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/image`);

	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/image`, {
			params: { id }, 
			//responseType: 'blob', 
		})
		.then((response) => 
		{
			resolve(response.data);
		})
		.catch((error) => 
		{
			console.error("getImage error");
			console.error(error);

			reject();
		});
	});
}

export const getFile = ({ id, filename }) => 
{
	console.log("getFile");
	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/download`, {
			params: { id }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);

			reject();
		});
	});
}

export const getPrintFile = ({ contract, num, type, date, filename }) => 
{
	console.log("getFile");
	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/print-form`, {
			params: { contract, num, type, date }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);

			reject();
		});
	});
}

export const getFineBeforeAccrualFile = ({ contract, num, filename }) => 
{
	console.log("getFile");
	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/fine-before-accrual`, {
			params: { contract, num, }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);

			reject();
		});
	});
}

export const getFineAfterAccrualFile = ({ contract, num, filename }) => 
{
	console.log("getFile");
	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/fine-after-accrual`, {
			params: { contract, num, }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);

			reject();
		});
	});
}

export const getReconciliationFile = ({ contract, date_from, date_to, filename }) => 
{
	console.log("getFile");
	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/reconciliation`, {
			params: { contract, date_from, date_to, }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);

			reject();
		});
	});
}

export const getBitrixFile = ({ url, filename }) => 
{
	console.log("getFile");
	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/bitrix`, {
			params: { url, }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);

			reject();
		});

		/*
		axios.get(`${ url }`, {
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, filename);
			resolve();
		})
		.catch((error) => 
		{
			console.log("error");
			console.error(error);
			reject();
		});
		*/
	});
}

export const getContractDebtInvoiceFile = ({ contract }) => 
{
	console.log("ACTION", "getContractDebtInvoiceFile", { contract });

	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/contract/debt`, {
			params: { contract }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, `evoleasing_${ contract }_invoice_debt_${ moment().format("YYYY_MM_DD") }.pdf`);
			resolve();
		})
		.catch((error) => 
		{
			console.error("getContractDebtInvoiceFile", "error");
			console.error(error);

			reject();
		});
	});
}

export const getContractPenaltyInvoiceFile = ({ contract }) => 
{
	console.log("ACTION", "getContractPenaltyInvoiceFile", { contract });

	return new Promise((resolve, reject) => 
	{
		axios.get(`${ process.env.NEXT_PUBLIC_SELF_API_HOST }/api/file/contract/penalty`, {
			params: { contract }, 
			responseType: 'blob', 
		})
		.then((response) => 
		{
			fileDownload(response.data, `evoleasing_${ contract }_invoice_penalty_${ moment().format("YYYY_MM_DD") }.pdf`);
			resolve();
		})
		.catch((error) => 
		{
			console.error("getContractPenaltyInvoiceFile", "error");
			console.error(error);

			reject();
		});
	});
}