import { useEffect, useRef, useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import Toggle from '../../../components/Toggle';
import { Icon } from './icons';

const TEXT_DOMAIN = 'stock-sync-with-google-sheet-for-woocommerce';

export const GUIDE_URL = 'https://wppool.dev/docs-category/stock-sync-for-woocommerce-with-google-sheet/';

/** Page header: what this page controls in one sentence, plus a "Learn more" link. */
export const PageHeader = ( { title, description, learnMore = GUIDE_URL, actions } ) => (
	<header className="ssgsw-ui-pagehead">
		<div>
			<h1 className="ssgsw-ui-pagehead__title">{ title }</h1>
			{ description && (
				<p className="ssgsw-ui-pagehead__desc">
					{ description }{ ' ' }
					{ learnMore && (
						<a href={ learnMore } target="_blank" rel="noreferrer" className="ssgsw-ui-link">
							{ __( 'Learn more', TEXT_DOMAIN ) }
						</a>
					) }
				</p>
			) }
		</div>
		{ actions && <div className="ssgsw-ui-pagehead__actions">{ actions }</div> }
	</header>
);

export const Card = ( { title, description, children, className = '', id } ) => (
	<section className={ `ssgsw-ui-card ${ className }` } id={ id }>
		{ ( title || description ) && (
			<div className="ssgsw-ui-card__head">
				{ title && <h2 className="ssgsw-ui-card__title">{ title }</h2> }
				{ description && <p className="ssgsw-ui-card__desc">{ description }</p> }
			</div>
		) }
		<div className="ssgsw-ui-card__body">{ children }</div>
	</section>
);

/** One setting: friendly label + one line of helper text on the left, the control on the right. */
export const SettingRow = ( { id, label, help, children, stacked = false, htmlFor } ) => (
	<div className={ `ssgsw-ui-row${ stacked ? ' is-stacked' : '' }` } id={ id ? `ssgsw-row-${ id }` : undefined } data-setting={ id }>
		<div className="ssgsw-ui-row__text">
			<label className="ssgsw-ui-row__label" htmlFor={ htmlFor || id }>{ label }</label>
			{ help && <p className="ssgsw-ui-row__help" id={ id ? `${ id }-help` : undefined }>{ help }</p> }
		</div>
		<div className="ssgsw-ui-row__control">{ children }</div>
	</div>
);

/** One readable sentence with the fields inside it, e.g. "Look back [ 28 ] days". `id` lets "Find a setting" jump here. */
export const InlineSentence = ( { id, children, help } ) => (
	<div className="ssgsw-ui-sentence" id={ id ? `ssgsw-row-${ id }` : undefined } data-setting={ id }>
		<div className="ssgsw-ui-sentence__line">{ children }</div>
		{ help && <p className="ssgsw-ui-row__help">{ help }</p> }
	</div>
);

/** A setting that is an on/off switch. */
export const SwitchRow = ( { id, label, help, checked, onChange, disabled } ) => (
	<SettingRow id={ id } label={ label } help={ help }>
		<Toggle id={ id } name={ id } checked={ !! checked } onChange={ onChange } disabled={ disabled } describedBy={ help ? `${ id }-help` : undefined } />
	</SettingRow>
);

export const NumberInput = ( { id, value, onChange, min, max, step = 1, suffix, ariaLabel, inline = false, onFocus } ) => (
	<span className="ssgsw-ui-numwrap">
		<input
			id={ id }
			type="number"
			className={ `ssgsw-ui-input ssgsw-ui-input--num${ inline ? ' ssgsw-ui-input--inline' : '' }` }
			aria-label={ ariaLabel }
			onFocus={ onFocus }
			value={ value }
			min={ min }
			max={ max }
			step={ step }
			onChange={ ( event ) => onChange( event.target.value === '' ? '' : Number( event.target.value ) ) }
		/>
		{ suffix && <span className="ssgsw-ui-suffix">{ suffix }</span> }
	</span>
);

export const Select = ( { id, value, onChange, options, inline = false } ) => (
	<select id={ id } className={ `ssgsw-ui-input${ inline ? ' ssgsw-ui-input--inline' : '' }` } value={ value } onChange={ ( event ) => onChange( event.target.value ) }>
		{ options.map( ( option ) => (
			<option key={ option.value } value={ option.value }>{ option.label }</option>
		) ) }
	</select>
);

export const TextInput = ( { id, value, onChange, placeholder, type = 'text', ...rest } ) => (
	<input id={ id } type={ type } className="ssgsw-ui-input" value={ value } placeholder={ placeholder } onChange={ ( event ) => onChange( event.target.value ) } { ...rest } />
);

export const Button = ( { variant = 'secondary', children, ...rest } ) => (
	<button type="button" className={ `ssgsw-ui-btn ssgsw-ui-btn--${ variant }` } { ...rest }>{ children }</button>
);

export const Badge = ( { tone = 'neutral', children } ) => <span className={ `ssgsw-ui-badge ssgsw-ui-badge--${ tone }` }>{ children }</span>;

export const Notice = ( { tone = 'info', children } ) => (
	<p className={ `ssgsw-ui-notice ssgsw-ui-notice--${ tone }` } role={ tone === 'warning' ? 'alert' : undefined }>
		<Icon name="info" size={ 16 } />
		<span>{ children }</span>
	</p>
);

/** Rare options, collapsed by default. */
export const Advanced = ( { title, children } ) => (
	<details className="ssgsw-ui-advanced">
		<summary>
			<Icon name="chevron" size={ 14 } className="ssgsw-ui-advanced__chev" />
			{ title || __( 'Advanced options', TEXT_DOMAIN ) }
		</summary>
		<div className="ssgsw-ui-advanced__body">{ children }</div>
	</details>
);

/** Small "?" tip. */
export const HelpTip = ( { children } ) => {
	const [ open, setOpen ] = useState( false );
	const ref = useRef( null );
	useEffect( () => {
		if ( ! open ) {
			return undefined;
		}
		const close = ( e ) => ref.current && ! ref.current.contains( e.target ) && setOpen( false );
		document.addEventListener( 'mousedown', close );
		return () => document.removeEventListener( 'mousedown', close );
	}, [ open ] );
	return (
		<span className="ssgsw-ui-tip" ref={ ref } onMouseEnter={ () => setOpen( true ) } onMouseLeave={ () => setOpen( false ) }>
			<button type="button" aria-expanded={ open } aria-label={ __( 'What is this?', TEXT_DOMAIN ) } onFocus={ () => setOpen( true ) } onBlur={ () => setOpen( false ) } onClick={ () => setOpen( true ) } onKeyDown={ ( e ) => e.key === 'Escape' && setOpen( false ) }>
				<Icon name="help" size={ 15 } />
			</button>
			{ open && <span className="ssgsw-ui-tip__pop" role="note">{ children }</span> }
		</span>
	);
};

/** Chip-style checkbox group. */
export const ChipGroup = ( { legend, options, value, onChange } ) => (
	<fieldset className="ssgsw-ui-chips">
		{ legend && <legend className="ssgsw-ui-row__label">{ legend }</legend> }
		<div className="ssgsw-ui-chips__list">
			{ options.map( ( opt ) => {
				const on = value.includes( opt.value );
				return (
					<label key={ opt.value } className={ `ssgsw-ui-chip${ on ? ' is-on' : '' }` }>
						<input type="checkbox" className="ssgsw-ui-sr" checked={ on } onChange={ ( e ) => onChange( opt.value, e.target.checked ) } />
						{ on && <Icon name="check" size={ 13 } /> }
						<span>{ opt.label }</span>
					</label>
				);
			} ) }
		</div>
	</fieldset>
);

/** Settings page body: settings on the left, sticky preview on the right. */
export const SettingsLayout = ( { children, preview } ) => (
	<div className="ssgsw-ui-split">
		<div className="ssgsw-ui-split__main">{ children }</div>
		{ preview && <aside className="ssgsw-ui-split__preview" aria-label={ __( 'Preview', TEXT_DOMAIN ) }>{ preview }</aside> }
	</div>
);
