import Title from "@/components/Title";
import styles from "./styles.module.scss";
import routes from "@/routes";
import Button from "../../components/Button";
import BiometricData from "@/components/palmistry/biometric-data/biometric-data";
import { useNavigate } from "react-router-dom";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";
import ScanInstructionSVG from "../../images/SVG/ScanInstruction";
import { usePreloadImages } from "@/hooks/preload/images";
import Loader, { LoaderColor } from "@/components/Loader";
import { useUploadImage } from "@/hooks/palmistry/useUploadImage";

function ScanInstruction() {
  const { translate } = useTranslations(ELocalesPlacement.PalmistryV1);
  const navigate = useNavigate();
  usePreloadImages([
    "/v1/palmistry/love-line.svg",
    "/v1/palmistry/head-line.svg",
    "/v1/palmistry/life-line.svg",
    "/v1/palmistry/fate-line.svg",
    "/v1/palmistry/hand-little-finger.svg",
  ]);

  const handleToScannedPhoto = () => {
    navigate(routes.client.palmistryV1ScannedPhoto());
  };

  const { isLoading, isError, onSelectFile } = useUploadImage({
    fingersNames: {
      thumb: translate("thumb"),
      index_finger: translate("index_finger"),
      middle_finger: translate("middle_finger"),
      ring_finger: translate("ring_finger"),
      pinky: translate("pinky"),
    },
    keyOfStore: "palmistry",
    onImageUploaded: handleToScannedPhoto
  });

  const handleClick = () => {
    navigate(routes.client.palmistryV1Camera());
  };

  return (
    <div className={styles.page}>
      <Title variant="h2" className={styles.title}>
        {translate("/scan-instruction.title")}
      </Title>
      <ScanInstructionSVG />
      <Button className={styles.button} onClick={handleClick}>
        {translate("/scan-instruction.button")}
      </Button>
      <input
        id="upload-input"
        type="file"
        accept="image/*"
        style={{ display: 'none' }}
        onChange={onSelectFile}
      />
      <label htmlFor="upload-input" className={styles.labelUpload}>
        {translate("/scan-instruction.upload_photo")}
        {isLoading && <div className={styles.loaderContainer}>
          <Loader color={LoaderColor.Black} />
        </div>}
      </label>
      {isError &&
        <p className={styles.error}>{translate("/scan-instruction.error")}</p>
      }
      <BiometricData className={styles.biometric} />
    </div>
  );
}

export default ScanInstruction;
