/* eslint-disable react/destructuring-assignment */
import type { ReactNode } from 'react';
import type React from 'react';
import { createContext, useEffect, useState } from 'react';
import { useSelector } from 'react-redux';

import type { FinancialsModel } from '../../../Pages/Financials/Components/FinancialsForm/FinancialsForm.helper';
import type { RootState } from '../../../ReduxState/store';
import cashFlowAnalysisPerspectiveCalculationsHelper from './cashFlowPerspective-context-helper';

export type CashFlowPerspectiveContextModel = {
	assetsTurnoverSecondFinancial: string;
	assetsTurnoverThirdFinancial: string;
	assetsTurnoverChange: string;
	otherCapitalSecondFinancial: string;
	otherCapitalThirdFinancial: string;
	otherCapitalChange: string;
	returnOnCapitalFirst: string;
	returnOnCapitalSecond: string;
	returnOnCapitalChange: string;
	returnOnTotalAssetsFirst: string;
	returnOnTotalAssetsSecond: string;
	returnOnTotalAssetsChange: string;
	returnOnEquityFirst: string;
	returnOnEquitySecond: string;
	returnOnEquityChange: string;
	netOperatingAssetsFirst: string;
	netOperatingAssetsSecond: string;
	netOperatingAssetsChange: string;
	netProfitPreTaxFirst: string;
	netProfitPreTaxSecond: string;
	netProfitPreTaxChange: string;
	secondFinancialsMonth: string;
	thirdFinancialsMonth: string;
	secondFinancialsYear: string;
	thirdFinancialsYear: string;
};

export const CashFlowPerspectiveContext: React.Context<CashFlowPerspectiveContextModel> =
	createContext<CashFlowPerspectiveContextModel>({
		assetsTurnoverSecondFinancial: '',
		assetsTurnoverThirdFinancial: '',
		assetsTurnoverChange: '',
		otherCapitalSecondFinancial: '',
		otherCapitalThirdFinancial: '',
		otherCapitalChange: '',
		returnOnCapitalFirst: '',
		returnOnCapitalSecond: '',
		returnOnCapitalChange: '',
		returnOnTotalAssetsFirst: '',
		returnOnTotalAssetsSecond: '',
		returnOnTotalAssetsChange: '',
		returnOnEquityFirst: '',
		returnOnEquitySecond: '',
		returnOnEquityChange: '',
		netOperatingAssetsFirst: '',
		netOperatingAssetsSecond: '',
		netOperatingAssetsChange: '',
		netProfitPreTaxFirst: '',
		netProfitPreTaxSecond: '',
		netProfitPreTaxChange: '',
		secondFinancialsMonth: '',
		thirdFinancialsMonth: '',
		secondFinancialsYear: '',
		thirdFinancialsYear: ''
	});

