/**
 * Copyright (c) 2023-present Pi Dash Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import { useState } from "react";
import { ChevronDown } from "lucide-react";
import { observer } from "mobx-react";
import { useTheme } from "next-themes";
// pi dash imports
import { useTranslation } from "@pi-dash/i18n";
import { Button } from "@pi-dash/propel/button";
// assets
import darkActivityAsset from "@/app/assets/empty-state/profile/activity-dark.webp?url";
import lightActivityAsset from "@/app/assets/empty-state/profile/activity-light.webp?url";
// components
import { DetailedEmptyState } from "@/components/empty-state/detailed-empty-state-root";
import { ProfileSettingsHeading } from "@/components/settings/profile/heading";
// local imports
import { ActivityProfileSettingsList } from "./activity-list";

const PER_PAGE = 100;

export const ActivityProfileSettings = observer(function ActivityProfileSettings() {
  // states
  const [pageCount, setPageCount] = useState(1);
  const [totalPages, setTotalPages] = useState(0);
  const [resultsCount, setResultsCount] = useState(0);
  const [isEmpty, setIsEmpty] = useState(false);
  // theme hook
  const { resolvedTheme } = useTheme();
  // pi dash hooks
  const { t } = useTranslation();
  // derived values
  const resolvedPath = resolvedTheme === "light" ? lightActivityAsset : darkActivityAsset;

  const updateTotalPages = (count: number) => setTotalPages(count);

  const updateResultsCount = (count: number) => setResultsCount(count);

  const updateEmptyState = (isEmpty: boolean) => setIsEmpty(isEmpty);

  const handleLoadMore = () => setPageCount((prev) => prev + 1);

  const activityPages: React.ReactNode[] = [];
  for (let i = 0; i < pageCount; i++)
    activityPages.push(
      <ActivityProfileSettingsList
        key={i}
        cursor={`${PER_PAGE}:${i}:0`}
        perPage={PER_PAGE}
        updateResultsCount={updateResultsCount}
        updateTotalPages={updateTotalPages}
        updateEmptyState={updateEmptyState}
      />
    );

  const isLoadMoreVisible = pageCount < totalPages && resultsCount !== 0;

  if (isEmpty) {
    return (
      <div className="flex size-full flex-col gap-y-7">
        <ProfileSettingsHeading
          title={t("Activity")}
          description={t("Track your recent actions and changes across all projects and work items.")}
        />
        <DetailedEmptyState
          title={""}
          description={""}
          assetPath={resolvedPath}
          className="mx-auto min-h-fit w-full justify-center p-0!"
          size="base"
        />
      </div>
    );
  }

  return (
    <div className="size-full">
      <ProfileSettingsHeading
        title={t("Activity")}
        description={t("Track your recent actions and changes across all projects and work items.")}
      />
      <div className="mt-7 w-full">{activityPages}</div>
      {isLoadMoreVisible && (
        <div className="mt-4 flex w-full items-center justify-center">
          <Button variant="ghost" onClick={handleLoadMore} appendIcon={<ChevronDown />}>
            {t("Load more")}
          </Button>
        </div>
      )}
    </div>
  );
});
