defmodule LastFm.API do
  alias LastFm.API.{ErrorResponse, Signature}
  alias LastFm.{Artist, Session, Track}

  require Logger

  @type id_or_name :: {:musicbrainz_id, String.t()} | {:name, String.t()}

  @spec get_session(String.t(), LastFm.Config.t()) :: {:ok, Session.t()} | {:error, term()}
  def get_session(token, config) do
    params =
      %{
        api_key: config.api_key,
        method: "auth.getSession",
        token: token
      }

    signature = Signature.generate(params, config.shared_secret)

    params = Map.put(params, "api_sig", signature)

    config
    |> new_request()
    |> Req.merge(url: "/", params: params)
    |> Req.Request.append_response_steps(parse_error: &parse_error/1)
    |> Req.Request.append_response_steps(parse_session: &parse_session/1)
    |> get_request()
  end

  @spec scrobble([map()], String.t(), LastFm.Config.t()) :: {:ok, map()} | {:error, term()}
  def scrobble(tracks, session_key, config) do
    params =
      %{"api_key" => config.api_key, "method" => "track.scrobble", "sk" => session_key}

    track_params =
      tracks
      |> Enum.with_index()
      |> Enum.flat_map(fn {track, index} ->
        track
        |> Enum.map(fn {key, value} ->
          {"#{key}[#{index}]", value}
        end)
      end)
      |> Map.new()

    params = Map.merge(params, track_params)

    signature = Signature.generate(params, config.shared_secret)

    body = Map.merge(params, %{"api_sig" => signature, "format" => "json"})

    config
    |> new_request()
    |> Req.merge(url: "/", form: body, receive_timeout: 5_000)
    |> post_request()
  end

  @spec get_recent_tracks(keyword(), LastFm.Config.t()) :: {:ok, [Track.t()]} | {:error, term()}
  def get_recent_tracks(opts \\ [], config) do
    to_uts = Keyword.get(opts, :to_uts)
    limit = Keyword.get(opts, :limit, 100)

    params =
      config
      |> base_params()
      |> Keyword.merge(method: "user.getrecenttracks", limit: limit)

    params = if to_uts, do: Keyword.put(params, :to, to_uts), else: params

    config
    |> new_request()
    |> Req.merge(url: "/", params: params)
    |> Req.Request.append_response_steps(parse_tracks: &parse_tracks/1)
    |> get_request()
  end

  @spec get_artist_info(id_or_name(), LastFm.Config.t()) :: {:ok, Artist.t()} | {:error, term()}
  def get_artist_info(id_or_name_option, config) do
    params =
      config
      |> base_params()
      |> Keyword.put(:method, "artist.getInfo")
      |> put_musicbrainz_id_or_name(id_or_name_option)

    config
    |> new_request()
    |> Req.merge(url: "/", params: params)
    |> Req.Request.append_response_steps(parse_tracks: &parse_artist/1)
    |> get_request()
  end

  @spec get_similar_artists(id_or_name(), LastFm.Config.t()) ::
          {:ok, [Artist.t()]} | {:error, term()}
  def get_similar_artists(id_or_name_option, config) do
    params =
      config
      |> base_params()
      |> Keyword.merge(limit: 100, method: "artist.getSimilar")
      |> put_musicbrainz_id_or_name(id_or_name_option)

    config
    |> new_request()
    |> Req.merge(url: "/", params: params)
    |> Req.Request.append_response_steps(parse_tracks: &parse_similar_artists/1)
    |> get_request()
  end

  @spec get_artist_tags(id_or_name(), LastFm.Config.t()) ::
          {:ok, [{String.t(), integer()}]} | {:error, term()}
  def get_artist_tags(id_or_name_option, config) do
    params =
      config
      |> base_params()
      |> Keyword.put(:method, "artist.getTopTags")
      |> put_musicbrainz_id_or_name(id_or_name_option)

    config
    |> new_request()
    |> Req.merge(url: "/", params: params)
    |> Req.Request.append_response_steps(parse_tags: &parse_artist_tags/1)
    |> get_request()
  end

  @spec get_user_info(String.t(), LastFm.Config.t()) :: {:ok, String.t()} | {:error, term()}
  def get_user_info(session_key, config) do
    params =
      %{
        api_key: config.api_key,
        method: "user.getInfo",
        sk: session_key,
        format: "json"
      }

    config
    |> new_request()
    |> Req.merge(url: "/", params: params)
    |> Req.Request.append_response_steps(parse_user_info: &parse_user_info/1)
    |> get_request()
  end

  defp put_musicbrainz_id_or_name(params, {:musicbrainz_id, musicbrainz_id}) do
    Keyword.put(params, :mbid, musicbrainz_id)
  end

  defp put_musicbrainz_id_or_name(params, {:name, name}) do
    Keyword.put(params, :artist, name)
  end

  defp base_params(config) do
    [
      user: config.user,
      api_key: config.api_key,
      format: "json"
    ]
  end

  defp new_request(config) do
    Req.new(
      base_url: "https://ws.audioscrobbler.com/2.0/",
      # Experimental: metrics show some long running requests
      # that end up hitting timeouts (at default values),
      # so we make them shorter to leverage retries
      max_retries: 1,
      pool_timeout: 10_000,
      # the maximum number of milliseconds that a pool can be idle before
      # being terminated, Setting it to a low value so that it gets
      # terminated and we don't get failures after the vm resumes.
      pool_max_idle_time: 10_000,
      receive_timeout: 2500,
      connect_options: [
        timeout: 2500
      ],
      user_agent: config.user_agent
    )
    |> Req.Request.merge_options(config.req_options)
    |> Req.RateLimiter.attach(name: :last_fm, cooldown: config.api_cooldown)
    |> Req.Request.put_private(:api_key, config.api_key)
    |> Req.Request.append_request_steps(log_attempt: &log_attempt/1)
    |> Req.Request.append_response_steps(parse_error: &parse_error/1)
  end

  defp get_request(request) do
    case Req.get(request) do
      {:ok, %{body: %ErrorResponse{} = error_response}} ->
        {:error, error_response.error}

      {:ok, response} ->
        {:ok, response.body}

      error ->
        error
    end
  end

  defp post_request(request) do
    case Req.post(request) do
      {:ok, %{body: %ErrorResponse{} = error_response}} ->
        {:error, error_response.error}

      {:ok, response} ->
        {:ok, response.body}

      error ->
        error
    end
  end

  defp log_attempt(request) do
    url = URI.to_string(request.url)
    api_key = Req.Request.get_private(request, :api_key)
    Logger.debug("Fetching data from #{sanitize_url(url, api_key)}")
    request
  end

  defp parse_session({request, response}) do
    session = Session.parse(response.body)
    {request, Map.put(response, :body, session)}
  end

  defp parse_error({request, response}) do
    case response.body do
      %{"error" => error_number, "message" => message} ->
        error = ErrorResponse.new(error_number, message)

        Logger.error(fn ->
          url = URI.to_string(request.url)
          api_key = Req.Request.get_private(request, :api_key)
          "Failed to fetch data from #{sanitize_url(url, api_key)}, reason: #{message}."
        end)

        Req.Request.halt(request, Map.put(response, :body, error))

      _other ->
        {request, response}
    end
  end

  defp parse_tracks({request, response}) do
    tracks =
      response.body
      |> get_in(["recenttracks", "track"])
      |> Track.from_api_response()

    {request, Map.put(response, :body, tracks)}
  end

  defp parse_artist({request, response}) do
    artist =
      response.body
      |> Map.get("artist")
      |> Artist.from_api_response()

    {request, Map.put(response, :body, artist)}
  end

  defp parse_similar_artists({request, response}) do
    artists =
      response.body
      |> get_in(["similarartists", "artist"])
      |> Enum.map(&Artist.from_api_response/1)

    {request, Map.put(response, :body, artists)}
  end

  defp parse_artist_tags({request, response}) do
    tags =
      case get_in(response.body, ["toptags", "tag"]) do
        nil ->
          []

        tags when is_list(tags) ->
          tags
          |> Enum.map(fn tag -> {tag["name"], parse_tag_count(tag["count"])} end)
          |> Enum.filter(fn {_name, count} -> count >= 2 end)
          |> Enum.sort_by(&elem(&1, 1), :desc)
          |> Enum.take(15)
      end

    {request, Map.put(response, :body, tags)}
  end

  defp parse_tag_count(count) when is_integer(count), do: count

  defp parse_tag_count(count) when is_binary(count) do
    case Integer.parse(count) do
      {n, _} -> n
      :error -> 0
    end
  end

  defp parse_tag_count(_), do: 0

  defp parse_user_info({request, response}) do
    username = get_in(response.body, ["user", "name"])
    {request, Map.put(response, :body, username)}
  end

  defp sanitize_url(url, api_key) do
    String.replace(url, api_key, "<redacted_api_key>")
  end
end
