import sys, os

src = os.path.join(os.path.dirname(__file__), '../../frontend/lab.html')

with open(src, 'r', encoding='utf-8') as f:
    content = f.read()

# ─────────────────────────────────────────────────────────────────────────────
# PATCH 1: Add animated ray buttons + lens-maker controls to lens panel
# Insert BEFORE the Aberrации section
# ─────────────────────────────────────────────────────────────────────────────

OLD_LENS = (
    '            <div class="pp-hint">Тащи стрелку-предмет или фокус мышью</div>\n'
    '            <div style="margin-top:8px"></div>\n'
    '            <div class="gp-section-title" style="margin-bottom:6px">Аберрации</div>'
)

NEW_LENS = (
    '            <div class="pp-hint">Тащи стрелку-предмет или фокус мышью</div>\n'
    '            <div style="margin-top:8px"></div>\n'
    '            <!-- Feature 1: animated ray buttons -->\n'
    '            <div style="display:flex;gap:4px;margin-bottom:8px">\n'
    '              <button onclick="if(lensSim)lensSim.buildRays()" style="flex:1;padding:5px 0;border-radius:6px;border:none;background:linear-gradient(135deg,#06D6E0,#9B5DE5);color:#fff;font-size:.72rem;font-weight:700;cursor:pointer">Построить лучи</button>\n'
    '              <button onclick="if(lensSim)lensSim.resetRays()" style="padding:5px 9px;border-radius:6px;border:1px solid #333;background:#1a1a2e;color:#888;font-size:.78rem;cursor:pointer" title="Сбросить лучи">&#8634;</button>\n'
    '            </div>\n'
    '            <!-- Feature 3: lens-maker toggle -->\n'
    '            <div style="margin-bottom:6px">\n'
    '              <label style="display:flex;align-items:center;gap:6px;font-size:.72rem;color:#ccc;cursor:pointer">\n'
    '                <input type="checkbox" id="ltog-lensmaker" onchange="lensToggleLM(this.checked)">\n'
    '                Подробный (R1/R2/n)\n'
    '              </label>\n'
    '            </div>\n'
    '            <!-- LM sliders (hidden by default) -->\n'
    '            <div id="ob-lm-sliders" style="display:none">\n'
    '              <div class="proj-slider-row" style="margin-bottom:6px">\n'
    '                <label style="font-size:.72rem;color:#ccc;width:60px">R1 = <span id="lm-r1-val" style="color:#FFD166;font-weight:700">200</span></label>\n'
    '                <input type="range" id="sl-lm-r1" min="-300" max="300" step="5" value="200" oninput="lensLMParam(\'R1\',this.value)" style="flex:1">\n'
    '              </div>\n'
    '              <div class="proj-slider-row" style="margin-bottom:6px">\n'
    '                <label style="font-size:.72rem;color:#ccc;width:60px">R2 = <span id="lm-r2-val" style="color:#FFD166;font-weight:700">-200</span></label>\n'
    '                <input type="range" id="sl-lm-r2" min="-300" max="300" step="5" value="-200" oninput="lensLMParam(\'R2\',this.value)" style="flex:1">\n'
    '              </div>\n'
    '              <div class="proj-slider-row" style="margin-bottom:8px">\n'
    '                <label style="font-size:.72rem;color:#ccc;width:60px">n = <span id="lm-n-val" style="color:#9B5DE5;font-weight:700">1.50</span></label>\n'
    '                <input type="range" id="sl-lm-n" min="1.3" max="2.4" step="0.05" value="1.5" oninput="lensLMParam(\'n\',this.value)" style="flex:1">\n'
    '              </div>\n'
    '              <div style="font-size:.68rem;color:#888;margin-bottom:6px">f = 1/((n-1)*(1/R1 - 1/R2))</div>\n'
    '            </div>\n'
    '            <div style="margin-top:0"></div>\n'
    '            <div class="gp-section-title" style="margin-bottom:6px">Аберрации</div>'
)

if OLD_LENS not in content:
    print('ERROR: lens panel marker not found'); exit(1)

content = content.replace(OLD_LENS, NEW_LENS, 1)

# ─────────────────────────────────────────────────────────────────────────────
# PATCH 2: Add R slider + parabolic toggle to mirror panel
# Insert BEFORE the "Отображение" section
# ─────────────────────────────────────────────────────────────────────────────

OLD_MIRROR = (
    '            <div class="gp-section-title" style="margin-bottom:6px">Отображение</div>'
)

NEW_MIRROR = (
    '            <!-- Feature 2: R-slider + parabolic toggle -->\n'
    '            <div style="margin-bottom:6px">\n'
    '              <label style="display:flex;align-items:center;gap:6px;font-size:.72rem;color:#ccc;cursor:pointer">\n'
    '                <input type="checkbox" id="mtog-useR" onchange="mirrorToggleR(this.checked)">\n'
    '                Радиус R (непрерывный)\n'
    '              </label>\n'
    '            </div>\n'
    '            <div id="ob-mirror-R-row" class="proj-slider-row" style="margin-bottom:6px;display:none">\n'
    '              <label style="font-size:.72rem;color:#ccc;width:60px">R = <span id="mirror-R-val" style="color:var(--cyan);font-weight:700">240</span></label>\n'
    '              <input type="range" id="sl-mirror-R" min="-250" max="250" step="5" value="240" oninput="mirrorRParam(this.value)" style="flex:1">\n'
    '            </div>\n'
    '            <div style="display:flex;gap:4px;margin-bottom:8px">\n'
    '              <button id="mirror-parab-btn" onclick="mirrorToggleParabolic(this)" style="flex:1;padding:5px 0;border-radius:6px;border:1px solid #333;background:#1a1a2e;color:#888;font-size:.72rem;cursor:pointer">Сферическое</button>\n'
    '            </div>\n'
    '            <div class="gp-section-title" style="margin-bottom:6px">Отображение</div>'
)

if OLD_MIRROR not in content:
    print('ERROR: mirror panel Отображение marker not found'); exit(1)

content = content.replace(OLD_MIRROR, NEW_MIRROR, 1)

with open(src, 'w', encoding='utf-8') as f:
    f.write(content)

print('OK')
