<div id="nm-list">
  <% nms_with_popped = APP_DATA[instance.zone.to_sym][:nms].map {|nm|
    is_popped = instance.pops.filter { |pop| (Time.now.utc - 120.minutes) <= pop.created_at }.any? { |pop| pop.name == nm[:name].parameterize }
    { is_popped: is_popped, **nm }
  } %>
  <% content_for :title, "#{instance.zone}, #{nms_with_popped.filter { |n| !n[:is_popped] && !has_missing_reqs?(n, forecast) }.count} NMs avail. | eureka.coffee" %>
  <% nms_with_popped.each do |nm| %>
    <section class="<%= class_names(popped: nm[:is_popped], missing_reqs: has_missing_reqs?(nm, forecast)) %>">
      <div>
        <img src="<%= "/#{nm[:element]}.png" %>" alt="<%= nm[:element] %>" width="30" />
      </div>
      <div class="meta">
        <h3 class="nm-info">
          <span class="badge">LV<%= nm[:level].to_s.rjust(2, "0") %></span>
          <% if nm[:x] and nm[:y] %>
            <span class="map-icon">
              <%= render partial: "map_icon" %>
              <div class="map-popup">
                <div style="position: relative; width: 200px; height: 200px;">
                  <img src="/maps/<%= @instance.zone %>_fw.jpg" style="width: 200px; z-index: 100;" />
                  <span class="map-nm-loc" style="position: absolute; left: <%= (nm[:x] / 42.0) * 100.0 %>%; top: <%= (nm[:y] / 42.0) * 100.0 %>%;"></span>
                </div>
              </div>
            </span>
          <% end %>
          <%= nm[:name] %>
          <% if nm[:drops] %>
            <span title="<%= nm[:drops] %>" style="font-size: 14px;">✨</span>
          <% end %>
          <% if nm[:weather] %>
            <img src="/weather/<%= nm[:weather] %>.png" title="during <%= nm[:weather] %> only" width="15" />
          <% end %>
        </h3>
        <div class="spawn-info">
          «
          <strong><%= nm[:spawned_by][:name] %></strong>
          <% if nm[:spawned_by][:night_only] %>
            <span title="only at night">🌙</span>
          <% end %>
          <% if nm[:spawned_by][:weather] %>
            <img src="/weather/<%= nm[:spawned_by][:weather] %>.png" title="during <%= nm[:spawned_by][:weather] %> only" width="15" />
          <% end %>
          <small class="badge">LV<%= nm[:spawned_by][:level].to_s.rjust(2, "0") %></small>
        </div>
      </div>
      <div class="condition">
        <% if nm[:is_popped] %>
          <div class="pop-timer">
            <% pop = instance.pops.find { |pop| pop.name == nm[:name].parameterize } %>
            <% mins = ActiveSupport::Duration.build(Time.current - pop.created_at) %>
            <div class="timer">
              <div>» <%= (120.minutes - mins).in_minutes.ceil %>m</div>
              <div class="progress-container">
                <span class="progress-bar" style="width: <%= (mins.in_minutes / 120) * 100 %>%"></span>
              </div>
            </div>
            <details class="needs_pwd" hx-preserve id="change-form-<%= nm[:name].parameterize %>">
              <summary>adjust pop time</summary>
              <%= form_with url: adjust_pop_path(instance: instance.public_id, nm: nm[:name].parameterize), class: "action-form", html: {"hx-boost": true, "hx-replace-url": false, "hx-target": "#nm-list", "hx-select": "#nm-list", "hx-swap": "outerHTML:w
"} do |f| %>
                <%= f.number_field :mins, placeholder: "minutes ago" %>
                <%= f.submit "change" %>
              <% end %>
            </details>
          </div>
        <% end %>
        <div>
          <% if nm[:is_popped] && has_missing_reqs?(nm, forecast) %>
            »
          <% end %>
          <% if nm[:weather] && forecast[0][:curr_weather] != nm[:weather] %>
            <% next_pattern = forecast.find { |f| f[:curr_weather] == nm[:weather] } %>
            <%= Weather.get_weather_name(nm[:weather]) %> in <%= ((next_pattern[:time] - Time.now.utc) / 1.minutes).ceil %>m
          <% end %>
          <% if nm[:spawned_by][:weather] && forecast[0][:curr_weather] != nm[:spawned_by][:weather] %>
            <% next_pattern = forecast.find { |f| f[:curr_weather] == nm[:spawned_by][:weather] } %>
            <%= Weather.get_weather_name(nm[:spawned_by][:weather]) %> in <%= ((next_pattern[:time] - Time.now.utc) / 1.minutes).ceil %>m
          <% end %>
          <% if (nm[:night_only] || nm[:spawned_by][:night_only]) && is_day? %>
            Night in <%= ((Clock.to_earth_time(Clock.get_current_eorzea_time.change(hour: 18)) - Time.now.utc) / 1.minutes).ceil %>m
          <% end %>
        </div>
      </div>
      <div class="action needs_pwd">
        <% if nm[:is_popped] %>
          <button
            class="action reset"
            hx-post="/reset?instance=<%= @instance.public_id %>&nm=<%= nm[:name].parameterize %>"
            hx-target="#nm-list"
            hx-select="#nm-list"
            hx-swap="outerHTML"
          >Reset</button>
        <% else %>
          <button
            class="action"
            hx-post="/pop?instance=<%= @instance.public_id %>&nm=<%= nm[:name].parameterize %>"
            hx-target="#nm-list"
            hx-select="#nm-list"
            hx-swap="outerHTML"
          >Pop</button>
        <% end %>
      </div>
    </section>
  <% end %>


  <a href="/maps/<%= @instance.zone %>_full.jpg" target="_blank">
    <button>full map (new tab)</button>
  </a>
  <%= form_with url: clone_instance_path(instance: @instance.public_id), html: { style: "display: inline-block;" } do |f| %>
    <%= f.submit "clone instance" %>
  <% end %>
</div>
