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 {
  LLCheerMeterWidgetBody,
  LLCheerMeterWidgetBodyProps,
} from './LLCheerMeterWidgetBody';
import { WidgetKind } from '@livelike/javascript';

export type LLCheerMeterWidgetProps = Omit<LLBaseWidgetProps, 'widgetKind'> & {
  inlineFollowUp?: boolean;
  BodyComponent?: typeof LLCheerMeterWidgetBody;
  BodyComponentStyles?: LLCheerMeterWidgetBodyProps['styles'];
};

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

  return (
    <WidgetComponent
      widgetId={widgetId}
      widgetKind={WidgetKind.CHEER_METER}
      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>
  );
}
