import NetInfo from '@react-native-community/netinfo';
import axios from 'axios';
import { getFullApiUrl } from '../../config';
import { _showToast } from '../UIs/ToastConfig';
import AxiosError from './axiosErrors';

const TIME_OUT = 5000; // 5 seconds

const _checkInternetConnectivity = async (): Promise<any> => {
	const netInfoState = await NetInfo.fetch();
	return netInfoState?.isConnected;
};

const handleRequestError = (error: any) => {
	switch (error.code) {
		case AxiosError.ECONNABORTED:
		case AxiosError.ETIMEDOUT:
			_showToast('Check internet connection!', 'warning');
			return { error: 'Request timed out' };
		case AxiosError.ERR_NETWORK:
		case AxiosError.ERR_FR_TOO_MANY_REDIRECTS:
		case AxiosError.ERR_BAD_RESPONSE:
		case AxiosError.ERR_BAD_REQUEST:
		case AxiosError.ERR_CANCELED:
		case AxiosError.ERR_NOT_SUPPORT:
		case AxiosError.ERR_INVALID_URL:
			_showToast('Network-related issue!', 'error');
			return { error: 'Network-related issue' };
		case AxiosError.ERR_DEPRECATED:
			_showToast('Deprecated feature used!', 'warning');
			return { error: 'Deprecated feature used' };
		case AxiosError.ERR_BAD_OPTION_VALUE:
		case AxiosError.ERR_BAD_OPTION:
		default:
			_showToast('Unknown error occurred!', 'error');
			return { error: 'Unknown error' };
	}
};

const get = async (endpoint: any, params: any = '', customHeaders = []) => {
	const isConnected = await _checkInternetConnectivity();

	if (!isConnected) {
		_showToast('No Internet Connection!', 'error');
		return { error: 'No Internet Connection' };
	}

	let rootHeaders = {};

	if (customHeaders && customHeaders.length > 0) {
		rootHeaders = { ...rootHeaders, ...customHeaders };
	}

	if (endpoint && endpoint.length > 0) {
		try {
			const response = await axios.get(getFullApiUrl(endpoint) as any, {
				headers: rootHeaders,
				params: params,
				timeout: TIME_OUT
			});
			console.log("end_point ------->", endpoint, '------->', response?.data);
			return response?.data;
		} catch (error) {
			console.log("Error ------->", error);
			return handleRequestError(error);
		}
	}
	return { error: 'Endpoint not provided' };
};

const post = async (endpoint: any, data: any, customHeaders = []) => {
	const isConnected = await _checkInternetConnectivity();

	if (!isConnected) {
		_showToast('No Internet Connection', 'error');
		return { error: 'No Internet Connection' };
	}

	let rootHeaders = {};

	if (customHeaders && customHeaders.length > 0) {
		rootHeaders = { ...rootHeaders, ...customHeaders };
	}

	if (endpoint && endpoint.length > 0) {
		try {
			const response = await axios.post(getFullApiUrl(endpoint) as any, data, {
				headers: rootHeaders,
				timeout: TIME_OUT
			});
			console.log("end_point ------->", endpoint, '------->', response?.data);
			return response?.data;
		} catch (error) {
			console.log("Error ------->", error);
			return handleRequestError(error);
		}
	}
	return { error: 'Endpoint not provided' };
};

const postFormData = async (endpoint: any, data: any, customHeaders = []) => {
	const isConnected = await _checkInternetConnectivity();

	if (!isConnected) {
		_showToast('No Internet Connection', 'error');
		return { error: 'No Internet Connection' };
	}

	let rootHeaders = {};

	if (customHeaders && customHeaders.length > 0) {
		rootHeaders = { ...rootHeaders, ...customHeaders };
	}

	if (endpoint && endpoint.length > 0) {
		try {
			const response = await axios.post(endpoint, data, {
				headers: {
					...rootHeaders,
					'Content-Type': 'multipart/form-data',
				},
				timeout: TIME_OUT
			});
			console.log("end_point ------->", endpoint, '------->', response?.data);
			return response?.data;
		} catch (error) {
			console.log("Error ------->", error);
			return handleRequestError(error);
		}
	}
	return { error: 'Endpoint not provided' };
};


export const APIMethods = {
	get,
	post,
	postFormData
};