import re

with open("app/static/bim/index.html", "r", encoding="utf-8") as f:
    content = f.read()

# Replace the exportLifetimeVideo function definition
old_func_def = r"function exportLifetimeVideo\(durationMs = 50000\) \{"
new_func_def = r"function exportLifetimeVideo(durationMs = 150000) {"  # 2.5 minutes!
content = re.sub(old_func_def, new_func_def, content)

# Replace the waypoints
old_waypoints = r"          // Define 6-Waypoint Camera Spline along the curved 146\.46m Alignment.*?const targetSpline = new THREE\.CatmullRomCurve3\(targetWaypoints\);"

new_waypoints = """          // Generate Dynamic Waypoints based on Segment Stations
          const camWaypoints = [
            // Toàn cảnh đầu tuyến
            new THREE.Vector3(-30, 45, 30),
            // Phân đoạn 1 (từ góc cao)
            getPointAt(10, 25, 20),
            // Phân đoạn 1 (góc cận cảnh bên dưới để thấy cốt thép)
            getPointAt(35, -15, 10),
            // Chuyển giao sang Phân đoạn 2
            getPointAt(60, 20, 15),
            // Phân đoạn 2 (góc cận)
            getPointAt(85, -20, 12),
            // Chuyển giao sang Phân đoạn 3
            getPointAt(110, 25, 18),
            // Phân đoạn 3 (góc cận cửa xả)
            getPointAt(135, -20, 12),
            // Toàn cảnh cuối tuyến (rút cừ xong)
            new THREE.Vector3(50, 75, 80)
          ];
          const targetWaypoints = [
            getPointAt(10, 0, 0),
            getPointAt(25, 0, -2),
            getPointAt(35, 0, -2),
            getPointAt(70, 0, -2),
            getPointAt(85, 0, -2),
            getPointAt(115, 0, -2),
            getPointAt(135, 0, -2),
            getPointAt(75, 0, 0)
          ];
          
          const camSpline = new THREE.CatmullRomCurve3(camWaypoints);
          const targetSpline = new THREE.CatmullRomCurve3(targetWaypoints);"""

content = re.sub(old_waypoints, new_waypoints, content, flags=re.DOTALL)

with open("app/static/bim/index.html", "w", encoding="utf-8") as f:
    f.write(content)
print("Camera updated!")
