import type React from 'react';
import { AndroidWidget } from '../AndroidWidget';
import { buildWidgetTree } from './build-widget-tree';
import type { WidgetInfo, WidgetRepresentation } from './types';

export interface RequestWidgetUpdateProps {
  /**
   * The name of the widget to update
   */
  widgetName: string;
  /**
   * Callback function that will be called with {@link WidgetInfo}
   * It should return the JSX of the updated widget
   */
  renderWidget: (
    props: WidgetInfo
  ) => Promise<WidgetRepresentation> | WidgetRepresentation;
  /**
   * Callback function that will be called if no widgets are added on the home screen
   * It can be used to clean up background tasks that update the widget periodically
   */
  widgetNotFound?: () => void;
}

/**
 * Request widget update for a given widget name
 * A callback will be called for each widget with that name that is added to the home screen
 *
 * @param param0 {@link RequestWidgetUpdateProps}
 */
export async function requestWidgetUpdate({
  widgetName,
  renderWidget,
  widgetNotFound,
}: RequestWidgetUpdateProps): Promise<void> {
  const widgetsInfo = await AndroidWidget.getWidgetInfo(widgetName);

  if (widgetsInfo.length === 0) {
    widgetNotFound?.();
  }

  widgetsInfo.forEach(async (info: WidgetInfo) => {
    const widgetComponent = await renderWidget(info);

    const lightWidget =
      'light' in widgetComponent
        ? buildWidgetTree(widgetComponent.light)
        : buildWidgetTree(widgetComponent);
    const darkWidget =
      'dark' in widgetComponent
        ? buildWidgetTree(widgetComponent.dark as React.JSX.Element)
        : null;

    const config = {
      light: lightWidget,
      dark: darkWidget,
    };

    AndroidWidget.drawWidgetById(config, widgetName, info.widgetId);
  });
}
