const cameraHelperElementObject = () =>
{
    return {
        videoElement: document.querySelector('#video-element'),
        openCameraButton: document.querySelector('#open-the-camera'),
        openCameraLabel: document.querySelector('#open-the-camera-label'),
        videoContainer: document.querySelector('.visual-similarity__video-container'),
        visualSimilarityP: document.querySelector('.visual-similarity__p'),
        captureButton: document.querySelector('#capture-image'),
        containerBeforeDrop: document.querySelector('.visual-similarity__drop-container'),
        containerAfterDrop: document.querySelector('.visual-similarity__dropped-image-container'),
        previewImage: document.querySelector('#preview-image'),
        cancelButton: document.querySelector('#cancel-capture-image'),
        cameraSwitchButton: document.querySelector('#camera-switch-button'),
        leftCameraContainer: document.querySelector('.visual-similarity__camera-button-left'),
        infoContainer: document.querySelector('.visual-similarity-info__container')
    }
};

const handleOpenCameraElements = () =>
{
    const {
        containerBeforeDrop,
        videoContainer,
        visualSimilarityP,
        captureButton
    } = cameraHelperElementObject();

    if(videoContainer && videoContainer?.style.display !== 'flex') videoContainer.style.display = 'flex';
    if(!containerBeforeDrop?.classList.contains('athena-helper-hide')) containerBeforeDrop?.classList.add('athena-helper-hide');
    if(!visualSimilarityP?.classList.contains('athena-helper-hide')) visualSimilarityP?.classList.add('athena-helper-hide');
    if(captureButton?.classList.contains('athena-helper-hide')) captureButton?.classList.remove('athena-helper-hide');
};

const handleCloseCameraElements = (
    upload = false
)=>
{
    const {
        containerBeforeDrop,
        containerAfterDrop,
        videoContainer,
        visualSimilarityP,
        captureButton
    } = cameraHelperElementObject();

    if(videoContainer && videoContainer?.style.display !== 'none') videoContainer.style.display = 'none';

    if(upload) {
        if(!containerBeforeDrop?.classList.contains('athena-helper-hide')) containerBeforeDrop?.classList.add('athena-helper-hide');
        if(!containerAfterDrop?.classList.contains('athena-helper-show')) containerAfterDrop?.classList.add('athena-helper-show');
    } else {
        if(containerAfterDrop?.classList.contains('athena-helper-show')) containerAfterDrop?.classList.remove('athena-helper-show');
        if(containerBeforeDrop?.classList.contains('athena-helper-hide')) containerBeforeDrop?.classList.remove('athena-helper-hide');
    }

    if(!captureButton?.classList.contains('athena-helper-hide')) captureButton?.classList.add('athena-helper-hide');
    if(visualSimilarityP?.classList.contains('athena-helper-hide')) visualSimilarityP?.classList.remove('athena-helper-hide');
};

const checkIfCameraExists = async () =>
{
    if (!navigator.mediaDevices?.enumerateDevices)
        return false;

    try {
        const devices = await navigator.mediaDevices.enumerateDevices();
        return devices.some(device => device.kind === 'videoinput');
    } catch (error) {
        console.error(`Athena XAI Error Checking Camera: ${error}`);
        return false;
    }
};

const isDeviceMobileOrTablet = async () =>
{
    return /iPhone|iPad|iPod|Android|Tablet|Nexus 7|Nexus 10|KFAPWI/i.test(navigator.userAgent);
};

const setInitialCameraView = async () =>
{
    if(await isDeviceMobileOrTablet())
        return 'environment';

    return 'user';
};

const isIOS = async () =>
{
    return /iPad|iPhone|iPod/.test(navigator.userAgent) ||
        (navigator.userAgent.includes("Macintosh") && 'ontouchend' in document);
};

const closeLabelCamera = () =>
{
    const { openCameraLabel } = cameraHelperElementObject();
    if(openCameraLabel) openCameraLabel.style.display = 'none';
};

const openLabelCamera = () =>
{
    const { openCameraLabel } = cameraHelperElementObject();
    if(openCameraLabel) openCameraLabel.style.display = 'flex';
};

export {
    handleCloseCameraElements,
    handleOpenCameraElements,
    cameraHelperElementObject,
    checkIfCameraExists,
    isDeviceMobileOrTablet,
    setInitialCameraView,
    isIOS,
    closeLabelCamera,
    openLabelCamera
}