import type { SelectChangeEvent } from '@mui/material';
import type { AxiosResponse } from 'axios';
import axios from 'axios';
import type { ChangeEvent } from 'react';
import type React from 'react';
import { createContext } from 'react';

import environment from '../../Environments/environment';
import companyDropdownPredefinedValues from '../../Pages/Companies/Companies.helper';
import type { AddCompanyModel } from '../../Pages/Companies/Components/AddNewCompany';
import type { CompanyDbModel } from '../../Pages/Companies/Components/CompaniesComponents.helper';
import styles from '../../Pages/Companies/Components/CompaniesList.module.scss';
import type { FindIndustryInterface } from './companies-context';

export type CompanyContextModel = {
	isChecked: boolean | null;
	addCompany: (props: boolean) => void;
	editCompany: (id: string) => void;
	companyForEdit: CompanyDbModel;
	listCompany: (event: CompanyDbModel[]) => void;
	addNewCompany: () => void;
	editMode: boolean;
	deleteHandler: (event: string) => void;
	addRemoveMessage: string;
	switchToForm: boolean;
	switchHandler: () => void;
	cancelCompanyHandler: () => void;
	companyNameHandler: (event: ChangeEvent<HTMLInputElement>) => void;
	industryTypeHandler: (event: SelectChangeEvent<string>) => void;
	industryHandler: (event: SelectChangeEvent<string>) => void;
	roleHandler: (event: SelectChangeEvent<string>) => void;
	currencyHandler: (event: SelectChangeEvent<string>) => void;
	companyName: string;
	industryType: string;
	industry: string;
	currency: string;
	companyId: string;
	saveEditedCompany: () => void;
	addOrRemoveCompanyClass: string;
	errorMessage: { companyName: string; industryType: string };
	resetErrorMessageHandler: () => void;
	createdCompany: AddCompanyModel;
	companyRegNumber: number;
	companyRegNumberHandler: (event: ChangeEvent<HTMLInputElement>) => void;
	subscriberHandler: (event: ChangeEvent<HTMLInputElement>) => void;
	subscriber: string;
};

export const CompaniesContext: React.Context<CompanyContextModel> =
	createContext<CompanyContextModel>({
		isChecked: false,
		addCompany: (): void => {},
		editCompany: (): void => {},
		companyForEdit: {
			company_id: '',
			cognito_username: '',
			_id: '',
			company_name: '',
			currency_symbol: '',
			gdpr: true,
			industry_id: 0,
			industry_type: '',
			record_created: '',
			record_updated: '',
			company_reg_number: '',
			plan: {
				cost: 0,
				description: '',
				title: '',
				id: 0
			}
		},
		listCompany: () => {},
		addNewCompany: () => {},
		editMode: false,
		deleteHandler: (): void => {},
		addRemoveMessage: '',
		switchToForm: false,
		switchHandler: (): void => {},
		cancelCompanyHandler: (): void => {},
		companyNameHandler: (): void => {},
		industryTypeHandler: (): void => {},
		industryHandler: (): void => {},
		roleHandler: (): void => {},
		currencyHandler: (): void => {},
		companyName: '',
		industryType: companyDropdownPredefinedValues.industryType[0].value,
		currency: companyDropdownPredefinedValues.currency[0].value,
		industry: companyDropdownPredefinedValues.industry[0].value,
		companyId: '',
		saveEditedCompany: (): void => {},
		addOrRemoveCompanyClass: '',
		errorMessage: {
			companyName: '',
			industryType: ''
		},
		resetErrorMessageHandler: (): void => {},
		createdCompany: {
			company_id: '',
			company_name: '',
			currency_symbol: '',
			industry_id: 0,
			company_reg_number: 0,
			industry_type: ''
		},
		companyRegNumber: 0,
		companyRegNumberHandler: (): void => {},
		subscriberHandler: (): void => {},
		subscriber: ''
	});

export interface CompaniesStateInterface {
	isListOn: boolean;
	editMode: boolean;
	switchToForm: boolean;
	companies: CompanyDbModel[];
	addRemoveMessage: string;
	companyForEdit: CompanyDbModel;
	companyName: string;
	industry: string;
	industryType: string;
	currency: string;
	companyId: string;
	companyRegNumber: number;
	subscriber: string;
	role: string;
}

