import * as React from 'react';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import { Repeater, type SetRepeaterValuesMeta } from '@elementor/editor-controls';
import { InfoCircleFilledIcon, PlayerPlayIcon } from '@elementor/icons';
import { Alert, AlertTitle, Box, IconButton, Tooltip } from '@elementor/ui';
import { __ } from '@wordpress/i18n';

import { useInteractionsContext } from '../contexts/interactions-context';
import { InteractionItemContextProvider } from '../contexts/interactions-item-context';
import type { ElementInteractions, InteractionItemPropValue, InteractionItemValue } from '../types';
import { buildDisplayLabel, createDefaultInteractionItem, extractString } from '../utils/prop-value-utils';
import {
	dispatchScrollInteraction,
	extractScrollOverlayParams,
	syncGridOverlay,
} from '../utils/scroll-interaction-event';
import { trackInteractionCreated } from '../utils/tracking';
import { DEFAULT_VALUES } from './interaction-details';
import { InteractionsListItem } from './interactions-list-item';
export const MAX_NUMBER_OF_INTERACTIONS = 5;

export type InteractionListProps = {
	onSelectInteractions: ( interactions: ElementInteractions ) => void;
	interactions: ElementInteractions;
	onPlayInteraction: ( interactionId: string ) => void;
	triggerCreateOnShowEmpty?: boolean;
};

export function InteractionsList( props: InteractionListProps ) {
	const { interactions, onSelectInteractions, onPlayInteraction, triggerCreateOnShowEmpty } = props;
	const { elementId } = useInteractionsContext();

	const hasInitializedRef = useRef( false );
	const newlyCreatedIdsRef = useRef< Set< string > >( new Set() );

	const handleUpdateInteractions = useCallback(
		( newInteractions: ElementInteractions ) => {
			onSelectInteractions( newInteractions );
		},
		[ onSelectInteractions ]
	);

	useEffect( () => {
		if (
			triggerCreateOnShowEmpty &&
			! hasInitializedRef.current &&
			( ! interactions.items || interactions.items?.length === 0 )
		) {
			hasInitializedRef.current = true;
			const newItem = createDefaultInteractionItem();
			newlyCreatedIdsRef.current.add( extractString( newItem.value.interaction_id ) );
			const newState: ElementInteractions = {
				version: 1,
				items: [ newItem ],
			};
			handleUpdateInteractions( newState );
		}
	}, [ triggerCreateOnShowEmpty, interactions.items, handleUpdateInteractions ] );

	const isMaxNumberOfInteractionsReached = useMemo( () => {
		return interactions.items?.length >= MAX_NUMBER_OF_INTERACTIONS;
	}, [ interactions.items?.length ] );

	const infotipContent = isMaxNumberOfInteractionsReached ? (
		<Alert color="secondary" icon={ <InfoCircleFilledIcon /> } size="small">
			<AlertTitle>{ __( 'Interactions', 'elementor' ) }</AlertTitle>
			<Box component="span">
				{ __(
					"You've reached the limit of 5 interactions for this element. Please remove an interaction before creating a new one.",
					'elementor'
				) }
			</Box>
		</Alert>
	) : undefined;

	const handleRepeaterChange = useCallback(
		(
			newItems: ElementInteractions[ 'items' ],
			_: unknown,
			meta?: SetRepeaterValuesMeta< InteractionItemPropValue >
		) => {
			handleUpdateInteractions( {
				...interactions,
				items: newItems,
			} );
			if ( meta?.action?.type === 'add' ) {
				const addedItem = meta.action.payload[ 0 ]?.item;
				if ( addedItem ) {
					newlyCreatedIdsRef.current.add( extractString( addedItem.value.interaction_id ) );
				}
			}
		},
		[ interactions, handleUpdateInteractions ]
	);

	const handleInteractionChange = useCallback(
		( index: number, newInteractionValue: InteractionItemValue ) => {
			const newItems = structuredClone( interactions.items );
			newItems[ index ] = {
				$$type: 'interaction-item',
				value: newInteractionValue,
			};
			handleUpdateInteractions( {
				...interactions,
				items: newItems,
			} );
		},
		[ interactions, handleUpdateInteractions ]
	);

	const contextValue = useMemo(
		() => ( {
			onInteractionChange: handleInteractionChange,
			onPlayInteraction,
		} ),
		[ handleInteractionChange, onPlayInteraction ]
	);

	return (
		<InteractionItemContextProvider value={ contextValue }>
			<Repeater
				openOnAdd
				openItem={ triggerCreateOnShowEmpty ? 0 : undefined }
				label={ __( 'Interactions', 'elementor' ) }
				values={ interactions.items }
				setValues={ handleRepeaterChange }
				showDuplicate={ false }
				showToggle={ false }
				isSortable={ false }
				disableAddItemButton={ isMaxNumberOfInteractionsReached }
				addButtonInfotipContent={ infotipContent }
				itemSettings={ {
					initialValues: createDefaultInteractionItem(),
					Label: ( { value }: { value: InteractionItemPropValue } ) => buildDisplayLabel( value.value ),
					Icon: () => null,
					Content: InteractionsListItem,
					onPopoverOpen: ( value: InteractionItemPropValue ) => {
						const { trigger, start, end, relativeTo } = extractScrollOverlayParams(
							value.value,
							DEFAULT_VALUES
						);
						syncGridOverlay( trigger, start, end, relativeTo );
					},
					onPopoverClose: ( value: InteractionItemPropValue ) => {
						dispatchScrollInteraction( null );
						const id = extractString( value.value.interaction_id );
						if ( newlyCreatedIdsRef.current.has( id ) ) {
							newlyCreatedIdsRef.current.delete( id );
							trackInteractionCreated( elementId, value );
						}
					},
					actions: ( value: InteractionItemPropValue ) => (
						<Tooltip key="preview" placement="top" title={ __( 'Preview', 'elementor' ) }>
							<IconButton
								aria-label={ __( 'Play interaction', 'elementor' ) }
								size="tiny"
								onClick={ () => onPlayInteraction( extractString( value.value.interaction_id ) ) }
							>
								<PlayerPlayIcon fontSize="tiny" />
							</IconButton>
						</Tooltip>
					),
				} }
			/>
		</InteractionItemContextProvider>
	);
}
