import InfoIcon from '@mui/icons-material/Info';
import { Box, Card, FormControl, FormControlLabel, FormLabel, Radio, RadioGroup, Typography } from '@mui/material';
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { Spinner } from '../../index';
import { InputField } from '../../LocationModal';

const initialState = { nickname: '', username: '', password: '', customer_number: '', rate_type: 'S' };

const FIELDS = [
	{ name: 'nickname', label: 'Nickname' },
	{ name: 'username', label: 'Username' },
	{ name: 'password', label: 'Password', secret: true },
	{ name: 'customer_number', label: 'Account number' },
];

const ConnectionSettings = ({ connSettings, setConnSettings, isLoading, errorsMsg }) => {
	const admin_obj = eniture_rtsq || {};
	const { eniture_api_key_status } = useSelector(state => state.plan);

	useEffect(() => {
		setConnSettings(prevState => ({
			...initialState,
			...prevState,
		}));
	}, []);

	if (isLoading) return <Spinner />;

	return (
		<>
			<Box my={2} sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
				<Card
					variant='elevation'
					title='Estes account note'
					sx={{ p: 1, display: 'flex', justifyContent: 'space-between', gap: 2 }}>
					<InfoIcon />
					<Typography variant='subtitle2' sx={{ flexGrow: 1, textWrap: 'wrap !important' }}>
						<strong>Note!</strong> You must have an Estes Express account to use this
						application. If you do not have one, contact Estes Express at 866-378-3748, or
						email{' '}
						<a href='mailto:customercare@estes-express.com' style={{ textDecoration: 'none' }}>
							customercare@estes-express.com
						</a>
						.
					</Typography>
				</Card>
				<Card
					variant='elevation'
					title='Estes REST API note'
					sx={{ p: 1, display: 'flex', justifyContent: 'space-between', gap: 2 }}>
					<InfoIcon />
					<Typography variant='subtitle2' sx={{ flexGrow: 1, textWrap: 'wrap !important' }}>
						<strong>Important:</strong> The Estes REST API uses case-sensitive credentials.
						Enter your username and password exactly as provided by Estes, using the correct
						uppercase and lowercase letters.
					</Typography>
				</Card>
			</Box>

			<Box my={3} width={100}>
				<Card variant=''>
					<img
						src={`${admin_obj?.eniture_rtsq_url}/build/assets/carrier_logos/exla.png`}
						alt='provider icon'
						title='Estes Express'
						style={{ width: '90px', objectFit: 'contain' }}
					/>
				</Card>
			</Box>

			<Box component='form'>
				{eniture_api_key_status === 'error' && (
					<Typography
						color='error'
						variant='subtitle2'
						title='Invalid Eniture API Key'
						sx={{ textWrap: 'wrap !important' }}
						mb={2}>
						<strong>Note!</strong> Your Eniture API Key is missing or invalid. Kindly
						update your API Key in the Eniture API Connection tab.
					</Typography>
				)}

				<form autoComplete='off' onSubmit={e => e.preventDefault()}>
					<Box component='div' sx={{ display: 'flex', flexDirection: 'column', gap: 2 }}>
						{FIELDS.map(({ name, label, secret }) => (
							<InputField
								key={name}
								name={name}
								label={label}
								maxLength={50}
								value={connSettings[name]}
								onChange={(_, val) => setConnSettings(prev => ({ ...prev, [name]: val }))}
								required={true}
								type={secret ? 'password' : 'text'}
								autoComplete={secret ? 'new-password' : 'off'}
								inputProps={{
									id: `eniture-rtsq-estes-${name}`,
									'aria-label': label,
									maxLength: 50,
									autoCapitalize: 'none',
									spellCheck: false,
								}}
								error={Boolean(errorsMsg?.[name])}
								helperText={errorsMsg?.[name] || ''}
							/>
						))}

						<FormControl>
							<FormLabel id='eniture-rtsq-estes-rate-type'>Request LTL freight quotes as:</FormLabel>
							<RadioGroup
								aria-labelledby='eniture-rtsq-estes-rate-type'
								name='eniture_rtsq_estes_rate_type'
								value={connSettings.rate_type === 'T' ? 'T' : 'S'}
								onChange={e => setConnSettings(prev => ({ ...prev, rate_type: e.target.value }))}>
								<FormControlLabel value='S' control={<Radio />} label='Shipper' />
								<FormControlLabel value='T' control={<Radio />} label='3rd party' />
							</RadioGroup>
						</FormControl>
					</Box>
				</form>
			</Box>
		</>
	);
};

export default ConnectionSettings;
