import React, { useState } from "react";
import { Helmet } from "react-helmet";
import { useWizard, Wizard } from 'react-use-wizard';

import WelcomePage from "./steps/welcome-page";
import ServerName from "./steps/server-name";
import AdminAccount from "./steps/admin-account";
import WhoCanSignUp from "./steps/who-can-sign-up";
import InviteLink from "./steps/invite-link";
import DonePage from "./steps/done-page";
import steps from "./steps";
import { useTranslation } from "react-i18next";
import clsx from "clsx";


const Navigator = () => {
  const { activeStep, goToStep } = useWizard();
  const canJumpTo = steps[activeStep]?.canJumpTo || [];
  console.log("active step", activeStep);

  return (
    <div className="absolute top-5 w-full flex justify-center gap-2 z-10">
      {steps.map((stepToRender, indexToRender) => {
        const clickable = canJumpTo.includes(stepToRender.name);
        const itemClass = clsx(`text-sm text-gray-600`, clickable && "cursor-pointer hover:text-gray-500", indexToRender === activeStep && "font-bold text-black", indexToRender >= activeStep && "text-gray-400");
        const nodeCls = `${itemClass}`;
        return (
          <React.Fragment key={indexToRender}>
            <span
              className={nodeCls}
              onClick={() => {
                if (clickable) {
                  goToStep(indexToRender);
                }
              }}
            >
              {stepToRender.label}
            </span>
            {indexToRender !== steps.length - 1 && <span className={nodeCls}>→</span>}
          </React.Fragment>
        );
      })}
    </div>
  );
};

export default function OnboardingPage() {
  const { t } = useTranslation("welcome");
  const [serverName, setServerName] = useState("");
  return (
    <>
      <Helmet>
        <title>{t("onboarding.title")}</title>
      </Helmet>
      <div className="h-screen overflow-y-auto">
        <Wizard header={<Navigator />}>
          <WelcomePage />
          <ServerName serverName={serverName} setServerName={setServerName} />
          <AdminAccount serverName={serverName} />
          <WhoCanSignUp />
          {/* lazy call invite link API  */}
          <InviteLink />
          <DonePage serverName={serverName} />
        </Wizard>
      </div>
    </>
  );
}
