/**
 * Popup Builder Extension - Main Entry Point
 */
import { useState } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { addFilter } from '@wordpress/hooks';
import { InspectorControls } from '@wordpress/block-editor';
//import UAGAdvancedPanelBody from '@Components/advanced-panel-body';
import TriggerOptions from './settings/TriggerOptions';
import DisplayConditions from './settings/DisplayConditions';
import {
	__experimentalToolsPanel as ToolsPanel,
	__experimentalToolsPanelItem as ToolsPanelItem,
	RangeControl,
	ToggleControl,
	SelectControl
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
 * Add custom attributes to the popup builder block.
 *
 * @param {Object} settings The settings object.
 * @param {string} name     The name of the block.
 * @return {Object}         The modified settings object.
 */
addFilter( 
	'blocks.registerBlockType',
	'spectra-pro/popup-builder/popup-attributes',
	( settings, name ) => {
		
		if ( name !== 'spectra/popup-builder' ) {
			return settings;
		}
		
		return {
			...settings,
			attributes: {
				...settings.attributes,
				enableCookies: {
					type: 'boolean',
					default: false,
				},
				setCookiesOn: {
					type: 'string'
				},
				hideForDays: {
					type: 'number',
					default: 2,
				},
			}
		};
	}
);

/**
 * Popup Builder settings panel (Pro)
 *
 * Provides additional Popup Builder controls in Pro.
 *
 * @param {Object} props - Component props.
 * @return {JSX.Element} modal options panel component.
 */
export const AddProPopupPanels = ( props ) => {
	const { clientId, attributes, setAttributes } = props;

	const {
		enableCookies,
		setCookiesOn,
		hideForDays,
	} = attributes;

	// Get the Meta and setMeta Entity Props.
	const postType = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType() );
	const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );

	// Set the trigger State to Conditionally Render the Repetition Slider when
	const [ trigger, setTrigger ] = useState( meta['spectra-popup-trigger'] );
	return (
		<InspectorControls group="settings">
			<ToolsPanel
				label={__( 'Visibility', 'spectra-pro' )}
				panelId={clientId}
			>
				<DisplayConditions { ...{ meta, setMeta } } />
				<TriggerOptions {...{ meta, setMeta, trigger, setTrigger, clientId } } />
				{ 'element' !== trigger && (
				<>
					<ToolsPanelItem
						hasValue={ () => !! enableCookies }
						label={ __( 'Enable Cookies', 'spectra-pro' ) }
						onDeselect={ () => setAttributes( { enableCookies: false } ) }
						resetAllFilter={ () => ( {
							enableCookies: false,
						} ) }
						isShownByDefault
						panelId={clientId}
					>
						<ToggleControl
							label={ __( 'Enable Cookies', 'spectra-pro' ) }
							checked={ enableCookies }
							onChange={ () =>
								setAttributes( {
									enableCookies: ! enableCookies,
								} )
							}
						/>
					</ToolsPanelItem>
					{ enableCookies && (
						<>
							<ToolsPanelItem
								hasValue={ () => !! setCookiesOn }
								label={ __( 'Set Cookies on', 'spectra-pro' ) }
								onDeselect={ () => setAttributes( { setCookiesOn: 'close-action' } ) }
								resetAllFilter={ () => ( {
									setCookiesOn: 'close-action',
								} ) }
								isShownByDefault
								panelId={clientId}
							>
								<SelectControl
									label={ __( 'Set Cookies on', 'spectra-pro' ) }
									value={ setCookiesOn }
									help={ __(
										'Choose an action on which you want to set cookies to hide the popup for number of days.',
										'spectra-pro'
									) }
									options={ [
										{ value: 'close-action', label: __( 'Close Action', 'spectra-pro' ) },
										{ value: 'page-refresh', label: __( 'Page Refresh', 'spectra-pro' ) },
									] }
									onChange={ ( value ) => setAttributes( { setCookiesOn: value } ) }
								/>
							</ToolsPanelItem>
							<ToolsPanelItem
								hasValue={ () => !! hideForDays }
								label={ __( 'Hide for number of days', 'spectra-pro' ) }
								onDeselect={ () => setAttributes( { hideForDays: 2 } ) }
								resetAllFilter={ () => ( {
									hideForDays: 2,
								} ) }
								isShownByDefault
								panelId={clientId}
							>
								<RangeControl
									__nextHasNoMarginBottom
									__next40pxDefaultSize
									initialPosition={ 2 }
									label={ __( 'Hide for number of days', 'spectra-pro' ) }
									max={ 1000 }
									min={ 0 }
									value={ hideForDays }
									onChange={ ( value ) =>
										setAttributes( {
											hideForDays: value
										} )
									}
								/>
							</ToolsPanelItem>
						</>
					)}
				</>
				)}
			</ToolsPanel>
		</InspectorControls>
	);
};

/**
 * Enhances BlockEdit component with Popup Builder controls in the editor.
 *
 * @since x.x.x
 *
 * @param {Function} BlockEdit - The original BlockEdit component.
 * @return {Function} Enhanced BlockEdit component with Popup Builder controls.
 */
addFilter( 'spectra.popup-builder.extension', 'spectra-pro/popup-builder-options', AddProPopupPanels );