"use client";

import Image from "next/image";
import { HOME_CONTENT_WIDTH_CLASS } from "./homeCarouselLayout";
import { useTranslation } from "react-i18next";
import CountUp from "@/components/ui/CountUp";
import { parseLocalizedNumber } from "@/lib/digits";
import { type HomeStatistics } from "@/features/shared";

/**
 * The slider dots and the three platform-statistics cards that overlap the
 * banner from below.
 *
 * The dots live here rather than with the slider because they are positioned
 * inside this block's container, not the banner's.
 */
export default function BannerStats({
  statistics,
  slideCount,
  currentImageIndex,
  setCurrentImageIndex,
  showStatistics = true,
  standalone = false,
  overlapBanner = false,
}: {
  statistics: HomeStatistics;
  slideCount: number;
  currentImageIndex: number;
  setCurrentImageIndex: (index: number) => void;
  showStatistics?: boolean;
  standalone?: boolean;
  overlapBanner?: boolean;
}) {
  const { t } = useTranslation();

  /**
   * `parseLocalizedNumber` rather than the raw field: the public homepage is
   * server-rendered through `lib/api/server.ts`, which pins the language header
   * to English and so receives real numbers, while the authenticated homepage
   * reads the same payload over axios in the user's language and receives
   * Arabic-Indic digit *strings*. Both shapes have to reach CountUp as a number.
   */
  const cards = [
    {
      img: "/assets/homepage/volunteer_register.svg",
      number: parseLocalizedNumber(statistics.volunteer_count),
      text: t("COMMON.VOLUNTEER--"),
      borderColor: "border-primary-200",
      textColor: "text-primary-200",
    },
    {
      img: "/assets/homepage/team.svg",
      number: parseLocalizedNumber(statistics.volunteer_team_count),
      text: t("COMMON.VOLUNTEER.TEAM"),
      borderColor: "border-primary-300",
      textColor: "text-primary-300",
    },
    {
      img: "/assets/homepage/organizations.svg",
      number: parseLocalizedNumber(statistics.organization_count),
      text: t("COMMON.ORGANIZATIONS"),
      borderColor: "border-primary-100",
      textColor: "text-primary-100",
    },
  ];

  return (
    <div className={`${standalone ? overlapBanner ? "relative z-20 -mt-16 mb-8 mobilescreen:-mt-10" : "my-8" : "relative top-[-70px] mobilescreen:top-[-50px]"} ${HOME_CONTENT_WIDTH_CLASS} mx-auto`}>
      <div className="mb-2">
        {slideCount > 1 && (
          <div className="flex justify-center py-1">
            {Array.from({ length: slideCount }).map((_, index) => (
              <button
                key={index}
                type="button"
                onClick={() => setCurrentImageIndex(index)}
                className={`w-3 h-3 rounded-full mx-1 ${
                  index === currentImageIndex
                    ? "bg-[#29246D]"
                    : "bg-[#FFFFFF4D]"
                } shadow-md`}
                aria-label={`${t("COMMON.GO_TO_IMAGE")} ${index + 1}`}
                aria-pressed={index === currentImageIndex}
              />
            ))}
          </div>
        )}
      </div>
      {showStatistics && <div className="grid 2xl:px-5 px-3 mobilescreen:px-[13px] grid-cols-3 md:grid-cols-3 lg:grid-cols-3 2xl:gap-[67px] lg:gap-[40px] md:gap-[20px] mobilescreen:gap-[10px]">
        {cards.map((item, index) => (
          <div
            key={index}
            className={`bg-white xs:pb-5 lg:p-3 xss:pb-6 md:p-3 sm:p-6 xss:p-3 p-3 rounded-lg shadow-[0px_4px_4px_0px_rgba(0,0,0,0.25)] border-b-[7px] ${item.borderColor} text-center rounded-tl-none rounded-tr-[40px] xss:rounded-tr-[20px] rounded-bl-[40px] xss:rounded-bl-[20px] rounded-br-[40px] xss:rounded-br-[20px] flex flex-col items-center justify-center h-full`}
          >
            <div className="xss:h-[45px]">
              <Image
                src={item.img}
                alt=""
                width={75}
                height={75}
                aria-hidden="true"
                className="w-[75px] 2xl:h-[75px] lg:h-[40px] mobilescreen:w-[46px] xs:w-[40px] xs:h-[40px] mobilescreen:h-[41px] object-contain"
                unoptimized
              />
            </div>
            <p
              className={`lg:text-[40px] xss:text-base md:text-[40px] sm:text-[36px] font-bold ${item.textColor} mt-2`}
            >
              <CountUp value={item.number} />
            </p>
            <p className="xss:h-5 xss:mb-2 text-secondary-102 extrasmall:text-xs 2xl:text-[22px] lg:text-[20px] sm:text-[18px] md:text-[22px] xss:text-[12px] font-semibold pt-3 xss:pt-0 text-center">
              {item.text}
            </p>
          </div>
        ))}
      </div>}
    </div>
  );
}
