import React from 'react';
import { LLWidgetHeader } from '../LLWidgetHeader';
import {
  useWidgetExpiryEffect,
  useWidgetInteractiveTimeout,
} from '../../hooks';
import { LLCoreWidget } from '../LLCoreWidget';
import { LLBaseWidgetProps } from '../LLBaseWidgetProps';
import { WidgetKind } from '@livelike/javascript';
import {
  LLTextAskWidgetBody,
  LLTextAskWidgetBodyProps,
} from './LLTextAskWidgetBody';
import { LLWidgetFooter } from '../LLWidgetFooter';
import { LLTextAskWidgetActionInfo } from './LLTextAskWidgetActionInfo';

export type LLTextAskWidgetProps = Omit<LLBaseWidgetProps, 'widgetKind'> & {
  BodyComponent?: typeof LLTextAskWidgetBody;
  BodyComponentStyles?: LLTextAskWidgetBodyProps['styles'];
};

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

  useWidgetExpiryEffect({ widgetId });

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