"use client";
import React from "react";
import Table from "@/components/ContextComponents/Commons/Table";
import IsNotAllowedButton from "@/components/ContextComponents/Commons/isNotAllowedButton";
import IsNotAllowed from "@/components/ContextComponents/Commons/PageisNotAllowed";
import DeleteButton from "@/components/ContextComponents/Commons/ButtonDelete";
import UpdateButton from "@/components/ContextComponents/Commons/ButtonUpdate";
import CreateButton from "@/components/ContextComponents/Commons/ButtonCreate";
import { retrieveAvailableEvents } from "@/(apicalls)/cookies/token";
import {
  retrieveBuildList,
  updateBuild,
  createBuild,
} from "@/(apicalls)/building/build";
import CreatePage from "@/components/ContextComponents/Commons/PageCreate";
import DashPage from "@/components/ContextComponents/Commons/DashPage";
import PageUpdate from "@/components/ContextComponents/Commons/PageUpdate";

const Build: React.FC = () => {
  const [formPage, setFormPage]: [any, any] = React.useState(null);
  const [endpointNeedsList, setEndpointNeedsList] = React.useState({});

  const endpointNeeds = {
    table: {
      endpoint: "/building/build/list",
      component: (
        <Table
          createEndpoint="/building/build/list"
          updateEndpoint="/building/build/update"
          tableFunction={retrieveBuildList}
          UpdatePage={PageUpdate}
          saveFunction={createBuild}
          setFormPage={setFormPage}
          returnToPage={() => setFormPage(null)}
          updatePageInfo={{
            title: "Bina Güncelle",
            description: "Bina güncelleme sayfasına hoş geldiniz",
          }}
        />
      ),
      notAllowed: <IsNotAllowed buttonKey="list-is-not-allowed" />,
    },
    create: {
      endpoint: "/building/build/create",
      component: (
        <CreateButton
          buttonLabel="Yeni Bina ekle"
          setFormPage={() =>
            setFormPage(
              <CreatePage
                pageInfo={{
                  title: "Bina Oluştur",
                  description: "Bina oluştur sayfasına hoş geldiniz",
                }}
                endpoint="/building/build/create"
                returnToPage={() => setFormPage(null)}
                saveFunction={createBuild}
              />
            )
          }
        />
      ),
      notAllowed: (
        <IsNotAllowedButton
          buttonKey="create-is-not-allowed"
          label="Bina Oluştur"
        />
      ),
    },
    delete: {
      endpoint: "/building/build/delete",
      component: <DeleteButton onClick={() => () => console.log("Hi")} />,
      notAllowed: (
        <IsNotAllowedButton
          buttonKey="delete-is-not-allowed"
          label="Binayı Sil"
        />
      ),
    },
  };

  React.useEffect(() => {
    retrieveAvailableEvents()
      .then((data) => {
        for (const [key, component] of Object.entries(endpointNeeds)) {
          if (data?.availableEvents.includes(component.endpoint)) {
            setEndpointNeedsList((prev) => ({
              ...prev,
              [key]: component.component,
            }));
          }
        }
      })
      .catch(() => {});
  }, [formPage]);

  return (
    <>{formPage ? formPage : <DashPage endpointNeeds={endpointNeedsList} />}</>
  );
};

export default Build;