export const companiesInitialState: CompaniesStateInterface = {
	isListOn: false,
	editMode: false,
	switchToForm: false,
	companies: [],
	addRemoveMessage: '',
	companyForEdit: {
		company_id: '',
		cognito_username: '',
		_id: '',
		company_name: '',
		currency_symbol: '',
		gdpr: true,
		industry_id: 0,
		industry_type: '',
		record_created: '',
		record_updated: '',
		company_reg_number: '',
		plan: {
			cost: 0,
			description: '',
			title: '',
			id: 0
		}
	},
	companyName: '',
	industry: companyDropdownPredefinedValues.industry[0].value,
	industryType: companyDropdownPredefinedValues.industryType[0].value,
	currency: companyDropdownPredefinedValues.currency[0].value,
	companyId: '',
	companyRegNumber: 0,
	subscriber: '',
	role: ''
};

export interface Action {
	type: string;
	value: string | number | boolean | CompanyDbModel | CompanyDbModel[];
}

export function companyReducerHelper(
	state: CompaniesStateInterface,
	action: Action
): CompaniesStateInterface {
	switch (action.type) {
		case 'IS_LIST_ON':
			return {
				...state,
				isListOn: action.value as boolean
			};
		case 'EDIT_MODE':
			return {
				...state,
				editMode: action.value as boolean
			};
		case 'SWITCH_TO_FORM':
			return {
				...state,
				switchToForm: action.value as boolean
			};
		case 'COMPANY_FOR_EDIT':
			return {
				...state,
				companyForEdit: action.value as CompanyDbModel
			};
		case 'COMPANIES':
			return {
				...state,
				companies: action.value as CompanyDbModel[]
			};
		case 'ADD_REMOVE_MESSAGE':
			return {
				...state,
				addRemoveMessage: action.value as string
			};
		case 'COMPANY_NAME':
			return {
				...state,
				companyName: action.value as string
			};
		case 'INDUSTRY':
			return {
				...state,
				industry: action.value as string
			};
		case 'ROLE':
			return {
				...state,
				role: action.value as string
			};
		case 'INDUSTRY_TYPE':
			return {
				...state,
				industryType: action.value as string
			};
		case 'CURRENCY':
			return {
				...state,
				currency: action.value as string
			};
		case 'COMPANY_REG_NUMBER':
			return {
				...state,
				companyRegNumber: +action.value
			};
		case 'SUBSCRIBER':
			return {
				...state,
				subscriber: action.value as string
			};
		case 'COMPANY_ID':
			return {
				...state,
				companyId: action.value as string
			};
		case 'RESET':
			return companiesInitialState;
		default:
			return state;
	}
}

const { apiUrl, backendRoutes } = environment;

export async function createCompanyRequestHandler(
	companies: AddCompanyModel
	// ): Promise<AxiosResponse<CompanyDbModel[] | string>> {
): Promise<CompanyDbModel[] | string> {
	// const ret = await axios.post(apiUrl + backendRoutes.company, companies);
	// return ret;
	return 'method should not be called';
}

export default function checkForEmptyCompanyValues(
	companyName: string,
	findIndustry: FindIndustryInterface | undefined,
	setErrorMessage: React.Dispatch<
		React.SetStateAction<{
			companyName: string;
			industryType: string;
		}>
	>
): void {
	if (!companyName) {
		setErrorMessage((prevState) => ({
			...prevState,
			companyName: styles.errorMessage
		}));
	} else {
		setErrorMessage((prevState) => ({
			...prevState,
			companyName: ''
		}));
	}

	if (!findIndustry) {
		setErrorMessage((prevState) => ({
			...prevState,
			industryType: styles.errorMessage
		}));
	} else if (findIndustry.value === 'null') {
		setErrorMessage((prevState) => ({
			...prevState,
			industryType: styles.errorMessage
		}));
	} else {
		setErrorMessage((prevState) => ({
			...prevState,
			industryType: ''
		}));
	}
}
