import React from 'react';
import { LLWidgetHeader } from '../LLWidgetHeader';
import { LLWidgetFooter } from '../LLWidgetFooter';
import { LLQuizWidgetBody, LLQuizWidgetBodyProps } from './LLQuizWidgetBody';
import {
  useWidgetInteractiveTimeout,
  useWidgetExpiryEffect,
} from '../../hooks';
import { LLQuizWidgetActionInfo } from './LLQuizWidgetActionInfo';
import { LLBaseWidgetProps } from '../LLBaseWidgetProps';
import { LLCoreWidget } from '../LLCoreWidget';

export type LLQuizWidgetProps = LLBaseWidgetProps & {
  BodyComponent?: typeof LLQuizWidgetBody;
  BodyComponentStyles?: LLQuizWidgetBodyProps['styles'];
};

export function LLQuizWidget({
  programId,
  widgetId,
  widgetKind,
  onDismiss: onDismissProp,
  interactiveTimeout: interactiveTimeoutProp,
  onInteractiveTimeout: onInteractiveTimeoutProp,
  WidgetComponent = LLCoreWidget,
  HeaderComponent = LLWidgetHeader,
  BodyComponent = LLQuizWidgetBody,
  FooterComponent = LLWidgetFooter,
  WidgetComponentStyles,
  HeaderComponentStyles,
  BodyComponentStyles,
  FooterComponentStyles,
}: LLQuizWidgetProps) {
  const { interactiveTimeout, onInteractiveTimeout } =
    useWidgetInteractiveTimeout({
      widgetId,
      interactiveTimeout: interactiveTimeoutProp,
      onInteractiveTimeout: onInteractiveTimeoutProp,
    });
  useWidgetExpiryEffect({ widgetId });

  return (
    <WidgetComponent
      widgetId={widgetId}
      widgetKind={widgetKind}
      programId={programId}
      onDismiss={onDismissProp}
      styles={WidgetComponentStyles}
    >
      {({ widget, onDismiss }) => (
        <>
          <HeaderComponent
            title={widget.question}
            interactiveTimeout={interactiveTimeout}
            onInteractiveTimeout={onInteractiveTimeout}
            onDismiss={onDismiss}
            dismissable={!!onDismissProp}
            styles={HeaderComponentStyles}
          />
          <BodyComponent widgetId={widgetId} styles={BodyComponentStyles} />
          <FooterComponent
            styles={FooterComponentStyles}
            widgetId={widgetId}
            ActionInfoComponent={LLQuizWidgetActionInfo}
          />
        </>
      )}
    </WidgetComponent>
  );
}
