import React from "react";
import { HelloWidget } from "./HelloWidget";
import dayjs from "dayjs";
import { ToastAndroid } from "react-native";

const nameToWidget = {
  // Hello will be the **name** with which we will reference our widget.
  JR_shikoku_train_info: HelloWidget,
};

export async function widgetTaskHandler(props) {
  const widgetInfo = props.widgetInfo;
  const Widget = nameToWidget[widgetInfo.widgetName];
  const time = dayjs().format("HH:mm");
  ToastAndroid.show(
    `Widget Action: ${props.widgetAction} ${time}`,
    ToastAndroid.SHORT
  );
  const delayString = await fetch(
    "https://script.google.com/macros/s/AKfycbyKxch7z7l8e07LXulRHqxjVoIiB13kcgvoToLE-rqlxLmLSKdlmqz0FI1F2EuA7Zfg/exec"
  )
    .then((response) => response.text())
    .then((data) => {
      if (data !== "") {
        return data.split("^");
      }
      return null;
    });
  ToastAndroid.show(`${delayString}`, ToastAndroid.SHORT);
  switch (props.widgetAction) {
    case "WIDGET_ADDED":
    case "WIDGET_UPDATE":
    // Not needed for now
    case "WIDGET_CLICK":
    // Not needed for now
    case "WIDGET_RESIZED":
      // Not needed for now
      props.renderWidget(<Widget time={time} delayString={delayString} />);

      break;

      break;

      break;

    case "WIDGET_DELETED":
      // Not needed for now
      break;

      break;

    default:
      break;
  }
}
