<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="icon" type="image/png" href="{{ url_for('static', filename='favicon.png') }}"/>
        <link rel="stylesheet" href="{{ url_for('static', filename='js9/js9support.css')}}"/>
        <link rel="stylesheet" href="{{ url_for('static', filename='js9/js9.css')}}"/>
        <link href="{{ url_for('static', filename='styles.css') }}" rel="stylesheet"/>
        <!-- Fix: Remove extra quotation marks in script tags -->
        <script src="{{ url_for('static', filename='js9/js9prefs.js') }}"></script>
        <script src="{{ url_for('static', filename='js9/js9support.min.js') }}"></script>
        <script src="{{ url_for('static', filename='js9/js9.min.js') }}"></script>
        <script src="{{ url_for('static', filename='js9/js9plugins.js') }}"></script>
        <title>SIGMAS</title>
    </head>
    <body>
        <a href="{{ url_for('home') }}" class="home-button">←</a>
        <h1>SIGMAS - Simulation Interface for Generating METIS Astronomical Spectra</h1>
        <form action="/regular_simulation" method="post" enctype="multipart/form-data">
          {% with messages = get_flashed_messages() %}
            {% if messages %}
              <ul class=flashes>
              {% for message in messages %}
                <li>{{ message }}</li>
              {% endfor %}
              </ul>
            {% endif %}
          {% endwith %}
          <div class="choose">
            <h2>Please choose your Simulation Parameters:</h2>
          </div> 
          <div class="form-top">
            <div class="form-left">
              <label for="val1">Exposure Time:</label>
              <input type="number" id="val1" name="exposure_time" step="any" min=0>
            </div>
            <div class="form-right">
              <label for="source" class="dropdown_label">Choose your source:</label>
              <br>
              <select name="source" id="source" class="dropdown">
                <option value="simple_star12">Standard Star</option>
                <option value="star_field">Star Field</option>
                <option value="empty_sky">Empty Sky</option>
              </select>
              <br>
              <br>
              <label for="mode" class="dropdown_label">Choose observing mode:</label>
              <br>
              <select name="mode" id="mode" class="dropdown">
                <option value="img_lm">IMG LM mode</option>
                <option value="img_n">IMG N mode</option>
              </select>
            </div>
          </div>
          <div class="submit-row">
            <input type="submit" value="Start Simulation">
          </div>
        </form>

        {% if fits_url %}
        <div class="js9-container">
          <div class="sim_text">Simulated detector image of a {{ src }} in {{ mode }} mode.</div>
          <div class="JS9Menubar" data-width="420px"></div>
          <div class="JS9" id="JS9" data-width="420px" data-height="420px"></div>
        </div>
        <!-- Load FITS file after page is fully loaded -->
        <script>
          document.addEventListener('DOMContentLoaded', function() {
            // Make sure JS9 is ready
            if (typeof JS9 !== 'undefined') {
              // Wait a bit to ensure display is initialized
              setTimeout(function() {
                console.log("Loading FITS file from: {{ fits_url }}");
                JS9.Load("{{ fits_url }}", {scale: "log", colormap: "viridis"});
              }, 1000);
            } else {
              console.error("JS9 is not defined!");
            }
          });
        </script>
        {% endif %}
    </body>
</html>