   alias Neoscan.Vouts.Vout
   alias NeoscanMonitor.Api
   alias Neoscan.Blocks.Block
+
+  require Logger
 
   #total amount of available NEO
   def total_neo, do: 100_000_000

    []
  end
  def route_if_there_is_unclaimed_but_dont_add(unclaimed) do
    range = get_unclaimed_block_range(unclaimed)

    blocks_with_gas = get_blocks_gas(range)
                      |> Enum.sort_by(fn %{:index => index} -> index end)
                      |> check_blocks(range)

    Enum.map(
      unclaimed,
      fn %{:value => value} = vout -> Map.merge(vout, %{
          :unclaimed => compute_vout_bonus(vout, blocks_with_gas),
          :value => round(value),
        }) end
    )

  end

  def check_blocks(blocks, {min, max}) do
    count = max - min + 1

    cond do
      Enum.count(blocks) == count ->
        blocks
      true ->
        get_missing_block(blocks, {min, max})
    end
  end

  def verified_blocks(range) do
    blocks_with_gas = get_blocks_gas(range)
                      |> Enum.sort_by(fn %{:index => index} -> index end)
                      |> check_blocks(range)

    case blocks_with_gas do
      false ->
        verified_blocks(range)
      _ ->
        blocks_with_gas
    end
  end


  def get_missing_block(blocks, {min, _max}) do
    blocks
    |> Enum.reduce_while(min, fn (%{:index => index}, acc) ->
      cond do
        index == acc ->
          {:cont, index + 1}
        true ->
          Logger.info("Block #{acc} missing")
          Repair.get_and_add_missing_block_from_height(acc)
          {:halt, false}
      end
    end)
  end
