import type { SelectChangeEvent } from '@mui/material/Select';
import type React from 'react';

import type { FinancialsContextModel } from '../../Store/Financials/financials-context';
import type { GlobalStateModel } from '../../Store/global/GlobalState-context';
import type { CompanyDbModel } from '../Companies/Components/CompaniesComponents.helper';
import type { Action } from './Components/FinancialsForm/FinancialsForm';
import type { FinancialsInterface } from './Components/FinancialsForm/FinancialsForm.helper';

export default function financialsFormHelperHandler(
	state: FinancialsInterface,
	action: Action,
	initialState: FinancialsInterface,
	financialForEdit: FinancialsInterface | undefined
): FinancialsInterface {
	switch (action.type) {
		case 'FIRST_MONTH':
			return {
				...state,
				'Month Start': action.value
			};

		case 'FIRST_YEAR':
			return {
				...state,
				'Year Start': action.value
			};

		case 'LAST_MONTH':
			return {
				...state,
				'Month Ending': action.value
			};

		case 'LAST_YEAR':
			return {
				...state,
				'Year Ending': action.value
			};

		case 'REVENUE':
			return {
				...state,
				Revenue: action.value
			};

		case 'COGS':
			return {
				...state,
				COGS: action.value
			};

		case 'GROSS_PROFIT':
			return {
				...state,
				'Gross Profit': action.value
			};
		case 'DIRECT_LABOUR':
			return {
				...state,
				'Direct Labour': action.value
			};

		case 'EBIT':
			return {
				...state,
				EBIT: action.value
			};

		case 'NET_PROFIT':
			return {
				...state,
				'Net Profit': action.value
			};

		case 'DEPRECIATION':
			return {
				...state,
				Depreciation: action.value
			};

		case 'INTEREST_PAID':
			return {
				...state,
				'Interest Paid': action.value
			};

		case 'OTHER_INCOME':
			return {
				...state,
				'Other Income': action.value
			};

		case 'DIVIDENDS':
			return {
				...state,
				Dividends: action.value
			};

		case 'CASH_IN_BANK':
			return {
				...state,
				'Cash at Bank': action.value
			};

		case 'TRADE_DEBTORS':
			return {
				...state,
				'Trade Debtors': action.value
			};

		case 'STOCK_DAYS_WIP':
			return {
				...state,
				Stock: action.value
			};

		case 'TOTAL_CURRENT_ASSETS':
			return {
				...state,
				'Additional Total Current Assets': action.value
			};

		case 'FIXED_ASSETS':
			return {
				...state,
				'Additional Fixed Assets': action.value
			};

		case 'TRADE_CREDITORS':
			return {
				...state,
				'Trade Creditors': action.value
			};

		case 'TOTAL_ASSETS':
			return {
				...state,
				'Additional Total Assets': action.value
			};

		case 'TOTAL_CURRENT_LIABILITIES':
			return {
				...state,
				'Additional Total Current Liabilities': action.value
			};

		case 'TOTAL_LIABILITIES':
			return {
				...state,
				'Additional Total Liabilities': action.value
			};

		case 'BANK_LOANS_CURRENT':
			return {
				...state,
				'Additional Bank Loans - Current': action.value
			};

		case 'BANK_LOANS_NON_CURRENT':
			return {
				...state,
				'Additional Bank Loans - Non Current': action.value
			};

		case 'INCREASE_REVENUE':
			return {
				...state,
				'Increase Revenue By': action.value
			};

		case 'EDIT':
			return financialForEdit as FinancialsInterface;

		case 'CLEAR':
			return initialState;

		default:
			return initialState;
	}
}

export interface UserModel {
	plan: string;
	number: number;
}
export const userValues: UserModel[] = [
	{
		plan: 'Undefined',
		number: 0
	},
	{
		plan: 'Basic Plan',
		number: 1
	},
	{
		plan: 'Grow Plan',
		number: 2
	},
	{
		plan: 'Pro Plan',
		number: 3
	}
];

export function changeCompanyHandler(
	event: SelectChangeEvent<string>,
	setIsCompanySelected: React.Dispatch<React.SetStateAction<boolean>>,
	setPayload: React.Dispatch<React.SetStateAction<{ company_id: string }>>,
	setFinancialsId: React.Dispatch<React.SetStateAction<string>>,
	setRecordUpdated: React.Dispatch<React.SetStateAction<string>>,
	setCompanyType: React.Dispatch<React.SetStateAction<string>>,
	financialsContext: FinancialsContextModel,
	globalStateContext: GlobalStateModel
): void {
	if (event.target.value !== '') {
		setIsCompanySelected(true);
	}
	setPayload({
		company_id: event.target.value
	});
	const randomNumber: number = Math.random() * Math.random();
	setFinancialsId(JSON.stringify(randomNumber));
	const selectedCompany: CompanyDbModel[] = (
		globalStateContext.companies as CompanyDbModel[]
	).filter((item) => item.company_id === event.target.value);
	if (selectedCompany[0]) {
		if (
			selectedCompany[0].record_updated &&
			selectedCompany[0].industry_type &&
			selectedCompany[0].currency_symbol
		) {
			setRecordUpdated(selectedCompany[0].record_updated);
			setCompanyType(selectedCompany[0].industry_type);
			financialsContext.getCompanyTypeHandler(selectedCompany[0].industry_type);
			financialsContext.getCompanySignHandler(
				typeof selectedCompany[0].currency_symbol === 'string'
					? selectedCompany[0].currency_symbol
					: selectedCompany[0].currency_symbol.label
			);
			financialsContext.ebitdaHandler(selectedCompany[0].industry_id);
		}
	}
}
