import clsx from 'clsx';
import type { ReactNode } from 'react';
import { speak } from '@wordpress/a11y';
import { Component, Suspense } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { inertValue } from '@wordpress/react-inert-value';
import { Card, Notice, Spinner, Stack, VisuallyHidden } from '@wordpress/ui';
import type { WidgetType } from '@wordpress/widget-primitives';
import { WidgetHeader } from '../widget-header';
import { WidgetRender } from '../widget-render';
import { WidgetFrameFooter } from './widget-frame-footer';
import styles from './widget-frame.module.css';
import type { DashboardWidget } from '../../types';

interface ErrorBoundaryProps {
	children: ReactNode;
}

interface ErrorBoundaryState {
	hasError: boolean;
}

class WidgetErrorBoundary extends Component<
	ErrorBoundaryProps,
	ErrorBoundaryState
> {
	state: ErrorBoundaryState = { hasError: false };

	static getDerivedStateFromError(): ErrorBoundaryState {
		return { hasError: true };
	}

	componentDidCatch() {
		speak( __( 'This widget encountered an error.' ), 'polite' );
	}

	render() {
		if ( this.state.hasError ) {
			return (
				<Notice.Root intent="error">
					<Notice.Description>
						{ __( 'This widget encountered an error.' ) }
					</Notice.Description>
				</Notice.Root>
			);
		}
		return this.props.children;
	}
}

/* Centered spinner for resolving and Suspense waits. */
export function LoadingOverlay() {
	return (
		<Stack justify="center" align="center" className={ styles.loading }>
			<Spinner />
		</Stack>
	);
}

export interface WidgetFrameProps {
	widget: DashboardWidget< unknown >;
	widgetType: WidgetType;
	titleId: string;

	/**
	 * Inert the body and identity while customizing.
	 */
	editMode?: boolean;

	/**
	 * Toolbar shown beside the identity in the in-card header.
	 */
	headerToolbar?: ReactNode;
}

/**
 * Shared framing: `presentation` into header, content, and the actions footer,
 * with the error/loading boundaries. Hosts supply the `Card.Root` and their
 * own concerns.
 *
 * @param {WidgetFrameProps} props Component props.
 */
export function WidgetFrame( {
	widget,
	widgetType,
	titleId,
	editMode = false,
	headerToolbar,
}: WidgetFrameProps ) {
	// full-bleed hides the header; full-bleed and content-bleed bleed the body.
	const { presentation } = widgetType;
	const isHeaderHidden = presentation === 'full-bleed';
	const isBodyBleeding =
		presentation === 'full-bleed' || presentation === 'content-bleed';

	const body = (
		<WidgetErrorBoundary>
			<Suspense fallback={ <LoadingOverlay /> }>
				<WidgetRender widget={ widget } widgetType={ widgetType } />
			</Suspense>
		</WidgetErrorBoundary>
	);

	return (
		<>
			{ ! isHeaderHidden && (
				<WidgetHeader
					showIdentity
					widgetType={ widgetType }
					titleId={ titleId }
					editMode={ editMode }
				>
					{ headerToolbar }
				</WidgetHeader>
			) }

			<Card.Content
				className={ clsx(
					styles.content,
					isBodyBleeding && styles[ 'bleed-content' ]
				) }
				inert={ inertValue( editMode ) }
			>
				{ isHeaderHidden && widgetType.title && (
					<VisuallyHidden render={ <h2 id={ titleId } /> }>
						{ widgetType.title }
					</VisuallyHidden>
				) }
				{ body }
			</Card.Content>

			<WidgetFrameFooter
				widget={ widget }
				widgetType={ widgetType }
				editMode={ editMode }
			/>
		</>
	);
}
