   import Serum.Util
   alias Serum.Error
   alias Serum.Build
+  alias Serum.PostInfo
   alias Serum.Renderer
 
   @type state :: Build.state

  end

  @doc false
  @spec tag_task({Serum.Tag.t, [Serum.PostInfo.t]}, binary, state) :: :ok

  def tag_task({tag, posts}, dest, state) do
    tagdir = Path.join [dest, "tags", tag.name]

      error -> error
    end
  end

  """
  # for the first page
  path_index = Path.join state.list_dir, "index.html"
  fwrite path_index, html
  msg_gen path_index

  path = Path.join state.list_dir, "page-1.html"
  fwrite path, html
  msg_gen path

  # for other pages
  path = Path.join state.list_dir, "page-# {page_num}.html"
  fwrite path, html
  msg_gen path
  """

  @spec render_lists([[PostInfo.t]], integer, [Error.result(binary)], state)
    :: Error.result([binary])

  defp render_lists(paginated_posts, page_num, acc, state)

  defp render_lists([page], 1, _acc, state) do
    # The only page
    rendered = render_list page, 1, true, true, state
    [rendered]
    |> Error.filter_results_with_values(:render_lists)
  end

  defp render_lists([page], page_num, acc, state) do
    # The last page
    rendered = render_list page, page_num, false, true, state
    [rendered|acc]
    |> Enum.reverse()
    |> Error.filter_results_with_values(:render_lists)
  end

  defp render_lists([page|rest], 1, acc, state) do
    # The first page
    rendered = render_list page, 1, true, false, state
    render_lists rest, 2, [rendered|acc], state
  end

  defp render_lists([page|rest], page_num, acc, state) do
    # Other pages
    rendered = render_list page, page_num, false, false, state
    render_lists rest, page_num + 1, [rendered|acc], state
  end

  @spec render_list([PostInfo.t], integer, boolean, boolean, state)
    :: Error.result(binary)

  defp render_list(posts, page_num, first?, last?, state) do
    base = state.project_info.base_url
    path = state.list_path
    prev = if first?, do: Path.join([base, path, "page-#{page_num - 1}.html"])
    next = if last?, do: Path.join([base, path, "page-#{page_num + 1}.html"])
    list_ctx = [
      header: state.list_title,
      posts: posts,
      current_page: page_num,
      max_page: state.max_page,
      prev_page: prev,
      next_page: next
    ]
    Renderer.render "list", list_ctx, [page_title: state.list_title], state
  end
end
