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";
import { widgetLightColors, widgetDarkColors } from "./widget-theme";
import { AS } from "../../storageControl";
import { STORAGE_KEYS } from "../../constants";

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,
};

type ColorThemePref = "light" | "system" | "dark";

async function getColorThemePref(): Promise<ColorThemePref> {
  try {
    const val = await AS.getItem(STORAGE_KEYS.COLOR_THEME);
    if (val === "dark" || val === "light" || val === "system") return val;
  } catch {}
  return "system";
}

/** テーマ設定に応じてrenderWidgetに渡す値を組み立てる */
function renderWithTheme(
  renderWidget: (w: React.JSX.Element | { light: React.JSX.Element; dark: React.JSX.Element }) => void,
  theme: ColorThemePref,
  buildWidget: (isDark: boolean) => React.JSX.Element,
) {
  if (theme === "dark") {
    renderWidget(buildWidget(true));
  } else if (theme === "light") {
    renderWidget(buildWidget(false));
  } else {
    // system: 両方渡してAndroidが自動切替
    renderWidget({
      light: buildWidget(false),
      dark: buildWidget(true),
    });
  }
}

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;
      const theme = await getColorThemePref();

      if (name === "JR_shikoku_felica_balance") {
        const quickData = await getFelicaQuickAccessData();
        renderWithTheme(renderWidget, theme, (isDark) => (
          <FelicaQuickAccessWidget {...quickData} colors={isDark ? widgetDarkColors : widgetLightColors} />
        ));
        break;
      }

      if (name === "JR_shikoku_apps_shortcut") {
        const data = await getShortcutData();
        renderWithTheme(renderWidget, theme, (isDark) => (
          <ShortcutWidget
            {...data}
            widgetWidth={widgetInfo.width}
            widgetHeight={widgetInfo.height}
            colors={isDark ? widgetDarkColors : widgetLightColors}
          />
        ));
        break;
      }

      if (name === "JR_shikoku_train_strange") {
        const data = getStrangeTrainData();
        renderWithTheme(renderWidget, theme, (isDark) => (
          <StrangeTrainWidget {...data} colors={isDark ? widgetDarkColors : widgetLightColors} />
        ));
        break;
      }

      if (name === "JR_shikoku_info") {
        const { time, text } = await getInfoString();
        renderWithTheme(renderWidget, theme, (isDark) => (
          <InfoWidget time={time} text={text && text.toString()} colors={isDark ? widgetDarkColors : widgetLightColors} />
        ));
        break;
      }

      // JR_shikoku_train_info and default
      {
        const { time, delayString } = await getDelayData();
        renderWithTheme(renderWidget, theme, (isDark) => (
          <TraInfoEXWidget time={time} delayString={delayString} colors={isDark ? widgetDarkColors : widgetLightColors} />
        ));
      }
      break;
    }

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