<div class="flex flex-col space-y-8 justify-center items-center">
  <h1 class="title text-2xl title-primary-500">
    <%= gettext("Range day") %>
  </h1>

  <%= if @packs |> Enum.empty?() do %>
    <h1 class="title text-xl text-primary-600">
      <%= gettext("No ammo staged") %>
      <%= display_emoji("😔") %>
    </h1>

    <.link navigate={Routes.pack_index_path(Endpoint, :index)} class="btn btn-primary">
      <%= dgettext("actions", "Why not get some ready to shoot?") %>
    </.link>
  <% else %>
    <.link navigate={Routes.pack_index_path(Endpoint, :index)} class="btn btn-primary">
      <%= dgettext("actions", "Stage ammo") %>
    </.link>

    <div class="w-full flex flex-row flex-wrap justify-center items-stretch">
      <.pack_card
        :for={%{id: pack_id} = pack <- @packs}
        pack={pack}
        original_count={Map.fetch!(@original_counts, pack_id)}
        cpr={Map.get(@cprs, pack_id)}
        last_used_date={Map.get(@last_used_dates, pack_id)}
        current_user={@current_user}
      >
        <button
          type="button"
          class="btn btn-primary"
          phx-click="toggle_staged"
          phx-value-pack_id={pack.id}
          data-confirm={"#{dgettext("prompts", "Are you sure you want to unstage this ammo?")}"}
        >
          <%= if pack.staged,
            do: dgettext("actions", "Unstage from range"),
            else: dgettext("actions", "Stage for range") %>
        </button>

        <.link
          patch={Routes.range_index_path(Endpoint, :add_shot_group, pack)}
          class="btn btn-primary"
        >
          <%= dgettext("actions", "Record shots") %>
        </.link>
      </.pack_card>
    </div>
  <% end %>

  <hr class="hr" />

  <%= if @shot_groups |> Enum.empty?() and @search |> is_nil() do %>
    <h1 class="title text-xl text-primary-600">
      <%= gettext("No shots recorded") %>
      <%= display_emoji("😔") %>
    </h1>
  <% else %>
    <h1 class="title text-2xl text-primary-600">
      <%= gettext("Shot log") %>
    </h1>

    <canvas
      id="shot-log-chart"
      phx-hook="ShotLogChart"
      phx-update="ignore"
      class="max-h-72"
      data-chart-data={Jason.encode!(@chart_data)}
      data-label={gettext("Rounds shot")}
      data-color={random_color()}
      aria-label={gettext("Rounds shot chart")}
      role="img"
    >
      <%= dgettext("errors", "Your browser does not support the canvas element.") %>
    </canvas>

    <div class="w-full flex flex-col sm:flex-row justify-center items-center space-y-4 sm:space-y-0 sm:space-x-4 max-w-2xl">
      <.form
        :let={f}
        for={%{}}
        as={:ammo_type}
        phx-change="change_class"
        phx-submit="change_class"
        class="flex items-center"
      >
        <%= label(f, :class, gettext("Class"), class: "title text-primary-600 text-lg text-center") %>

        <%= select(
          f,
          :class,
          [
            {gettext("All"), :all},
            {gettext("Rifle"), :rifle},
            {gettext("Shotgun"), :shotgun},
            {gettext("Pistol"), :pistol}
          ],
          class: "mx-2 my-1 min-w-md input input-primary",
          value: @class
        ) %>
      </.form>

      <.form
        :let={f}
        for={%{}}
        as={:search}
        phx-change="search"
        phx-submit="search"
        class="grow flex items-center"
      >
        <%= text_input(f, :search_term,
          class: "grow input input-primary",
          value: @search,
          role: "search",
          phx_debounce: 300,
          placeholder: gettext("Search shot records")
        ) %>
      </.form>
    </div>

    <%= if @shot_groups |> Enum.empty?() do %>
      <h1 class="title text-xl text-primary-600">
        <%= gettext("No shots recorded") %>
        <%= display_emoji("😔") %>
      </h1>
    <% else %>
      <.live_component
        module={CanneryWeb.Components.ShotGroupTableComponent}
        id="shot_groups_index_table"
        shot_groups={@shot_groups}
        current_user={@current_user}
      >
        <:actions :let={shot_group}>
          <div class="px-4 py-2 space-x-4 flex justify-center items-center">
            <.link
              patch={Routes.range_index_path(Endpoint, :edit, shot_group)}
              class="text-primary-600 link"
              aria-label={
                dgettext("actions", "Edit shot record of %{shot_group_count} shots",
                  shot_group_count: shot_group.count
                )
              }
            >
              <i class="fa-fw fa-lg fas fa-edit"></i>
            </.link>

            <.link
              href="#"
              class="text-primary-600 link"
              phx-click="delete"
              phx-value-id={shot_group.id}
              data-confirm={
                dgettext("prompts", "Are you sure you want to delete this shot record?")
              }
              aria-label={
                dgettext("actions", "Delete shot record of %{shot_group_count} shots",
                  shot_group_count: shot_group.count
                )
              }
            >
              <i class="fa-fw fa-lg fas fa-trash"></i>
            </.link>
          </div>
        </:actions>
      </.live_component>
    <% end %>
  <% end %>
</div>

<%= case @live_action do %>
  <% :edit -> %>
    <.modal return_to={Routes.range_index_path(Endpoint, :index)}>
      <.live_component
        module={CanneryWeb.RangeLive.FormComponent}
        id={@shot_group.id}
        title={@page_title}
        action={@live_action}
        shot_group={@shot_group}
        return_to={Routes.range_index_path(Endpoint, :index)}
        current_user={@current_user}
      />
    </.modal>
  <% :add_shot_group -> %>
    <.modal return_to={Routes.range_index_path(Endpoint, :index)}>
      <.live_component
        module={CanneryWeb.Components.AddShotGroupComponent}
        id={:new}
        title={@page_title}
        action={@live_action}
        pack={@pack}
        return_to={Routes.range_index_path(Endpoint, :index)}
        current_user={@current_user}
      />
    </.modal>
  <% _ -> %>
<% end %>
