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

import type { FinancialsModel } from '../../Pages/Financials/Components/FinancialsForm/FinancialsForm.helper';
import { formDefaultValues } from '../../Pages/Financials/Components/FinancialsForm/FinancialsForm.helper';
// import type { GlobalStateModel } from '../global/GlobalState-context';
// import { GlobalStateContext } from '../global/GlobalState-context';

export type MoneyGrowContextModel = {
	financial: FinancialsModel;
	getRevenueIncreaseValue: (event: number) => void;
	increasedRevenue: number;
	incrementRevenueHandler: () => void;
	decrementRevenueHandler: () => void;
	switchToPageHandler: () => void;
	increasedRevenueUpdated: number;
	updateIncreaseRevenueUpdatedHandler: (event: number) => void;
	updateFinancialHandler: (event: FinancialsModel) => void;
};

export const MoneyGrowthContext: React.Context<MoneyGrowContextModel> =
	createContext<MoneyGrowContextModel>({
		financial: {
			financials_id: '',
			company_id: '',
			selected: true,
			cognito_username: '',
			input: formDefaultValues
		},
		getRevenueIncreaseValue: (): void => {},
		increasedRevenue: 0,
		incrementRevenueHandler: (): void => {},
		decrementRevenueHandler: (): void => {},
		switchToPageHandler: (): void => {},
		increasedRevenueUpdated: 100,
		updateIncreaseRevenueUpdatedHandler: (): void => {},
		updateFinancialHandler: (): void => {}
	});

const MoneyGrowthContextProvider: React.FC<{ children: ReactNode }> = (props: {
	children: ReactNode;
}) => {
	const [financial, setFinancial] = useState<FinancialsModel>({
		financials_id: '',
		company_id: '',
		selected: true,
		cognito_username: '',
		input: formDefaultValues
	});

	// TODO Here should stay pre-choosed number according to the table for
	// TODO the increasedRevenue

	// ebitdaMapValues.find()

	// const globalContext: GlobalStateModel = useContext(GlobalStateContext);
	// console.log(globalContext.companies);

	// TODO  HERE should stay selected company if they are multiply
	// TODO and the single company if it's only one

	const [increasedRevenue, setIncreasedRevenue] = useState<number>(100);
	const [increasedRevenueUpdated, setIncreasedRevenueUpdated] = useState<number>(100);
	const switchToPage: boolean = JSON.parse(localStorage.getItem('switchToPage') as string);

	useEffect(() => {
		if (
			localStorage.getItem('third-financial') !== null &&
			localStorage.getItem('third-financial') !== undefined
		) {
			setFinancial(JSON.parse(localStorage.getItem('third-financial') as string));
		} else if (
			localStorage.getItem('second-financial') !== null &&
			localStorage.getItem('second-financial') !== undefined
		) {
			setFinancial(JSON.parse(localStorage.getItem('second-financial') as string));
		} else if (
			localStorage.getItem('first-financial') !== null &&
			localStorage.getItem('first-financial') !== undefined
		) {
			setFinancial(JSON.parse(localStorage.getItem('first-financial') as string));
		}
	}, [setIncreasedRevenue, setFinancial, switchToPage]);

	function switchToPageHandler(): void {
		console.log('Executed');
	}

	function getRevenueIncreaseValue(event: number): void {
		setIncreasedRevenue(event);
	}

	function updateIncreaseRevenueUpdatedHandler(event: number): void {
		setIncreasedRevenueUpdated(event);
	}

	function incrementRevenueHandler(): void {
		setIncreasedRevenue((prevState) => +(prevState + 0.1).toFixed(2));
	}

	function decrementRevenueHandler(): void {
		if (increasedRevenue >= 0.1) {
			setIncreasedRevenue((oldValue) => +(oldValue - 0.1).toFixed(2));
		}
	}
	function updateFinancialHandler(event: FinancialsModel): void {
		setFinancial(event);
	}

	// eslint-disable-next-line react/jsx-no-constructed-context-values
	const contextValue: MoneyGrowContextModel = {
		financial,
		getRevenueIncreaseValue,
		increasedRevenue,
		incrementRevenueHandler,
		decrementRevenueHandler,
		switchToPageHandler,
		increasedRevenueUpdated,
		updateIncreaseRevenueUpdatedHandler,
		updateFinancialHandler
	};

	return (
		<MoneyGrowthContext.Provider value={contextValue}>{props.children}</MoneyGrowthContext.Provider>
	);
};

export default MoneyGrowthContextProvider;
