{% extends 'base.html.twig' %}

{% block title %}Calcul{% endblock %}


{% block stylesheets %}
    {{ parent() }}
    <link rel="stylesheet" href="{{ asset('styles/globals/bulles.css') }}">
    {# Styles spécifiques au calculateur (inputs, readouts, tip, chart) #}
    <link rel="stylesheet" href="{{ asset('styles/calculator/index.css') }}">
{% endblock %}

{% block main %}
    <div class="bulle-container aero-calc" data-controller="calculateur">
        <div class="bulle bulle-all-col bulle-header">
            <h1>Calulateur pour Create Aeronautics.</h1>
            <p class="ac-sub">Calculs dérivés des mods Aeronautics et Sable. Le ballon est supposé pleinement chauffé (volume rempli = capacité).</p>
        </div>

        <div class="bulle bulle-all-col">
            <details class="ac-consts">
                <summary>Constantes (config par défaut)</summary>
                <div class="ac-consts-grid">
                    <div class="ac-field"><label>C_T <span class="u">poussée</span></label><input id="aero-ct" type="number" step="any" value="0.2"></div>
                    <div class="ac-field"><label>s_air <span class="u">kpg/m³</span></label><input id="aero-sair" type="number" step="any" value="1.5"></div>
                    <div class="ac-field"><label>g <span class="u">gravité</span></label><input id="aero-g" type="number" step="any" value="11"></div>
                    <div class="ac-field"><label>y_mer <span class="u">niveau mer</span></label><input id="aero-sea" type="number" step="any" value="63"></div>
                    <div class="ac-field"><label>k <span class="u">pente pression</span></label><input id="aero-k" type="number" step="any" value="-0.004"></div>
                    <div class="ac-field"><label>P_max <span class="u">plafond</span></label><input id="aero-pmax" type="number" step="any" value="1.5"></div>
                    <div class="ac-field"><label>y_top <span class="u">limite haute</span></label><input id="aero-ytop" type="number" step="any" value="320"></div>
                    <div class="ac-field"><label>y_bas <span class="u">limite basse</span></label><input id="aero-ybot" type="number" step="any" value="-64"></div>
                </div>
            </details>
        </div>

        <div class="bulle bulle-all-col">

             <div class="ac-tool">
                <h2 class="ac-h3">Hélice</h2>
                <p class="ac-hint">Poussée le long de l'axe du bearing.</p>
                <div class="ac-field"><label>Voiles (sails) <span class="u">P</span></label><input id="aero-sails" type="number" min="0" step="1" value="24"></div>
                <div class="ac-field"><label>Vitesse de rotation <span class="u">N · RPM</span></label><input id="aero-rpm" type="number" step="any" value="256"></div>
                <div class="ac-field"><label>Altitude <span class="u">y</span></label><input id="aero-palt" type="number" step="any" value="63"></div>
                <div class="ac-readout"><div class="ac-rk">Poussée brute</div><div class="ac-rv key" id="aero-othrust">0 <small>pN</small></div></div>
                <div class="ac-readout"><div class="ac-rk">Appliquée à l'arrêt (× P_air)</div><div class="ac-rv" id="aero-othrustapp">0 <small>pN</small></div></div>
            </div>
        </div>
        <div class="bulle bulle-all-col">
                <div class="ac-tool">
                    <h2 class="ac-h3">Ballon chauffé</h2>
                    <p class="ac-hint">Pleinement chauffé : volume rempli = capacité V.</p>
                    <div class="ac-field"><label>Volume intérieur <span class="u">V · m³</span></label><input id="aero-vol" type="number" min="0" step="any" value="512"></div>
                    <div class="ac-field"><label>Altitude <span class="u">y</span></label><input id="aero-balt" type="number" step="any" value="63"></div>
                    <div class="ac-readout"><div class="ac-rk">Force ascensionnelle brute</div><div class="ac-rv key" id="aero-oforce">0 <small>pN</small></div></div>
                    <div class="ac-readout"><div class="ac-rk">Portance (masse soulevable)</div><div class="ac-rv" id="aero-olift">0 <small>kpg</small></div></div>
                    <div class="ac-readout"><div class="ac-rk">Pression locale</div><div class="ac-rv" id="aero-opress">1.000</div></div>
                </div>
        </div>

        <div class="bulle bulle-all-col">
            <h2 class="ac-h2">Altitude maximale du ballon</h2>
            <p class="ac-desc">Altitude de flottaison neutre où la portance compense le poids.</p>

            <div class="ac-controls">
                <div class="ac-field">
                    <label>Tracer en fonction</label>
                    <select id="aero-mode">
                        <option value="mass">du poids (volume fixé)</option>
                        <option value="vol">du volume (poids fixé)</option>
                    </select>
                </div>
                <div class="ac-field" id="aero-fixvolwrap"><label>Volume fixé <span class="u">V · m³</span></label><input id="aero-fixvol" type="number" min="1" step="any" value="512"></div>
                <div class="ac-field" id="aero-fixmasswrap"><label>Poids fixé <span class="u">m · kpg</span></label><input id="aero-fixmass" type="number" min="1" step="any" value="400"></div>
            </div>

            <div class="ac-chart-shell">
                <canvas id="aero-chart"></canvas>
                <div class="ac-tip" id="aero-tip"></div>
            </div>
            <div class="ac-legend">
                <span><i class="ac-sw"></i> altitude max</span>
                <span><i class="ac-sw"></i> repères (mer, limite)</span>
                <span><i class="ac-sw"></i> ne décolle pas</span>
            </div>
            <p class="ac-note"><b>m</b> kpg = <b>m</b> blocs standard (masse défaut 1 kpg/bloc). L'atténuation de pression sur les 40 derniers mètres sous la limite haute n'est pas modélisée.</p>
        </div>


        <section class="bulle bulle-all-col">
            <h2 class="ac-h2">Volume requis</h2>
            <p class="ac-desc">Volume d'air chaud minimal pour flotter à une altitude visée.</p>
            <div class="ac-vol">
                <div class="ac-vol-inputs">
                    <div class="ac-field"><label>Poids total <span class="u">m · kpg</span></label><input id="aero-vmass" type="number" min="0" step="any" value="400"></div>
                    <div class="ac-field"><label>Altitude visée <span class="u">y</span></label><input id="aero-valt" type="number" step="any" value="150"></div>
                </div>
                <div class="ac-vol-out">
                    <div class="ac-readout"><div class="ac-rk">Volume requis</div><div class="ac-rv key" id="aero-ovolreq">0 <small>m³</small></div></div>
                    <p class="ac-cube" id="aero-ocube"></p>
                </div>
            </div>
            <p class="ac-note" id="aero-vwarn"></p>
        </section>

        <section class="bulle bulle-all-col">
            <h2 class="ac-h2">Formules</h2>
            <div class="ac-formulas">
                <div><div class="ac-fk">Poussée hélice brute</div><code>T = C_T · P^(3/2) · N</code></div>
                <div><div class="ac-fk">Appliquée (à l'arrêt, A_flux = 1)</div><code>F = T · A_flux · P_air</code></div>
                <div><div class="ac-fk">Force ballon brute (pleinement chauffé)</div><code>F = V · s_air · g · P_air(y)</code></div>
                <div><div class="ac-fk">Portance (masse)</div><code>L = V · s_air · P_air(y)</code></div>
                <div><div class="ac-fk">Pression atmosphérique</div><code>P_air(y) = min( e^(k·(y − y_mer)) , P_max )</code></div>
                <div><div class="ac-fk">Altitude max (flottaison neutre)</div><code>y_max = y_mer + ln( V·s_air / m ) / k</code></div>
                <div><div class="ac-fk">Volume requis</div><code>V = m / ( s_air · P_air(y) )</code></div>
            </div>
        </section>

    </div>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script>
        // Fallback: si Stimulus n'a pas attaché le controller (par ex. problème de chargement),
        // on charge l'ancien script autonome qui cible la class .aero-calc.
        setTimeout(function(){
            try{
                const root = document.querySelector('.aero-calc');
                if(root && root.dataset.calculateurConnected !== '1'){
                    const s = document.createElement('script');
                    s.type = 'module';
                    s.src = '{{ asset('js/calcultateur_create.js') }}';
                    document.head.appendChild(s);
                    console.warn('calculateur: fallback legacy script injected');
                }
            }catch(e){ console.error(e); }
        }, 300);
    </script>
{% endblock %}

