import * as React from "react";
import { useEffect } from "@theia/core/shared/react";
import { OpenFileDialogProps } from "@theia/filesystem/lib/browser";
import URI from "@theia/core/lib/common/uri";
import { EScreenView, LocalStorageKey, RCloneEndPoints } from "../../constants";
import fetchRClone from "../../helpers/fetchRClone";
import AccessGrantScreen from "./pages/AccessGrantScreen";
import ActionsScreen from "./pages/ActionsScreen";
import RemoteListScreen from "./pages/RemoteListScreen";
import SyncActionScreen from "./pages/SyncActionScreen";

import { useStorjWidgetContext } from "./StorjWidget.context";
import { useCommonStyles } from "./StorjWidget.style";
import JobStatusScreen from "./pages/JobStatusScreen";
import JobListScreen from "./pages/JobListScreen";
import CopyActionScreen from "./pages/CopyActionScreen";

type Props = {
  onShowFileDialog: (options: OpenFileDialogProps) => Promise<URI | undefined>;
  onShowAlert: (message: string) => void;
  getIsPathFile: (path: string) => Promise<boolean>;
};
const StorjWidget = (props: Props) => {
  const { currentScreen, setCurrentScreen } = useStorjWidgetContext();
  const { onShowFileDialog, onShowAlert, getIsPathFile } = props;
  const classes = useCommonStyles();
  useEffect(() => {
    getFsInfo();
  }, []);
  const getFsInfo = async () => {
    const selectedRemoteName = localStorage.getItem(
      LocalStorageKey.SELECTED_REMOTE
    );
    const options = {
      body: JSON.stringify({}),
    };
    try {
      const response = await fetchRClone(RCloneEndPoints.listRemotes, options);
      const json = await response.json();

      if (json.remotes?.length > 0) {
        if (!selectedRemoteName) {
          localStorage.setItem(
            LocalStorageKey.SELECTED_REMOTE,
            json.remotes[0]
          );
        }
        setCurrentScreen(EScreenView.ACTIONS_SCREEN);
      } else {
        throw new Error();
      }
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <div className={classes.container}>
      {currentScreen === EScreenView.ACCESS_GRANT_SCREEN && (
        <AccessGrantScreen />
      )}
      {currentScreen === EScreenView.ACTIONS_SCREEN && (
        <ActionsScreen onShowAlert={onShowAlert} />
      )}
      {currentScreen === EScreenView.ACTIONS_SYNC_SCREEN && (
        <SyncActionScreen
          onShowFileDialog={onShowFileDialog}
          onShowAlert={onShowAlert}
          getIsPathFile={getIsPathFile}
        />
      )}
      {currentScreen === EScreenView.ACTIONS_COPY_SCREEN && (
        <CopyActionScreen
          getIsPathFile={getIsPathFile}
          onShowFileDialog={onShowFileDialog}
          onShowAlert={onShowAlert}
        />
      )}
      {currentScreen === EScreenView.REMOTE_LIST_SCREEN && (
        <RemoteListScreen onShowAlert={onShowAlert} />
      )}
      {currentScreen === EScreenView.JOB_STATUS_SCREEN && <JobStatusScreen />}
      {currentScreen === EScreenView.JOB_LIST_SCREEN && <JobListScreen />}
    </div>
  );
};

export default StorjWidget;
