import React from 'react';
import { LLWidgetHeader } from '../LLWidgetHeader';
import { LLWidgetFooter } from '../LLWidgetFooter';
import {
  useWidgetInteractiveTimeout,
  useWidgetExpiryEffect,
} from '../../hooks';
import { LLBaseWidgetProps } from '../LLBaseWidgetProps';
import { LLCoreWidget } from '../LLCoreWidget';
import { LLVoteWidgetBody, LLVoteWidgetBodyProps } from '../LLVoteWidgetBody';

export type LLPollWidgetProps = LLBaseWidgetProps & {
  inlineFollowUp?: boolean;
  BodyComponent?: typeof LLVoteWidgetBody;
  BodyComponentStyles?: LLVoteWidgetBodyProps['styles'];
};

export function LLPollWidget({
  programId,
  widgetId,
  widgetKind,
  onDismiss: onDismissProp,
  interactiveTimeout: interactiveTimeoutProp,
  onInteractiveTimeout: onInteractiveTimeoutProp,
  WidgetComponent = LLCoreWidget,
  HeaderComponent = LLWidgetHeader,
  BodyComponent = LLVoteWidgetBody,
  FooterComponent = LLWidgetFooter,
  WidgetComponentStyles,
  HeaderComponentStyles,
  BodyComponentStyles,
  FooterComponentStyles,
}: LLPollWidgetProps) {
  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} />
        </>
      )}
    </WidgetComponent>
  );
}
