import { useEffect } from "react";
import { useStore } from "./store/root";
import { messageHandler } from "./utils/webviewMessageHandler";
// import OptimizeCurrentDiff from "./components/optimizeCurrentDiff";
// import CurrentFileFunctions from "./components/currentFileFunctions";
import { vscode } from "./utils/vscode";
import ChatView from "./components/chatView";
import Tabs from "./components/tabs";
import OptimizationQueue from "./components/optimizationQueue";
import DemoOptimization from "./components/demoOptimization";
import SignInForm from "./components/signInForm";
import InitForm from "./components/initForm";

function App() {
  const store = useStore();
  useEffect(() => {
    vscode.postMessage({
      type: "webviewReady",
    });

    const handler = (e: MessageEvent) => messageHandler(e, store);
    window.addEventListener("message", handler);

    return () => {
      window.removeEventListener("message", handler);
    };
  }, []);

  if (store.status == "apiKeyError") {
    return <SignInForm />;
  }

  if (store.activeTab === "projectConfig") {
    return (
      <div className="app-container">
        <InitForm
          suggestions={store.projectConfigSuggestions ?? {}}
          pyprojectPath={store.configPath ?? ""}
          errorDetails=""
        />
      </div>
    );
  }

  // https://cdn.displate.com/artwork/270x380/2024-11-23/60ffe7e4-4185-4ac8-9081-5268ba425a85.jpg
  const firstTime = !store.triedCodeflashDemo;

  return (
    <div className="app-container">
      {firstTime ? (
        <>
          <div className="gridBackground" />
          <DemoOptimization />
        </>
      ) : (
        <>
          <Tabs />
          <div className="content">
            {store.activeTab === "optimization" && <ChatView />}
            {store.activeTab === "tasks" && (
              <OptimizationQueue queueTasks={store.queueTasks} />
            )}
          </div>
        </>
      )}
    </div>
  );
}

//  <OptimizeCurrentDiff />
//  {store.functionsInCurrentFile.length > 0 && <CurrentFileFunctions />}

export default App;
