import React, { useEffect, useRef } from "react";

import Typography from "@mui/material/Typography";

import { Player } from "@player";
import { AugmentationName } from "@enums";

import { GeneralDev } from "./DevMenu/ui/GeneralDev";
import { TimeSkipDev } from "./DevMenu/ui/TimeSkipDev";

import { StatsDev } from "./DevMenu/ui/StatsDev";
import { FactionsDev } from "./DevMenu/ui/FactionsDev";
import { AugmentationsDev } from "./DevMenu/ui/AugmentationsDev";
import { SourceFilesDev } from "./DevMenu/ui/SourceFilesDev";
import { ProgramsDev } from "./DevMenu/ui/ProgramsDev";
import { ServersDev } from "./DevMenu/ui/ServersDev";
import { CompaniesDev } from "./DevMenu/ui/CompaniesDev";
import { BladeburnerDev } from "./DevMenu/ui/BladeburnerDev";
import { GangDev } from "./DevMenu/ui/GangDev";
import { CorporationDev } from "./DevMenu/ui/CorporationDev";
import { CodingContractsDev } from "./DevMenu/ui/CodingContractsDev";
import { StockMarketDev } from "./DevMenu/ui/StockMarketDev";
import { SleevesDev } from "./DevMenu/ui/SleevesDev";
import { StanekDev } from "./DevMenu/ui/StanekDev";
import { AchievementsDev } from "./DevMenu/ui/AchievementsDev";
import { EntropyDev } from "./DevMenu/ui/EntropyDev";

import { Exploit } from "./Exploits/Exploit";
import { useRerender } from "./ui/React/hooks";
import { DarknetDev } from "./DevMenu/ui/DarknetDev";
import { AutoExpandContext, getAutoExpandData, setAutoExpandData } from "./ui/AutoExpand/AutoExpandContext";
import { canAccessStockMarket } from "./StockMarket/StockMarket";

export function DevMenuRoot(): React.ReactElement {
  const autoExpandContextValue = useRef({
    data: getAutoExpandData(),
    set: (key: string, expanded: boolean) => {
      autoExpandContextValue.current.data[key] = expanded;
      setAutoExpandData(autoExpandContextValue.current.data);
    },
  });

  useEffect(() => {
    Player.giveExploit(Exploit.YoureNotMeantToAccessThis);
  }, []);

  // Pass rerender to certain subpages in case certain tabs are now valid/invalid due to changes made on those pages
  // Rerender periodically in case game state changes (e.g. player starts gang or buys wse account through a script)
  const rerender = useRerender(400);
  return (
    <AutoExpandContext.Provider value={autoExpandContextValue.current}>
      <Typography>Development Menu - Only meant to be used for testing/debugging</Typography>
      <GeneralDev parentRerender={rerender} />
      <StatsDev />
      <FactionsDev />
      <AugmentationsDev />
      <SourceFilesDev parentRerender={rerender} />
      <ProgramsDev />
      <ServersDev />
      <CompaniesDev />

      {Player.bladeburner && <BladeburnerDev bladeburner={Player.bladeburner} />}

      {Player.gang && <GangDev />}

      {Player.corporation && <CorporationDev />}

      <CodingContractsDev />

      {canAccessStockMarket() && <StockMarketDev />}

      {Player.sleeves.length > 0 && <SleevesDev />}
      {Player.augmentations.some((aug) => aug.name === AugmentationName.StaneksGift1) && <StanekDev />}

      <TimeSkipDev />
      <AchievementsDev />
      <EntropyDev />
      <DarknetDev />
    </AutoExpandContext.Provider>
  );
}
