"use client";
import { Link } from "@/components/navigation";
import {
  Carousel,
  CarouselContent,
  CarouselItem,
  CarouselNext,
  CarouselPrevious,
} from "@/components/ui/carousel";
import { listCuratedContent } from "@/service/curated-content/curated-content";
import { getListContent } from "@/service/landing/landing";
import {
  formatDateToIndonesian,
  generateLocalizedPath,
  textEllipsis,
} from "@/utils/globals";
import { Icon } from "@iconify/react/dist/iconify.js";
import { useParams, usePathname, useRouter } from "next/navigation";
import React, { Component, useEffect, useState } from "react";

const AudioSliderPage = () => {
  const [audioData, setAudioData] = useState<any>();
  const [displayAudio, setDisplayAudio] = useState<any[]>([]);
  const [page, setPage] = useState(1);
  const [limit, setLimit] = React.useState(10);
  const [search, setSearch] = React.useState("");

  useEffect(() => {
    initFetch();
  }, [page, limit, search]);

  useEffect(() => {
    if (audioData?.length > 0) {
      shuffleAndSetVideos();
      const interval = setInterval(shuffleAndSetVideos, 5000);
      return () => clearInterval(interval); // Cleanup interval on unmount
    }
  }, [audioData]);

  const initFetch = async () => {
    const response = await listCuratedContent(search, limit, page - 1, 4, "1");
    console.log(response);

    const data = response?.data?.data;
    const contentData = data?.content;
    setAudioData(contentData);
  };

  const shuffleAndSetVideos = () => {
    const shuffled = shuffleArray([...audioData]);
    setDisplayAudio(shuffled.slice(0, 3));
  };

  const shuffleArray = (array: any[]) => {
    for (let i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
  };

  return (
    <div className="mx-3 px-5">
      <div className=" grid grid-cols-1 gap-6 ">
        {displayAudio?.map((audio: any) => (
          <Link
            href={`/shared/curated-content//giat-routine/audio/detail/${audio.id}`}
            key={audio?.id}
            className="flex flex-col sm:flex-row items-center hover:scale-110 transition-transform duration-300 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-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" /> 518
              </div>
              <div className="font-semibold text-gray-900 dark:text-white mt-1 text-sm">
                {audio?.title}
              </div>
            </div>
            <div className="flex items-center justify-center gap-3">
              <div className="mt-2">
                <img src="/assets/wave.svg" className="w-80" />
              </div>
              <div className="flex flex-row items-center justify-center text-gray-500 dark:text-gray-400">
                <img
                  src="/assets/audio-icon.png"
                  alt="#"
                  className="flex items-center justify-center"
                />
                <div className="flex mx-2 items-center justify-center">
                  {audio?.duration}
                </div>
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  width="20"
                  height="20"
                  viewBox="0 0 20 20"
                >
                  <path
                    fill="#f00"
                    d="M7.707 10.293a1 1 0 1 0-1.414 1.414l3 3a1 1 0 0 0 1.414 0l3-3a1 1 0 0 0-1.414-1.414L11 11.586V6h5a2 2 0 0 1 2 2v7a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h5v5.586zM9 4a1 1 0 0 1 2 0v2H9z"
                  />
                </svg>
              </div>
            </div>
          </Link>
        ))}
      </div>
    </div>
  );
};

export default AudioSliderPage;
