import type React from 'react';

import type { FinancialsContextModel } from '../../../Store/Financials/financials-context';
import type { IndustryModel } from '../../../Store/Financials/financialsContext.helper';
import { ebitdaMapValues } from '../../../Store/Financials/financialsContext.helper';
import type { CompanyDbModel } from '../../Companies/Components/CompaniesComponents.helper';
import type { FinancialsModel } from '../../Financials/Components/FinancialsForm/FinancialsForm.helper';
import { latestFinancialsHelper } from '../../MoneyForCeos/moneyMultiplierForCeos.helper';

export interface FifthChartDataInterface {
	name: string;
	amt: number;
	'Gross Margin': number;
	'Working Capital %': number;
}

export interface SixthChartDataInterface {
	name: string;
	amt: number;
	'Your Gap': number;
}
export const dataForSixthChart: SixthChartDataInterface[] = [
	{
		name: 'Your Gap',
		amt: 100,
		'Your Gap': 30
	},
	{
		name: 'Year Ending April 2022',
		amt: 100,
		'Your Gap': 100
	},
	{
		name: 'Year Ending June 2022',
		amt: 100,
		'Your Gap': 40
	}
];

export default function findRightIndustryIndexHandler(
	deferredSelectedCompany: CompanyDbModel,
	secondFinancial: FinancialsModel,
	setSelectedIndustry: React.Dispatch<React.SetStateAction<IndustryModel | undefined>>,
	firstFinancial: FinancialsModel,
	financialContext: FinancialsContextModel,
	setEbitdaInput: React.Dispatch<React.SetStateAction<number>>
): void {
	const foundedIndustry = ebitdaMapValues.find((industry) => {
		if (!deferredSelectedCompany) {
			return +industry.id === 7;
		}
		return +industry.id === +deferredSelectedCompany.industry_id;
	});

	const { ebit, deprecation } = latestFinancialsHelper(secondFinancial);

	setSelectedIndustry(foundedIndustry);
	if (!firstFinancial) {
		return;
	}

	const ebitdaAverage =
		((+ebit + +deprecation + (+firstFinancial.input.EBIT + +firstFinancial.input.Depreciation)) /
			2) *
		+financialContext.ebitdaValue;

	if (+ebitdaAverage > 0 && +ebitdaAverage < 2000000) {
		setEbitdaInput(foundedIndustry?.zeroToOne as number);
		financialContext.setEbitdaHandler(foundedIndustry?.zeroToOne as number);
	} else if (+ebitdaAverage >= 2000000 && +ebitdaAverage < 6000000) {
		setEbitdaInput(foundedIndustry?.twoToFive as number);
		financialContext.setEbitdaHandler(foundedIndustry?.twoToFive as number);
	} else if (+ebitdaAverage >= 6000000 && +ebitdaAverage < 11000000) {
		setEbitdaInput(foundedIndustry?.sixToTen as number);
		financialContext.setEbitdaHandler(foundedIndustry?.sixToTen as number);
	}
}
