"use client";
import Link from "next/link";
import PersianNumber from "plugins/PersianNumber";
import { useEffect, useState } from "react";

const TimerDown = () => {
  const targetTimestamp = 1710654443 * 1000; // Convert to milliseconds

  const [timeRemaining, setTimeRemaining] = useState({
    days: 0,
    hours: 0,
    minutes: 0,
    seconds: 0,
    milliseconds: 0,
  });

  useEffect(() => {
    const interval = setInterval(() => {
      const now = new Date();
      const timeDifference = targetTimestamp - now.getTime();

      if (timeDifference > 0) {
        const days = Math.floor(timeDifference / (1000 * 60 * 60 * 24));
        const hours = Math.floor(
          (timeDifference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
        );
        const minutes = Math.floor(
          (timeDifference % (1000 * 60 * 60)) / (1000 * 60)
        );
        const seconds = Math.floor((timeDifference % (1000 * 60)) / 1000);
        const milliseconds = timeDifference % 1000;

        setTimeRemaining({
          days,
          hours,
          minutes,
          seconds,
          milliseconds,
        });
      } else {
        clearInterval(interval);
      }
    }, 1000);

    return () => clearInterval(interval);
  }, []);

  return (
    <div className=" justify-center ltr realtive mr-5 px-2 mt-[-30px] xs:hidden lg:flex ">
      <div className=" rounded-full bg-red-100 w-[50px] h-[50px] mx-1">
        <p className="mb-0 = text-center   text-red-900 font-bold pt-3 ">
          <PersianNumber
            number={timeRemaining.days}
            style={"text-[27px] text-red-900 "}
          />
        </p>

        <p className="text-[10px] pt-4 text-center text-gray-600 ">روز</p>
      </div>

      <div className=" rounded-full bg-red-100 w-[50px] h-[50px] mx-1">
        <p className="mb-0 text-center text-red-900 font-bold pt-3 ">
          <PersianNumber
            number={timeRemaining.hours}
            style={"text-[27px] text-red-900 "}
          />
        </p>
        <p className="text-[10px] pt-4 text-center text-gray-600 ">ساعت</p>
      </div>
      <div className=" rounded-full bg-red-100 w-[50px] h-[50px] mx-1">
        <p className="mb-0  text-center   text-red-900 font-bold pt-3 ">
          <PersianNumber
            number={timeRemaining.minutes}
            style={"text-[27px] text-red-900 "}
          />
        </p>
        <p className="text-[10px] pt-4 text-center text-gray-600 ">دقیقه</p>
      </div>
      <div className=" rounded-full bg-red-100 w-[50px] h-[50px] mx-1">
        <Link href="/login">
          {" "}
          <p className="mb-0 text-center   text-red-900 font-bold pt-3 ">
            <PersianNumber
              number={timeRemaining.seconds}
              style={"text-[27px] text-red-900 "}
            />
          </p>
          <p className="text-[10px] pt-4 text-center text-gray-600 ">ثانیه</p>
        </Link>
      </div>
    </div>
  );
};

export default TimerDown;
