"use client";
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from "@/components/ui/carousel";
import { close, loading } from "@/config/swal";
import { Link, usePathname } from "@/i18n/routing";
import { listData } from "@/service/landing/landing";
import { formatDateToIndonesian, getOnlyMonthAndYear } from "@/utils/globals";
import { Icon } from "@iconify/react/dist/iconify.js";
import { pages } from "next/dist/build/templates/app-page";
import { useParams, useSearchParams } from "next/navigation";
import React, { useEffect } from "react";
import { useState } from "react";

export default function FilterAudioComponent(props: {
  categoryFilter?: any;
  sortByOpt?: string;
  startDateString?: string;
  endDateString?: string;
  monthYearFilter?: any;
}) {
  const {
    categoryFilter,
    sortByOpt,
    startDateString,
    endDateString,
    monthYearFilter,
  } = props;
  const [newContent, setNewContent] = useState<any>();
  const asPath = usePathname();
  const params = useParams();
  const searchParams = useSearchParams();
  const [imageData, setImageData] = useState<any>();
  const [totalData, setTotalData] = useState<number>(1);
  const [totalPage, setTotalPage] = useState<number>(1);
  const sortBy = searchParams?.get("sortBy");
  const title = searchParams?.get("title");
  const categorie = searchParams?.get("category");
  const group = searchParams?.get("group");
  const [totalContent, setTotalContent] = useState();

  useEffect(() => {
    getDataAll();
  }, [
    title,
    categoryFilter,
    categorie,
    group,
    startDateString,
    endDateString,
    monthYearFilter,
    sortByOpt,
  ]);

  async function getDataAll() {
    if (asPath?.includes("/polda/") == true) {
      if (asPath?.split("/")[2] !== "[polda_name]") {
        const filter =
          categoryFilter?.length > 0
            ? categoryFilter?.sort().join(",")
            : categorie || "";

        const name = title == undefined ? "" : title;
        const filterGroup = group == undefined ? asPath.split("/")[2] : group;
        loading();
        const response = await listData(
          "4",
          name,
          filter,
          12,
          0,
          sortByOpt,
          "",
          "",
          filterGroup,
          startDateString,
          endDateString,
          monthYearFilter
            ? getOnlyMonthAndYear(monthYearFilter)
                ?.split("/")[0]
                ?.replace("", "")
            : "",
          monthYearFilter
            ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[1]
            : ""
        );
        close();
        const data = response?.data?.data;
        const contentData = data?.content;
        setNewContent(contentData);
        setTotalData(data?.totalElements);
        setTotalPage(data?.totalPages);
        setTotalContent(response?.data?.data?.totalElements);
      }
    } else {
      const filter =
        categoryFilter?.length > 0
          ? categoryFilter?.sort().join(",")
          : categorie || "";

      const name = title == undefined ? "" : title;
      loading();
      const response = await listData(
        "4",
        name,
        filter,
        12,
        0,
        sortByOpt,
        "",
        "",
        "",
        startDateString,
        endDateString,
        monthYearFilter
          ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[0]?.replace("", "")
          : "",
        monthYearFilter
          ? getOnlyMonthAndYear(monthYearFilter)?.split("/")[1]
          : ""
      );
      close();

      const data = response?.data?.data;
      const contentData = data?.content;

      setNewContent(contentData);
      setTotalData(data?.totalElements);
      setTotalPage(data?.totalPages);
      setTotalContent(response?.data?.data?.totalElements);
    }
  }

  return newContent?.length > 0 ? (
    <div className="flex flex-col gap-3">
      <p>{`Audio(${totalContent})`}</p>
      <Carousel className="w-full max-w-7xl mx-auto">
        <CarouselContent>
          {newContent?.map((audio: any) => (
            <CarouselItem key={audio?.id} className="md:basis-1/2 lg:basis-1/3">
              <div className="flex flex-row gap-6">
                <Link
                  href={`/audio/detail/${audio?.slug}`}
                  className="flex flex-col sm:flex-row items-center bg-white dark:bg-gray-800 cursor-pointer shadow-md rounded-lg p-4 gap-4 w-full"
                >
                  <div className="flex items-center justify-center bg-red-500 text-white rounded-lg w-16 h-8 lg:h-16">
                    <svg
                      width="32"
                      height="34"
                      viewBox="0 0 32 34"
                      fill="null"
                      xmlns="http://www.w3.org/2000/svg"
                    >
                      <path
                        d="M23.404 0.452014C23.7033 0.35857 24.0204 0.336816 24.3297 0.388509C24.639 0.440203 24.9318 0.563895 25.1845 0.749599C25.4371 0.935304 25.6426 1.17782 25.7843 1.45756C25.9259 1.73731 25.9998 2.04644 26 2.36001V14.414C25.3462 14.2296 24.6766 14.1064 24 14.046V8.36001L10 12.736V27C10 28.1264 9.6197 29.2197 8.92071 30.1029C8.22172 30.9861 7.24499 31.6075 6.14877 31.8663C5.05255 32.125 3.90107 32.0061 2.88089 31.5287C1.86071 31.0514 1.03159 30.2435 0.52787 29.2361C0.024152 28.2286 -0.124656 27.0806 0.105556 25.9781C0.335768 24.8755 0.931513 23.883 1.79627 23.1613C2.66102 22.4396 3.74413 22.031 4.87009 22.0017C5.99606 21.9724 7.09893 22.3242 8.00001 23V6.73601C7.99982 6.30956 8.13596 5.8942 8.38854 5.55059C8.64112 5.20698 8.99692 4.9531 9.40401 4.82601L23.404 0.452014ZM10 10.64L24 6.26601V2.36001L10 6.73601V10.64ZM5.00001 24C4.20436 24 3.44129 24.3161 2.87869 24.8787C2.31608 25.4413 2.00001 26.2044 2.00001 27C2.00001 27.7957 2.31608 28.5587 2.87869 29.1213C3.44129 29.6839 4.20436 30 5.00001 30C5.79566 30 6.55872 29.6839 7.12133 29.1213C7.68394 28.5587 8.00001 27.7957 8.00001 27C8.00001 26.2044 7.68394 25.4413 7.12133 24.8787C6.55872 24.3161 5.79566 24 5.00001 24ZM32 25C32 27.387 31.0518 29.6761 29.364 31.364C27.6761 33.0518 25.387 34 23 34C20.6131 34 18.3239 33.0518 16.636 31.364C14.9482 29.6761 14 27.387 14 25C14 22.6131 14.9482 20.3239 16.636 18.6361C18.3239 16.9482 20.6131 16 23 16C25.387 16 27.6761 16.9482 29.364 18.6361C31.0518 20.3239 32 22.6131 32 25ZM27.47 24.128L21.482 20.828C21.3298 20.7443 21.1583 20.7016 20.9846 20.7043C20.8108 20.707 20.6408 20.7549 20.4912 20.8433C20.3416 20.9317 20.2176 21.0576 20.1315 21.2086C20.0453 21.3595 20 21.5302 20 21.704V28.304C20 28.4778 20.0453 28.6486 20.1315 28.7995C20.2176 28.9504 20.3416 29.0763 20.4912 29.1647C20.6408 29.2531 20.8108 29.301 20.9846 29.3037C21.1583 29.3064 21.3298 29.2638 21.482 29.18L27.47 25.88C27.6268 25.7937 27.7575 25.6669 27.8486 25.5128C27.9397 25.3587 27.9877 25.183 27.9877 25.004C27.9877 24.825 27.9397 24.6493 27.8486 24.4952C27.7575 24.3412 27.6268 24.2143 27.47 24.128Z"
                        fill="white"
                      />
                    </svg>
                  </div>

                  <div className="flex flex-col  flex-1">
                    <div className="text-gray-500 dark:text-gray-400 flex flex-row text-sm">
                      {formatDateToIndonesian(new Date(audio?.createdAt))}{" "}
                      {audio?.timezone ? audio?.timezone : "WIB"} |{" "}
                      <Icon icon="formkit:eye" width="15" height="15" />{" "}
                      {audio?.clickCount}{" "}
                    </div>
                    <div className="font-semibold text-gray-900 dark:text-white mt-1 text-sm h-5 hover:h-auto truncate hover:whitespace-normal hover:overflow-visible">
                      {audio?.title}
                    </div>
                  </div>
                </Link>
              </div>
            </CarouselItem>
          ))}
        </CarouselContent>
        <CarouselPrevious />
        <CarouselNext />
      </Carousel>
    </div>
  ) : (
    ""
  );
}
