import re

with open('resources/views/surat-keluar-workflow/partials/modals.blade.php', 'r') as f:
    content = f.read()

helper = """
                    const getAdjustedCoords = (page, vx, vy, vw, vh) => {
                        const cropBox = page.getCropBox() || page.getMediaBox();
                        const rot = (page.getRotation().angle % 360 + 360) % 360;
                        let cx = cropBox.x || 0, cy = cropBox.y || 0, cw = cropBox.width, ch = cropBox.height;
                        let rx, ry, rRot = 0;
                        if (rot === 0) { rx = cx + vx; ry = cy + ch - vy - vh; rRot = 0; }
                        else if (rot === 90) { rx = cx + vy + vh; ry = cy + vx; rRot = 90; }
                        else if (rot === 180) { rx = cx + cw - vx; ry = cy + vy + vh; rRot = 180; }
                        else if (rot === 270) { rx = cx + cw - vy - vh; ry = cy + ch - vx; rRot = -90; }
                        return { x: rx, y: ry, rotate: rRot };
                    };
"""

# Replace in Stamp
content = re.sub(
    r'const cropBox = targetPage\.getCropBox[^;]+;\n\s*const xPoints = [^;]+;\n\s*const yPoints = [^;]+;',
    helper + r"""
                    const coord = getAdjustedCoords(targetPage, pagePosX * mmToPoints, pagePosY * mmToPoints, 0, fontSize);
                    const xPoints = coord.x;
                    const yPoints = coord.y;
                    const rotOffset = coord.rotate;
""",
    content
)

# Replace in Date
content = re.sub(
    r'const cropBox = targetPage\.getCropBox[^;]+;\n\s*const dateXPoints = [^;]+;\n\s*const dateYPoints = [^;]+;',
    r"""
                        const coord = getAdjustedCoords(targetPage, pagePosX * mmToPoints, pagePosY * mmToPoints, 0, fontSize);
                        const dateXPoints = coord.x;
                        const dateYPoints = coord.y;
                        const rotOffset = coord.rotate;
""",
    content
)

# Replace in QR
content = re.sub(
    r'const cropBox = targetPage\.getCropBox[^;]+;\n\s*const qrX = [^;]+;\n\s*const qrY = [^;]+;',
    r"""
                                const coord = getAdjustedCoords(targetPage, qrPosXMM * mmToPoints, qrPosYMM * mmToPoints, scaledQrWidth, scaledQrHeight);
                                const qrX = coord.x;
                                const qrY = coord.y;
                                const rotOffset = coord.rotate;
""",
    content
)

# Replace in Signature
content = re.sub(
    r'const cropBox = targetPage\.getCropBox[^;]+;\n\s*const sigXPoints = [^;]+;\n\s*const sigYPoints = [^;]+;',
    r"""
                                const coord = getAdjustedCoords(targetPage, pagePosX * mmToPoints, pagePosY * mmToPoints, signatureWidth, signatureHeight);
                                const sigXPoints = coord.x;
                                const sigYPoints = coord.y;
                                const rotOffset = coord.rotate;
""",
    content
)

# Fix drawText parameter to include the base rotation offset
content = re.sub(
    r'rotate: \{ type: \'degrees\', angle: rotation \}',
    r'rotate: { type: \'degrees\', angle: rotation + rotOffset }',
    content
)
# For the rotation === 0 blocks which missing rotate property:
content = re.sub(
    r'if \(rotation === 0\) \{\s+targetPage\.drawText\(([^,]+), \{\s+x: ([^,]+),\s+y: ([^,]+),\s+size: ([^,]+),\s+font: ([^,]+),\s+color: rgb\(0,\s*0,\s*0\)\s+\}\);\s+\}\s+else\s+\{\s+targetPage\.drawText\(\1, \{\s+x: \2,\s+y: \3,\s+size: \4,\s+font: \5,\s+color: rgb\(0,\s*0,\s*0\),\s+rotate: \{ type: \'degrees\', angle: rotation \+ rotOffset \}\s+\}\);\s+\}',
    r'''targetPage.drawText(\1, { x: \2, y: \3, size: \4, font: \5, color: rgb(0, 0, 0), rotate: { type: 'degrees', angle: rotation + rotOffset } });''',
    content
)

content = re.sub(
    r'if \(rotation === 0\) \{\s+targetPage\.drawImage\(([^,]+), \{\s+x: ([^,]+),\s+y: ([^,]+),\s+width: ([^,]+),\s+height: ([^,]+)\s+\}\);\s+\}\s+else\s+\{\s+targetPage\.drawImage\(\1, \{\s+x: \2,\s+y: \3,\s+width: \4,\s+height: \5,\s+rotate: \{ type: \'degrees\', angle: rotation \+ rotOffset \}\s+\}\);\s+\}',
    r'''targetPage.drawImage(\1, { x: \2, y: \3, width: \4, height: \5, rotate: { type: 'degrees', angle: rotation + rotOffset } });''',
    content
)

# Also fix QR which has NO rotation handling initially!
content = re.sub(
    r'rotate: setumQrRotation,',
    r'rotate: { type: \'degrees\', angle: (setumQrRotDeg || 0) + rotOffset },',
    content
)
content = re.sub(
    r'rotate: bagianQrRotation,',
    r'rotate: { type: \'degrees\', angle: (bagianQrRotDeg || 0) + rotOffset },',
    content
)
content = re.sub(
    r'rotate: tteQrRotation,',
    r'rotate: { type: \'degrees\', angle: (tteQrRotDeg || 0) + rotOffset },',
    content
)

with open('resources/views/surat-keluar-workflow/partials/modals.blade.php', 'w') as f:
    f.write(content)
