import Title from "@/components/Title";
import styles from "./styles.module.scss";
import { useNavigate } from "react-router-dom";
import routes from "@/routes";
import { useSelector } from "react-redux";
import { selectors } from "@/store";
import { CircularProgressbar } from "react-circular-progressbar";
import { useCallback, useEffect, useState } from "react";
import { sleep } from "@/services/date";
import { images } from "../../data";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";

function Processing() {
  const { translate } = useTranslations(ELocalesPlacement.Chats);
  const navigate = useNavigate();
  const { username } = useSelector(selectors.selectUser);
  const [loadingProgress, setLoadingProgress] = useState(0);

  const handleNext = useCallback(() => {
    navigate(routes.client.chatsCategories());
  }, [navigate]);

  useEffect(() => {
    (async () => {
      if (loadingProgress >= 100) {
        await sleep(2000);
        return handleNext();
      }
      if (loadingProgress === 51) {
        await sleep(1500);
      } else if (loadingProgress === 73) {
        await sleep(1500);
      } else {
        await sleep(200);
      }
      setLoadingProgress((value) => value + 1);
    })();
  }, [handleNext, loadingProgress]);

  return (
    <section className={styles.container}>
      <Title className={styles.title} variant="h2">
        {translate("/quiz/processing.title", {
          style: (
            <span>
              {translate("/quiz/processing.title_style", {
                name: username,
              })}
            </span>
          ),
        })}
      </Title>

      <svg className={styles["svg-defs"]}>
        <defs>
          <linearGradient
            id="paint0_linear"
            x1="45"
            y2="-9.5"
            x2="53.1842"
            y1="40"
            gradientUnits="userSpaceOnUse"
          >
            <stop offset="4.19%" stopColor="#3639A2"></stop>
            <stop offset="49.79%" stopColor="#6D4BCD"></stop>
            <stop offset="81.72%" stopColor="#9D5BE9"></stop>
          </linearGradient>
        </defs>
      </svg>
      <div className={styles["progress-container"]}>
        <div className={styles["background-gradient"]} />
        <CircularProgressbar
          // counterClockwise={true}
          className={styles["progress-bar"]}
          styles={{
            path: { stroke: "url(#paint0_linear)" },
            trail: { stroke: "#D2D1F9" },
            text: { fill: "#fff", fontSize: "12px" },
          }}
          maxValue={100}
          minValue={0}
          value={loadingProgress}
          text={`${loadingProgress}%`}
          strokeWidth={8}
        />
      </div>

      <ul className={styles.list}>
        <li className={styles.item}>
          <div className={styles.circle}>
            <img src={images("done.webp")} alt="done" width="22" height="16" />
          </div>
          {translate("/quiz/processing.point_1")}
        </li>
        <li className={styles.item}>
          <div className={styles.circle}>
            <img src={images("done.webp")} alt="done" width="22" height="16" />
          </div>
          {translate("/quiz/processing.point_2")}
        </li>
        <li className={styles.item}>
          <div className={styles.circle}>
            <img src={images("done.webp")} alt="done" width="22" height="16" />
          </div>
          {translate("/quiz/processing.point_3")}
        </li>
        <li className={styles.item}>
          <div className={styles.circle}>
            <img src={images("done.webp")} alt="done" width="22" height="16" />
          </div>
          {translate("/quiz/processing.point_4")}
        </li>
      </ul>
    </section>
  );
}

export default Processing;