const CashFlowPerspectiveContextProvider: React.FC<{ children: ReactNode }> = (props: {
	children: ReactNode;
}) => {
	/** NEW */
	const selectedFinancials = useSelector(
		(state: RootState): FinancialsModel[] | null => state.financials.selectedFinancials
	);
	/** NEW end */
	const [assetsTurnoverSecondFinancial, setAssetsTurnoverSecondFinancial] = useState<string>('');
	const [assetsTurnoverThirdFinancial, setAssetsTurnoverThirdFinancial] = useState<string>('');
	const [assetsTurnoverChange, setAssetsTurnoverChange] = useState<string>('');
	const [otherCapitalSecondFinancial, setOtherCapitalSecondFinancial] = useState<string>('');
	const [otherCapitalThirdFinancial, setOtherCapitalThirdFinancial] = useState<string>('');
	const [otherCapitalChange, setOtherCapitalChange] = useState<string>('');
	const [returnOnCapitalFirst, setReturnOnCapitalFirst] = useState<string>('');
	const [returnOnCapitalSecond, setReturnOnCapitalSecond] = useState<string>('');
	const [returnOnCapitalChange, setReturnOnCapitalChange] = useState<string>('');
	const [returnOnTotalAssetsFirst, setReturnOnTotalAssetsFirst] = useState<string>('');
	const [returnOnTotalAssetsSecond, setReturnOnTotalAssetsSecond] = useState<string>('');
	const [returnOnTotalAssetsChange, setReturnOnTotalAssetsChange] = useState<string>('');
	const [returnOnEquityFirst, setReturnOnEquityFirst] = useState<string>('');
	const [returnOnEquitySecond, setReturnOnEquitySecond] = useState<string>('');
	const [returnOnEquityChange, setReturnOnEquityChange] = useState<string>('');
	const [netOperatingAssetsFirst, setNetOperatingAssetsFirst] = useState<string>('');
	const [netOperatingAssetsSecond, setNetOperatingAssetsSecond] = useState<string>('');
	const [netOperatingAssetsChange, setNetOperatingAssetsChange] = useState<string>('');
	const [netProfitPreTaxFirst, setNetProfitPreTaxFirst] = useState<string>('');
	const [netProfitPreTaxSecond, setNetProfitPreTaxSecond] = useState<string>('');
	const [netProfitPreTaxChange, setNetProfitPreTaxChange] = useState<string>('');
	const [secondFinancialsMonth, setSecondFinancialsMonth] = useState<string>('');
	const [thirdFinancialsMonth, setThirdFinancialsMonth] = useState<string>('');
	const [secondFinancialsYear, setSecondFinancialsYear] = useState<string>('');
	const [thirdFinancialsYear, setThirdFinancialsYear] = useState<string>('');
	useEffect(() => {
		window.scrollTo(0, 0);
		// const firstFinancial: FinancialsModel = JSON.parse(
		// 	localStorage.getItem('first-financial') as string
		// );
		// const secondFinancial: FinancialsModel = JSON.parse(
		// 	localStorage.getItem('second-financial') as string
		// );
		// const thirdFinancial: FinancialsModel = JSON.parse(
		// 	localStorage.getItem('third-financial') as string
		// );
		if (selectedFinancials && selectedFinancials.length >= 3) {
			const secondFinancial: FinancialsModel = selectedFinancials[1];
			const thirdFinancial: FinancialsModel = selectedFinancials[2];
			const {
				calculatedAssetsTurnoverFirst,
				calculatedAssetsTurnoverChange,
				calculatedOtherCapitalSecondFinancial,
				calculatedAssetsTurnoverSecond,
				calculatedOtherCapitalThirdFinancial,
				calculateOtherCapitalChange,
				calculatedReturnOnCapitalFirst,
				calculatedReturnOnCapitalChange,
				calculatedReturnOnCapitalSecond,
				calculatedReturnOnTotalAssetsFirst,
				calculatedReturnOnTotalAssetsSecond,
				calculatedReturnOnTotalAssetsChange,
				calculatedReturnOnEquityFirst,
				calculatedReturnOnEquitySecond,
				calculatedReturnOnEquityChange,
				calculatedNetOperatingAssetsChange,
				calculatedNetOperatingAssetsSecond,
				calculatedNetOperatingAssetsFirst,
				calculatedNetProfitPreTaxFirst,
				calculatedNetProfitPreTaxChange,
				calculatedNetProfitPreTaxSecond
			} = cashFlowAnalysisPerspectiveCalculationsHelper(secondFinancial, thirdFinancial);

			setAssetsTurnoverSecondFinancial(calculatedAssetsTurnoverFirst.toFixed(2).toString());
			setAssetsTurnoverThirdFinancial(calculatedAssetsTurnoverSecond.toFixed(2).toString());
			setAssetsTurnoverChange(calculatedAssetsTurnoverChange.toFixed(2).toString());
			setOtherCapitalSecondFinancial(calculatedOtherCapitalSecondFinancial.toFixed(0).toString());
			setOtherCapitalThirdFinancial(calculatedOtherCapitalThirdFinancial.toFixed(0).toString());
			setOtherCapitalChange(calculateOtherCapitalChange.toFixed(0).toString());
			setReturnOnCapitalFirst(calculatedReturnOnCapitalFirst.toFixed(2).toString());
			setReturnOnCapitalSecond(calculatedReturnOnCapitalSecond.toFixed(2).toString());
			setReturnOnCapitalChange(calculatedReturnOnCapitalChange.toFixed(2).toString());
			setReturnOnTotalAssetsFirst(calculatedReturnOnTotalAssetsFirst.toFixed(2).toString());
			setReturnOnTotalAssetsSecond(calculatedReturnOnTotalAssetsSecond.toFixed(2).toString());
			setReturnOnTotalAssetsChange(calculatedReturnOnTotalAssetsChange.toFixed(2).toString());
			setReturnOnEquityFirst(calculatedReturnOnEquityFirst.toFixed(2).toString());
			setReturnOnEquitySecond(calculatedReturnOnEquitySecond.toFixed(2).toString());
			setReturnOnEquityChange(calculatedReturnOnEquityChange.toFixed(2).toString());
			setNetOperatingAssetsFirst(calculatedNetOperatingAssetsFirst.toFixed(0).toString());
			setNetOperatingAssetsSecond(calculatedNetOperatingAssetsSecond.toFixed(0).toString());
			setNetOperatingAssetsChange(calculatedNetOperatingAssetsChange.toFixed(2).toString());
			setNetProfitPreTaxFirst(calculatedNetProfitPreTaxFirst.toFixed(0).toString());
			setNetProfitPreTaxSecond(calculatedNetProfitPreTaxSecond.toFixed(0).toString());
			setNetProfitPreTaxChange(calculatedNetProfitPreTaxChange.toFixed(2).toString());
			setSecondFinancialsMonth(secondFinancial.input['Month Ending']);
			setThirdFinancialsMonth(thirdFinancial.input['Month Ending']);
			setSecondFinancialsYear(secondFinancial.input['Year Ending']);
			setThirdFinancialsYear(thirdFinancial.input['Year Ending']);
		}
	}, [selectedFinancials]);

	// eslint-disable-next-line react/jsx-no-constructed-context-values
	const contextValue = {
		assetsTurnoverSecondFinancial,
		assetsTurnoverThirdFinancial,
		assetsTurnoverChange,
		otherCapitalSecondFinancial,
		otherCapitalThirdFinancial,
		otherCapitalChange,
		returnOnCapitalFirst,
		returnOnCapitalSecond,
		returnOnCapitalChange,
		returnOnTotalAssetsFirst,
		returnOnTotalAssetsSecond,
		returnOnTotalAssetsChange,
		returnOnEquityFirst,
		returnOnEquitySecond,
		returnOnEquityChange,
		netOperatingAssetsFirst,
		netOperatingAssetsSecond,
		netOperatingAssetsChange,
		netProfitPreTaxFirst,
		netProfitPreTaxSecond,
		netProfitPreTaxChange,
		secondFinancialsMonth,
		thirdFinancialsMonth,
		secondFinancialsYear,
		thirdFinancialsYear
	};
	return (
		<CashFlowPerspectiveContext.Provider value={contextValue}>
			{props.children}
		</CashFlowPerspectiveContext.Provider>
	);
};

export default CashFlowPerspectiveContextProvider;
