import {useEffect, useState} from '@wordpress/element';
import {__, _x, _n, _nx} from '@wordpress/i18n';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import Card from "react-bootstrap/Card";
import Spinner from 'react-bootstrap/Spinner';
import Form from 'react-bootstrap/Form';
import PlayerCheckbox from "./PlayerCheckbox";
import ResCheckbox from "./ResCheckbox";
import ColorPick from "./ColorPick";
import Tabs from "react-bootstrap/Tabs";
import Tab from "react-bootstrap/Tab";
import Button from "react-bootstrap/Button";
import Alert from "react-bootstrap/Alert";

export default function Settings() {
	const [loading, setLoading] = useState(false);
	const [settings, setSettings] = useState(IUP_VIDEO.settings);
	const [saveStatus, setSaveStatus] = useState(null); // null | 'success' | 'error'
	const [saveError, setSaveError] = useState('');

	function updateSettings() {
		setLoading(true);
		setSaveStatus(null);
		const formData = new FormData();
		formData.append('settings', JSON.stringify(settings));
		formData.append('nonce', IUP_VIDEO.nonce);

		const options = {
			method: 'POST',
			headers: {
				Accept: 'application/json',
			},
			body: formData,
		};

		fetch(`${ajaxurl}?action=infinite-uploads-video-settings`, options)
			.then((response) => response.json())
			.then((data) => {
				if (data.success) {
					setSettings(data.data);
					setSaveStatus('success');
				} else {
					console.error(data.data);
					setSaveStatus('error');
					setSaveError(typeof data.data === 'string' ? data.data : __('Failed to save settings.', 'infinite-uploads'));
				}
				setLoading(false);
			})
			.catch((error) => {
				console.log('Error:', error);
				setSaveStatus('error');
				setSaveError(error.message || __('An unexpected error occurred.', 'infinite-uploads'));
				setLoading(false);
			});
	}

	if (!settings) {
		return (
			<h2>{__('Video library not yet connected.', 'infinite-uploads')}</h2>
		)
	}

	return (
		<Container fluid>
			<Row className="justify-content-between align-items-center">
				<Col>
					<h1 className="text-muted mb-3">
						<img
							src={IUP_VIDEO.assetBase + '/img/iu-logo-gray.svg'}
							alt="Infinite Uploads Logo"
							height="32"
							width="32"
							className="me-2"
						/>
						{__('Infinite Uploads Video Settings', 'infinite-uploads')}
					</h1>
				</Col>
				<Col>
					<Button variant="primary" className="float-end" href={IUP_VIDEO.libraryUrl}>
						{__('Video Library', 'infinite-uploads')}
					</Button>
				</Col>
			</Row>

			<Card>
				<Card.Body>
					<Tabs
						defaultActiveKey="player"
						id="video-settings-tabs"
						className="mb-3"
					>
						<Tab eventKey="player" title={__('Player', 'infinite-uploads')} className="mt-4">
							<Row className="justify-content-center mb-5" xs={1} md={2}>
								<Col>
									<h5>{__('Main Player Color', 'infinite-uploads')}</h5>
									<p className="lead">{__('Select the primary color that will be displayed for the controls in the video player.', 'infinite-uploads')}</p>
								</Col>
								<Col>
									<ColorPick {...{settings, setSettings}} />
								</Col>
							</Row>
							<Row>
								<Col>
									<h5>{__('Enable Direct Play', 'infinite-uploads')}</h5>
									<p className="lead">{__('Enables video playback in the browser using the direct video URL. Anyone with the URL or video ID will be able to watch the video.', 'infinite-uploads')}</p>
								</Col>
								<Col>
									<Form>
										<Form.Check
											type="switch"
											id="allow-direct-play"
											label=""
											className={`custom-switch ${settings.AllowDirectPlay ? 'on' : 'off'}`}
											checked={!!settings.AllowDirectPlay}
											onChange={() =>
												setSettings({
													...settings,
													AllowDirectPlay: !settings.AllowDirectPlay,
												})
											}
										/>
									</Form>
								</Col>
							</Row>
							<Row className="justify-content-center mb-5" xs={1} md={2}>
								<Col>
									<h5>{__('Player Language', 'infinite-uploads')}</h5>
									<p className="lead">{__('Select the default language that will be displayed in the video player.', 'infinite-uploads')}</p>
								</Col>
								<Col>
									<Form.Select size="lg" value={settings.UILanguage} onChange={(e) => setSettings({...settings, UILanguage: e.target.value})}>
										<option value="en" label="English"></option>
										<option value="ar" label="Arabic"></option>
										<option value="bu" label="Bulgarian"></option>
										<option value="cn" label="Chinese"></option>
										<option value="cz" label="Czech"></option>
										<option value="dk" label="Danish"></option>
										<option value="nl" label="Dutch"></option>
										<option value="fi" label="Finnish"></option>
										<option value="fr" label="French"></option>
										<option value="de" label="German"></option>
										<option value="gr" label="Greek"></option>
										<option value="hu" label="Hungarian"></option>
										<option value="id" label="Indonesian"></option>
										<option value="it" label="Italian"></option>
										<option value="jp" label="Japanese"></option>
										<option value="kr" label="Korean"></option>
										<option value="no" label="Norwegian"></option>
										<option value="pl" label="Polish"></option>
										<option value="pt" label="Portuguese"></option>
										<option value="ro" label="Romanian"></option>
										<option value="rs" label="Serbian"></option>
										<option value="sk" label="Slovakian"></option>
										<option value="si" label="Slovenian"></option>
										<option value="es" label="Spanish"></option>
										<option value="se" label="Swedish"></option>
										<option value="ru" label="Russian"></option>
										<option value="th" label="Thai"></option>
										<option value="tr" label="Turkish"></option>
										<option value="ua" label="Ukrainian"></option>
										<option value="vn" label="Vietnamese"></option>
									</Form.Select>
								</Col>
							</Row>
							<Row className="justify-content-center mb-5 iu-player-control" xs={1} md={2}>
								<Col>
									<h5>{__('Player Controls', 'infinite-uploads')}</h5>
									<p className="lead">{__('Select the UI controls that will be displayed on the player.', 'infinite-uploads')}</p>
								</Col>
								<Col className="d-flex flex-wrap justify-content-between">
									<Row>
										<PlayerCheckbox control="play" icon="controls-play" label={__('Play / Pause', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="play-large" icon="video-alt3" label={__('Center Play Button', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="volume" icon="controls-volumeon" label={__('Volume', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="mute" icon="controls-volumeoff" label={__('Mute', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="pip" icon="external" label={__('Picture-in-Picture', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="settings" icon="admin-generic" label={__('Settings', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="captions" icon="format-status" label={__('Captions', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="current-time" icon="clock" label={__('Current Time', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="duration" icon="editor-video" label={__('Duration', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="rewind" icon="controls-skipback" label={__('10s Backward', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="fast-forward" icon="controls-skipforward" label={__('10s Forward', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="progress" icon="leftright" label={__('Progress Bar', 'infinite-uploads')} {...{settings, setSettings}} />
										<PlayerCheckbox control="fullscreen" icon="fullscreen-alt" label={__('Full Screen', 'infinite-uploads')} {...{settings, setSettings}} />
									</Row>
								</Col>
							</Row>
						</Tab>
						<Tab eventKey="encoding" title={__('Encoding', 'infinite-uploads')} className="mt-4">
							<Row className="justify-content-center mb-5" xs={1} md={2}>
								<Col>
									<h5>{__('Enabled Resolutions', 'infinite-uploads')}</h5>
									<p className="lead">{__('Select the enabled resolutions that will be encoded on upload. More resolutions provide a more efficient streaming service to users, but require more storage space. Resolutions larger than the original video will be skipped.', 'infinite-uploads')}</p>
								</Col>
								<Col>
									<ResCheckbox {...{settings, setSettings}} px={240} bitrate={600}/>
									<ResCheckbox {...{settings, setSettings}} px={360} bitrate={800}/>
									<ResCheckbox {...{settings, setSettings}} px={480} bitrate={1400}/>
									<ResCheckbox {...{settings, setSettings}} px={720} bitrate={2800}/>
									<ResCheckbox {...{settings, setSettings}} px={1080} bitrate={5000}/>
									<ResCheckbox {...{settings, setSettings}} px={1440} bitrate={8000}/>
									<ResCheckbox {...{settings, setSettings}} px={2160} bitrate={25000}/>
								</Col>
							</Row>
						</Tab>
					</Tabs>

					{saveStatus === 'success' && (
					<Alert variant="success" onClose={() => setSaveStatus(null)} dismissible>
						{__('Settings saved successfully.', 'infinite-uploads')}
					</Alert>
				)}
				{saveStatus === 'error' && (
					<Alert variant="danger" onClose={() => setSaveStatus(null)} dismissible>
						{saveError}
					</Alert>
				)}
				<Row className="justify-content-center mb-3">
						<Col className="text-center">
							<Button variant="info" className="text-nowrap text-white px-4" onClick={updateSettings} disabled={loading}>{__('Save Settings', 'infinite-uploads')}</Button>
						</Col>
					</Row>
				</Card.Body>
			</Card>

		</Container>
	);
}
