/* eslint-disable react/button-has-type */
import axios from 'axios';
import { useEffect, useState } from 'react';

import environment from '../../../Environments/environment';
import styles from './MoneyForBusinessFooter.module.scss';
import ToolsBottomNavigation from '../../ToolsNavigation/ToolsBottomNavigation/ToolsBottomNavigation';
import ToolsNextTool from '../../ToolsNavigation/ToolsNextTool/ToolsNextTool';

const MoneyForBusinessFooter = (props: { htmlString: string; cssString: string }): JSX.Element => {
	const date = new Date();
	const today = date.toLocaleDateString();
	const time = date.toLocaleTimeString();
	const { moneyForGrowth, cashFlowSummary } = environment.frontendRoutes;
	const { pathname } = window.location;
	const [nextToolButton, setNextToolButton] = useState<string>('');
	const nextToolContent = `
		<p>
			<b>Next tool: Plan your growth strategy.</b>
			<br /><br />
			What’s next? 
			<i>
				Find out if your strategy is driving positive cash flow or creating hidden funding gaps.
			</i>
		</p>
	`;

	function changeNextToolButtonLink(): void {
		if (pathname === '/money-for-ceos-business') {
			setNextToolButton(moneyForGrowth);
		} else if (pathname === '/money-for-growth') {
			setNextToolButton(cashFlowSummary);
		}
	}

	const generateReport = async () => {
		const apiUrl = environment.pdfUrl;
		try {
			const htmlContent = `<style>${props.cssString}</style><body>${props.htmlString}</body>`;
			const response = await axios.post(
				apiUrl,
				{
					htmlContent
				},
				{
					responseType: 'blob' // Set the response type to blob to handle binary data
				}
			);

			// Create a new Blob object from the PDF stream
			const pdfBlob = new Blob([response.data], { type: 'application/pdf' });

			// Create a link element
			const link = document.createElement('a');
			link.href = URL.createObjectURL(pdfBlob); // Create an object URL for the blob
			link.download = `money-for-growth-report-${Date.now()}.pdf`; // Set the file name for the downloaded file

			// Append the link to the body (even though it's not visually rendered)
			document.body.appendChild(link);

			// Programmatically click the link to trigger the download
			link.click();

			// Clean up and remove the link element from the DOM
			document.body.removeChild(link);
		} catch (error) {
			console.error('Error downloading PDF:', error);
		}
	};

	useEffect(() => {
		changeNextToolButtonLink();
		// eslint-disable-next-line react-hooks/exhaustive-deps
	}, []);

	return (
		<div className={styles.container}>
			<ToolsBottomNavigation
				stepNumber={2}
				maxSteps={2}
				prevAction={{
					title: 'Back to Multiplier Simulator',
					link: environment.frontendRoutes.moneyForCeos
				}}
				reportGenerated={(e) => {
					e.preventDefault();
					generateReport();
				}}
			/>
			<ToolsNextTool
				content={nextToolContent}
				nextAction={{ title: 'Assess growth impact', link: nextToolButton }}
			/>
		</div>
	);
};

export default MoneyForBusinessFooter;
