import InfoIcon from '@mui/icons-material/Info';
import { Box, Card, FormControl, FormControlLabel, 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: '',
	customer_account_number: '',
	username: '',
	password: '',
	billing_postal_code: '',
	bill_to_account_number: '',
	api_key: '',
	test_account_number: '1',
};

const FIELDS = [
	{ name: 'nickname', label: 'Nickname', required: true },
	{ name: 'customer_account_number', label: 'Pickup/Delivery Account Number', required: true },
	{ name: 'username', label: 'Username', required: true },
	{ name: 'password', label: 'Password', required: true, secret: true },
	{ name: 'billing_postal_code', label: 'Pickup/Delivery Postal Code', required: true, maxLength: 10 },
	{ name: 'bill_to_account_number', label: 'Bill To Account Number', required: false },
	{ name: 'api_key', label: 'API key', required: false, maxLength: 500 },
];

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}>
				<Card
					variant='elevation'
					title='XPO 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 XPO account to use this application. If
						you do not have one, contact XPO at 800-755-2728, or{' '}
						<a
							href='https://ltl.xpo.com/webapp/membership_app/membershipSignupCompanySearch.do'
							target='_blank'
							rel='noreferrer'>
							Create an LTL.XPO.com Account
						</a>
						.
					</Typography>
				</Card>
			</Box>

			<Box my={3} width={100}>
				<Card variant=''>
					<img
						src={`${admin_obj?.eniture_rtsq_url}/build/assets/carrier_logos/xpo-logistics.png`}
						alt='provider icon'
						title='XPO Logistics'
						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, required, secret, maxLength = 50 }) => (
							<InputField
								key={name}
								name={name}
								label={label}
								maxLength={maxLength}
								value={connSettings[name]}
								onChange={(_, val) => setConnSettings(prev => ({ ...prev, [name]: val }))}
								required={required}
								type={secret ? 'password' : 'text'}
								autoComplete={secret ? 'new-password' : 'off'}
								inputProps={{
									id: `eniture-rtsq-xpo-${name}`,
									'aria-label': label,
									maxLength,
									autoCapitalize: 'none',
									spellCheck: false,
								}}
								error={Boolean(errorsMsg?.[name])}
								helperText={errorsMsg?.[name] || ''}
							/>
						))}

						<FormControl>
							<RadioGroup
								row
								aria-label='Account to test'
								name='eniture_rtsq_xpo_test_account_number'
								value={String(connSettings.test_account_number) === '2' ? '2' : '1'}
								onChange={e => setConnSettings(prev => ({ ...prev, test_account_number: e.target.value }))}>
								<FormControlLabel value='1' control={<Radio />} label='Test Account Number' />
								<FormControlLabel value='2' control={<Radio />} label='Test Bill To Account Number' />
							</RadioGroup>
						</FormControl>
					</Box>
				</form>
			</Box>
		</>
	);
};

export default ConnectionSettings;
