
document.addEventListener("DOMContentLoaded", function () {
    const productWrappers = document.querySelectorAll('.bwd-cv-wrap-watermark');
    const wCV2Active = bwdcvSettings.wCV2Allow;
    const wCVActive = bwdcvSettings.wCVAllow;
    const wRightClickActive = bwdcvSettings.wRightClickCheck;
    const wDeveloperToolsctive = bwdcvSettings.wDeveloperToolsCheck;
    const wTextOrImg = bwdcvSettings.wTextImageAllow;
    if(wCV2Active!='on'){
        if(wCVActive==='on'){
            productWrappers.forEach((wrapper) => {
                const watermarkWidth = 100;
                const watermarkHeight = 50;
                const margin = 10;
                const rotationValue = bwdcvSettings.textRotation;
                const wText = bwdcvSettings.watermarkText;
                const watermarkImage = bwdcvSettings.wImageURL;
                const fillColor = bwdcvSettings.textColor;
                const fontSize = bwdcvSettings.fontSize + 'px';
                const fontFamily = bwdcvSettings.fontFamily;
                const fontWeight = bwdcvSettings.fontWeight;
                const fontStyle = bwdcvSettings.fontStyle;
                const opacityImg = bwdcvSettings.imgOpacity;

                const canvas = document.createElement('canvas');
                const ctx = canvas.getContext('2d');

                canvas.width = wrapper.offsetWidth;
                canvas.height = wrapper.offsetHeight;

                if (wTextOrImg==='w-logo') {
                    // if (watermarkImage.startsWith('https')) {
                        const img = new Image();
                        img.crossOrigin = "anonymous";
                        img.src = watermarkImage;
                        ctx.globalAlpha = opacityImg;
                        img.onload = () => {
                            for (let row = 0; row < canvas.height / (watermarkHeight + margin); row++) {
                                for (let col = 0; col < canvas.width / (watermarkWidth + margin); col++) {
                                    const x = col * (watermarkWidth + margin);
                                    const y = row * (watermarkHeight + margin);

                                    ctx.save();
                                    ctx.translate(x + watermarkWidth / 2, y + watermarkHeight / 2);
                                    ctx.rotate((rotationValue * Math.PI) / 180);
                                    ctx.drawImage(img, -watermarkWidth / 2, -watermarkHeight / 2, watermarkWidth, watermarkHeight);
                                    ctx.restore();
                                }
                            }

                            applyWatermark(wrapper, canvas);
                        };
                    // }
                } else if(wTextOrImg==='w-text') {
                    ctx.font = `${fontWeight} ${fontStyle} ${fontSize} ${fontFamily}`;
                    ctx.fillStyle = fillColor;
                    ctx.textAlign = 'center';
                    ctx.textBaseline = 'middle';

                    for (let row = 0; row < canvas.height / (watermarkHeight + margin); row++) {
                        for (let col = 0; col < canvas.width / (watermarkWidth + margin); col++) {
                            const x = col * (watermarkWidth + margin) + watermarkWidth / 2;
                            const y = row * (watermarkHeight + margin) + watermarkHeight / 2;

                            ctx.save();
                            ctx.translate(x, y);
                            ctx.rotate((rotationValue * Math.PI) / 180);
                            ctx.fillText(wText, 0, 0);
                            ctx.restore();
                        }
                    }

                    applyWatermark(wrapper, canvas);
                }
            });

            function applyWatermark(wrapper, canvas) {
                const watermarkOverlay = document.createElement('div');
                watermarkOverlay.style.position = 'absolute';
                watermarkOverlay.style.top = '0';
                watermarkOverlay.style.left = '0';
                watermarkOverlay.style.width = '100%';
                watermarkOverlay.style.height = '100%';
                watermarkOverlay.style.pointerEvents = 'none';
                watermarkOverlay.style.zIndex = '1000';
                watermarkOverlay.style.backgroundImage = `url(${canvas.toDataURL()})`;
                watermarkOverlay.style.backgroundSize = 'cover';
                watermarkOverlay.style.backgroundPosition = 'center';

                wrapper.style.position = 'relative';
                wrapper.appendChild(watermarkOverlay);
            }
        }
    }

    if(wRightClickActive==='on'){
        productWrappers.forEach(function (wrapper) {
            wrapper.addEventListener("contextmenu", function (e) {
                e.preventDefault();
            });
        });
    }

    if(wDeveloperToolsctive==='on'){
        let devtoolsOpen = false;
        const threshold = 160;
        function detectDevTools() {
            const widthThreshold = window.outerWidth - window.innerWidth > threshold;
            const heightThreshold = window.outerHeight - window.innerHeight > threshold;
            if (widthThreshold || heightThreshold) {
                if (!devtoolsOpen) {
                    devtoolsOpen = true;
                    alert("Developer Tools have been opened. Please close it first.");
                    window.location.reload();
                }
            } else {
                devtoolsOpen = false;
            }
        }
        setInterval(detectDevTools, 1000);
    }

});

