import React from "react";
import { TraInfoEXWidget, getDelayData } from "./TraInfoEXWidget";
import { InfoWidget, getInfoString } from "./InfoWidget";
import {
  FelicaQuickAccessWidget,
  getFelicaQuickAccessData,
} from "./FelicaQuickAccessWidget";
import { ShortcutWidget, getShortcutData } from "./ShortcutWidget";
import { StrangeTrainWidget, getStrangeTrainData } from "./StrangeTrainWidget";

export const nameToWidget = {
  JR_shikoku_train_info: TraInfoEXWidget,
  Info_Widget: InfoWidget,
  JR_shikoku_apps_shortcut: ShortcutWidget,
  JR_shikoku_felica_balance: FelicaQuickAccessWidget,
  JR_shikoku_train_strange: StrangeTrainWidget,
};

export async function widgetTaskHandler(props) {
  const {
    widgetInfo,
    widgetAction,
    renderWidget,
    clickAction,
    clickActionData,
  } = props;

  switch (widgetAction) {
    case "WIDGET_ADDED":
    case "WIDGET_UPDATE":
    case "WIDGET_CLICK":
    case "WIDGET_RESIZED": {
      const name = widgetInfo.widgetName;

      if (name === "JR_shikoku_felica_balance") {
        const quickData = await getFelicaQuickAccessData();
        renderWidget(<FelicaQuickAccessWidget {...quickData} />);
        break;
      }

      if (name === "JR_shikoku_apps_shortcut") {
        const data = await getShortcutData();
        renderWidget(<ShortcutWidget {...data} />);
        break;
      }

      if (name === "JR_shikoku_train_strange") {
        const data = getStrangeTrainData();
        renderWidget(<StrangeTrainWidget {...data} />);
        break;
      }

      if (name === "JR_shikoku_info") {
        const { time, text } = await getInfoString();
        renderWidget(
          <InfoWidget time={time} text={text && text.toString()} />
        );
        break;
      }

      // JR_shikoku_train_info and default
      {
        const { time, delayString } = await getDelayData();
        renderWidget(
          <TraInfoEXWidget time={time} delayString={delayString} />
        );
      }
      break;
    }

    case "WIDGET_DELETED":
      break;
    default:
      break;
  }
}
