import React, { ComponentType } from 'react';
import { WidgetKind } from '@livelike/javascript';
import { LLPollWidget, LLPollWidgetProps } from '../LLPollWidget';
import {
  LLPredictionFollowUpWidget,
  LLPredictionFollowUpWidgetProps,
  LLPredictionWidget,
  LLPredictionWidgetProps,
} from '../LLPredictionWidget';
import {
  LLQuizWidget,
  LLQuizWidgetBodyProps,
  LLQuizWidgetProps,
} from '../LLQuizWidget';
import {
  LLNumberPredictionFollowUpWidget,
  LLNumberPredictionFollowUpWidgetProps,
  LLNumberPredictionWidget,
  LLNumberPredictionWidgetProps,
} from '../LLNumberPredictionWidget';
import {
  LLEmojiSliderWidgetProps,
  LLEmojiSliderWidget,
} from '../LLEmojiSliderWidget';
import {
  LLCheerMeterWidget,
  LLCheerMeterWidgetProps,
} from '../LLCheerMeterWidget';
import { LLAlertWidget, LLAlertWidgetProps } from '../LLAlertWidget';
import { LLTextAskWidget, LLTextAskWidgetProps } from '../LLTextAskWidget';
import { LLSocialEmbedWidget } from '../LLSocialEmbedWidget';

type LLAllWidgetProps =
  | LLPollWidgetProps
  | LLPredictionWidgetProps
  | LLPredictionFollowUpWidgetProps
  | LLQuizWidgetBodyProps
  | LLNumberPredictionWidgetProps
  | LLNumberPredictionFollowUpWidgetProps
  | LLEmojiSliderWidgetProps
  | LLCheerMeterWidgetProps
  | LLAlertWidgetProps
  | LLTextAskWidgetProps;

export type LLWidgetProps = LLAllWidgetProps & {
  widgetKind: WidgetKind;
  PollWidgetComponent?: typeof LLPollWidget;
  QuizWidgetComponent?: typeof LLQuizWidget;
  PredictionWidgetComponent?: typeof LLPredictionWidget;
  PredictionFollowUpWidgetComponent?: typeof LLPredictionFollowUpWidget;
  NumberPredictionWidgetComponent?: typeof LLNumberPredictionWidget;
  NumberPredictionFollowUpWidgetComponent?: typeof LLNumberPredictionFollowUpWidget;
  EmojiSliderWidgetComponent?: typeof LLEmojiSliderWidget;
  CheerMeterWidgetComponent?: typeof LLCheerMeterWidget;
  AlertComponent?: typeof LLAlertWidget;
  TextAskComponent?: typeof LLTextAskWidget;
  VideoAlertComponent?: ComponentType<unknown>;
  SocialEmbedComponent?: ComponentType<unknown>;
};
export function LLWidget({
  widgetKind,
  PollWidgetComponent = LLPollWidget,
  QuizWidgetComponent = LLQuizWidget,
  PredictionWidgetComponent = LLPredictionWidget,
  PredictionFollowUpWidgetComponent = LLPredictionFollowUpWidget,
  NumberPredictionWidgetComponent = LLNumberPredictionWidget,
  NumberPredictionFollowUpWidgetComponent = LLNumberPredictionFollowUpWidget,
  EmojiSliderWidgetComponent = LLEmojiSliderWidget,
  CheerMeterWidgetComponent = LLCheerMeterWidget,
  AlertComponent = LLAlertWidget,
  TextAskComponent = LLTextAskWidget,
  VideoAlertComponent,
  SocialEmbedComponent = LLSocialEmbedWidget,
  ...rest
}: LLWidgetProps) {
  switch (widgetKind) {
    case WidgetKind.TEXT_POLL:
    case WidgetKind.IMAGE_POLL:
      return (
        <PollWidgetComponent
          widgetKind={widgetKind}
          {...(rest as LLPollWidgetProps)}
        />
      );

    case WidgetKind.TEXT_QUIZ:
    case WidgetKind.IMAGE_QUIZ:
      return (
        <QuizWidgetComponent
          widgetKind={widgetKind}
          {...(rest as LLQuizWidgetProps)}
        />
      );

    case WidgetKind.TEXT_PREDICTION:
    case WidgetKind.IMAGE_PREDICTION:
      return (
        <PredictionWidgetComponent
          widgetKind={widgetKind}
          {...(rest as LLPredictionWidgetProps)}
        />
      );
    case WidgetKind.TEXT_PREDICTION_FOLLOW_UP:
    case WidgetKind.IMAGE_PREDICTION_FOLLOW_UP:
      return (
        <PredictionFollowUpWidgetComponent
          widgetKind={widgetKind}
          {...(rest as LLPredictionFollowUpWidgetProps)}
        />
      );

    case WidgetKind.TEXT_NUMBER_PREDICTION:
    case WidgetKind.IMAGE_NUMBER_PREDICTION:
      return (
        <NumberPredictionWidgetComponent
          widgetKind={widgetKind}
          {...(rest as LLNumberPredictionWidgetProps)}
        />
      );
    case WidgetKind.TEXT_NUMBER_PREDICTION_FOLLOW_UP:
    case WidgetKind.IMAGE_NUMBER_PREDICTION_FOLLOW_UP:
      return (
        <NumberPredictionFollowUpWidgetComponent
          widgetKind={widgetKind}
          {...(rest as LLNumberPredictionFollowUpWidgetProps)}
        />
      );

    case WidgetKind.EMOJI_SLIDER:
      return (
        <EmojiSliderWidgetComponent {...(rest as LLEmojiSliderWidgetProps)} />
      );

    case WidgetKind.CHEER_METER:
      return (
        <CheerMeterWidgetComponent {...(rest as LLCheerMeterWidgetProps)} />
      );

    case WidgetKind.ALERT:
      return <AlertComponent {...(rest as LLAlertWidgetProps)} />;

    case WidgetKind.TEXT_ASK: {
      return <TextAskComponent {...(rest as LLTextAskWidgetProps)} />;
    }

    case WidgetKind.VIDEO_ALERT: {
      if (VideoAlertComponent) {
        return <VideoAlertComponent {...(rest as any)} />;
      }
      break;
    }

    case WidgetKind.SOCIAL_EMBED: {
      if (SocialEmbedComponent) {
        return <SocialEmbedComponent {...(rest as any)} />;
      }
      break;
    }

    default:
      return undefined;
  }
}
