defmodule ZoeyscomputerWeb.ImageLive.FormComponent do
  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"
      >
        <.input field={@form[:file]} type="text" label="File" />
        <:actions>
          <.button phx-disable-with="Saving...">Save Image</.button>
        </:actions>
      </.simple_form>
    </div>
    """
  end

  @impl true
  def update(%{image: image} = assigns, socket) do
    {: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

  def handle_event("save", %{"image" => image_params}, socket) do
    save_image(socket, socket.assigns.action, image_params)
  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 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
