import Webcam from "react-webcam";
import styles from "./styles.module.scss";
import ModalOverlay, { ModalOverlayType } from "@/components/palmistry/modal-overlay/modal-overlay";
import Modal from "@/components/palmistry/modal/modal";
import { useRef, useState } from "react";
// import { useDynamicSize } from "@/hooks/useDynamicSize";

interface CameraModalProps {
    onClose: () => void;
    onTakePhoto: (photo: string) => void;
    onError: (error: string | DOMException) => void;
    reinitializeKey?: number; // for reinitializing the camera (change the key to reinitialize the camera)
    isCameraVisible?: boolean;
}

interface ExtendedMediaTrackCapabilities extends MediaTrackCapabilities {
    torch?: boolean;
}

interface TorchConstraints extends MediaTrackConstraintSet {
    torch?: boolean;
}

interface ExtendedMediaTrackConstraints extends MediaTrackConstraints {
    advanced?: TorchConstraints[];
}

function CameraModal({
    onClose,
    onTakePhoto,
    onError,
    reinitializeKey = 0,
    isCameraVisible = true
}: CameraModalProps) {
    const [isVideoReady, setIsVideoReady] = useState(false);
    const [isTorchOn, setIsTorchOn] = useState(false);
    const [isTorchAvailable, setIsTorchAvailable] = useState(false);
    // const {
    //     width: _width, height: _height,
    //     elementRef: containerRef } = useDynamicSize<HTMLDivElement>({});
    // const [width, height] = [720, 1280];
    // const width = _width * 2;
    // const height = _height * 2;

    // const isLandscape = height <= width;
    // const ratio = isLandscape ? width / height : height / width;
    const cameraRef = useRef<Webcam>(null);

    // useEffect(() => {
    //     setIsVideoReady(false);
    // }, [reinitializeKey]);

    const onClickOverlay = (e: React.MouseEvent) => {
        if (e.target === e.currentTarget) {
            onClose();
        }
    };

    const onClickTakePhoto = () => {
        if (!isVideoReady) return onError("Video is not ready");
        const photo = cameraRef.current?.getScreenshot(
            // {
            //     width,
            //     height,
            // }
        );
        if (photo) {
            onTakePhoto(photo);
        }
    };

    const checkTorchAvailability = async (track: MediaStreamTrack) => {
        try {
            const capabilities = track.getCapabilities() as ExtendedMediaTrackCapabilities;
            setIsTorchAvailable(!!capabilities.torch);
        } catch (error) {
            setIsTorchAvailable(false);
            console.error('Ошибка при проверке поддержки фонарика:', error);
        }
    };

    const onUserMedia = (stream: MediaStream) => {
        setIsVideoReady(true);
        const track = stream.getVideoTracks()[0];
        if (track) {
            checkTorchAvailability(track);
        }
    };

    const toggleTorch = async () => {
        try {
            const track = cameraRef.current?.video?.srcObject instanceof MediaStream
                ? (cameraRef.current.video.srcObject as MediaStream).getVideoTracks()[0]
                : null;

            if (track) {
                const capabilities = track.getCapabilities() as ExtendedMediaTrackCapabilities;
                if (capabilities.torch) {
                    const constraints: ExtendedMediaTrackConstraints = {
                        advanced: [{ torch: !isTorchOn }]
                    };
                    await track.applyConstraints(constraints);
                    setIsTorchOn(!isTorchOn);
                } else {
                    onError("Вспышка не поддерживается на этом устройстве");
                }
            }
        } catch (error) {
            onError(error instanceof Error ? error.message : "Ошибка при управлении вспышкой");
        }
    };

    return <ModalOverlay
        type={ModalOverlayType.Dark}
        className={styles.overlay}
        onClick={onClickOverlay}
    >
        <Modal
            onClose={onClose}
            noCloseButton
            modalClassName={styles.modal}
        >
            <div
                // ref={containerRef}
                className={styles.cameraContainer}>
                {/* {width} {height} */}
                <svg
                    className={styles.handIcon} width="226" height="435" viewBox="0 0 226 435" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M44.9888 101.991C31.3554 80.777 16.4145 88.555 8.56162 99.382C3.99661 105.676 3.45507 113.764 3.41227 121.539C3.06124 185.302 2.66386 298.933 3.47936 322.748C4.34753 348.102 22.1089 380.082 30.881 392.903C47.521 412.857 95.0172 446.752 151.882 422.699C208.748 398.646 222.91 337.033 223 307.148V181.957V181.957C222.663 170.327 216.966 158.307 205.412 156.937C201.087 156.425 197.042 156.699 193.716 157.409C191.581 157.864 189.641 158.915 187.848 160.159V160.159C181.442 164.605 177.583 171.876 177.493 179.673L176.571 259.523L177.251 200.682L176.705 74.0516C176.618 53.7907 170.942 24.5591 151.038 28.3435C144.116 29.6596 138.353 35.2118 135.333 42.942M44.9888 101.991L46.074 202.212M44.9888 101.991L45.9236 62.9801C46.2026 51.3361 47.4303 38.0466 57.4751 32.1503C67.0778 26.5135 80.2774 27.7506 90.8392 42.942M90.8392 42.942V202.212M90.8392 42.942V38.4912C90.8392 26.0387 91.7293 11.455 102.608 5.39458C108.902 1.88803 116.617 1.86364 124.232 7.80872C130.541 12.7337 133.624 20.5445 134.497 28.5001C134.994 33.0286 135.333 38.1796 135.333 42.942M135.333 42.942V202.212" stroke="white" strokeWidth="6" strokeLinecap="round" />
                </svg>

                {isCameraVisible && <Webcam
                    key={reinitializeKey}
                    ref={cameraRef}
                    className={styles.camera}
                    muted
                    // width={width}
                    // height={height}
                    // disablePictureInPicture={true}
                    screenshotQuality={2}
                    videoConstraints={{
                        facingMode: { ideal: "environment" },
                        // width,
                        // height,
                        // aspectRatio: ratio,
                    }}
                    onUserMedia={onUserMedia}
                    onUserMediaError={(error) => {
                        setIsVideoReady(false);
                        setIsTorchAvailable(false);
                        console.error(error);
                        onError(error);
                    }}
                />}
                <div
                    className={styles.shutterButton}
                    onClick={onClickTakePhoto}
                    style={{
                        opacity: isVideoReady ? 1 : 0.5,
                    }}
                />
                {isTorchAvailable && (
                    <button
                        className={`${styles.torchButton} ${isTorchOn ? styles.torchButtonActive : ""}`}
                        onClick={toggleTorch}
                        style={{
                            opacity: isVideoReady ? 1 : 0.5,
                        }}
                    >
                        <div className={styles.torchIconContainer}>
                            <svg
                                className={styles.torchIcon}
                                width="38"
                                height="38"
                                viewBox="0 0 1024 1024"
                                version="1.1"
                                xmlns="http://www.w3.org/2000/svg"
                            >
                                <path fill="#fff" d="M634 64H390c-35.2 0-48 28.8-48 64h340c0-35.2-12.8-64-48-64zM392.2 295c15.2 17.6 23.8 40 23.8 63.4v531.8c0 43.8 35.8 69.8 79.8 69.8h32.6c43.8 0 79.8-25.8 79.8-69.8V358.4c0-23.4 8.6-45.6 23.8-63.4 30.8-35.8 50-69 50-135H342c0 70 19.2 99.2 50.2 135z m63.8 181.6c0-31.2 25.2-56.6 56-56.6s56 25.4 56 56.6v70.8c0 31.2-25.2 56.6-56 56.6s-56-25.4-56-56.6v-70.8z" />
                                <path fill="#fff" d="M512 546m-40 0a40 40 0 1 0 80 0 40 40 0 1 0-80 0Z" />
                            </svg>
                        </div>
                    </button>
                )}
            </div>
        </Modal>
    </ModalOverlay>;
}

export default CameraModal;

