import React from "react";
import { PmBoardsPage } from "../PmBoardsPage";
import { PmDashboardPage } from "../PmDashboardPage";
import PmSingleBoard from "../PmSingleBoard";
import { PmWorkspaceMembers } from "../PmWorkspaceMembers";
import { GlobalContext } from "@/context/Global";
import { PmWorkspaceBoards } from "../PmWorkspaceBoards";

const DashboardViewScreen = ({
  children,
  workspacesList,
  activeWorkspaceId,
  setActiveWorkspaceId,
  activeLeftPanel,
  setActiveLeftPanel,
  activeBoardId,
  setActiveBoardId,
}) => {
  const {
    state: { path },
    dispatch: globalDispatch,
  } = React.useContext(GlobalContext);

  function handleWorkspaceListCardNavClick(id, path) {
    setActiveWorkspaceId(id);
    setActiveLeftPanel("workspace");
    globalDispatch({
      type: "SETPATH",
      payload: {
        path,
      },
    });
  }

  function handleWorkspaceBoardClick(boardId) {
    setActiveBoardId(boardId);
    setActiveLeftPanel("hidden");
    globalDispatch({
      type: "SETPATH",
      payload: {
        path: "workspaceBoards",
      },
    });
  }

  function handleBackButton() {
    setActiveLeftPanel("default");
    globalDispatch({
      type: "SETPATH",
      payload: {
        path: "home",
      },
    });
  }

  function renderRightView(activeState) {
    switch (activeState) {
      case "boards":
        return (
          <PmBoardsPage
            workspacesList={workspacesList}
            handleWorkspaceListCardNavClick={handleWorkspaceListCardNavClick}
            handleWorkspaceBoardClick={handleWorkspaceBoardClick}
          />
        );

      case "home":
        return <PmDashboardPage />;

      case "workspaceBoards":
        if (activeLeftPanel === "hidden") {
          return (
            <PmSingleBoard
              activeBoardId={activeBoardId}
              handleBackButton={handleBackButton}
            />
          );
        } else
          return (
            <PmWorkspaceBoards
              activeWorkspaceId={activeWorkspaceId}
              handleWorkspaceBoardClick={handleWorkspaceBoardClick}
            />
          );

      case "workspaceMembers":
        return <PmWorkspaceMembers activeWorkspaceId={activeWorkspaceId} />;

      default:
        return <div></div>;
    }
  }
  return (
    <div
      className={`${activeLeftPanel !== "hidden"
          ? "rounded-md shadow-[0_0_2px_2px_#c3c9ff5e]"
          : ""
        }  h-screen py-4 text-xl`}
    >
      {children}
      {renderRightView(path)}
    </div>
  );
};
export default DashboardViewScreen;
