defmodule ZoeyscomputerWeb.ImageLive.FormComponent do
  import Mogrify
  alias Zoeyscomputer.IdGenerator
  use ZoeyscomputerWeb, :live_component

  alias Zoeyscomputer.Images

  @impl true
  def render(assigns) do
    ~H"""
    <div>
      <.header>
        <%= @title %>
        <:subtitle>Use this form to manage image records in your database.</:subtitle>
      </.header>

      <.simple_form
        for={@form}
        id="image-form"
        phx-target={@myself}
        phx-change="validate"
        phx-submit="save"
      >
        <.live_file_input upload={@uploads.image} />
        <section phx-drop-target={@uploads.image.ref}>
          <%= for entry <- @uploads.image.entries do %>
            <article class="upload-entry">
              <figure>
                <.live_img_preview entry={entry} />
                <figcaption><%= entry.client_name %></figcaption>
              </figure>
              <progress class="w-full" value={entry.progress} max="100">
                <%= entry.progress %>
              </progress>
            </article>
          <% end %>

          <%= for err <- upload_errors(@uploads.image) do %>
            <p class="alert alert-danger"><%= error_to_string(err) %></p>
          <% end %>
        </section>
        <:actions>
          <.button phx-disable-with="Saving...">Save Image</.button>
        </:actions>
      </.simple_form>
    </div>
    """
  end

  defp error_to_string(:too_large), do: "Too Large"
  defp error_to_string(:not_accepted), do: "Not Accepted"

  defp error_to_string(:too_manu_files), do: "Too many files!"

  @impl true
  def update(%{image: image} = assigns, socket) do
    socket =
      socket
      |> assign(:uploaded_files, [])
      |> allow_upload(:image, accept: ~w(.jpg .jpeg .png), max_entries: 2)

    {:ok,
     socket
     |> assign(assigns)
     |> assign_new(:form, fn ->
       to_form(Images.change_image(image))
     end)}
  end

  @impl true
  def handle_event("validate", %{"image" => image_params}, socket) do
    changeset = Images.change_image(socket.assigns.image, image_params)
    {:noreply, assign(socket, form: to_form(changeset, action: :validate))}
  end

  @impl true
  def handle_event("save", _params, socket) do
    uploaded_files =
      consume_uploaded_entries(socket, :image, fn %{path: path}, _entry ->
        dest = Path.join("/tmp/", Path.basename(path))
        File.cp(path, dest)

        handle_upload(dest)
      end)

    update(socket, :uploaded_files, &(&1 ++ uploaded_files))

    save_image(socket, socket.assigns.action, %{file: uploaded_files})
  end

  @impl true
  def handle_event("validate", _params, socket) do
    {:noreply, socket}
  end

  @impl true
  def handle_event("cancel-upload", %{"ref" => ref}, socket) do
    {:noreply, cancel_upload(socket, :image, ref)}
  end

  defp save_image(socket, :edit, image_params) do
    case Images.update_image(socket.assigns.image, image_params) do
      {:ok, image} ->
        notify_parent({:saved, image})

        {:noreply,
         socket
         |> put_flash(:info, "Image updated successfully")
         |> push_patch(to: socket.assigns.patch)}

      {:error, %Ecto.Changeset{} = changeset} ->
        {:noreply, assign(socket, form: to_form(changeset))}
    end
  end

  defp handle_upload(path) do
    id = IdGenerator.generate()
    key = "uploads/#{id}.png"
    bucket = "imgs"

    try do
      image = open(path) |> format("png") |> save()
      IO.inspect(image)

      {:ok, new_file_binary} = File.read(image.path)

      case ExAws.S3.put_object(bucket, key, new_file_binary, %{content_type: "image/png"})
           |> ExAws.request() do
        {:ok, _response} -> {:ok, id}
        {:error, reason} -> {:error, reason}
      end
    after
      File.rm(path)
    end
  end

  defp save_image(socket, :new, image_params) do
    case Images.create_image(image_params) do
      {:ok, image} ->
        notify_parent({:saved, image})

        {:noreply,
         socket
         |> put_flash(:info, "Image created successfully")
         |> push_patch(to: socket.assigns.patch)}

      {:error, %Ecto.Changeset{} = changeset} ->
        {:noreply, assign(socket, form: to_form(changeset))}
    end
  end

  defp notify_parent(msg), do: send(self(), {__MODULE__, msg})
end
