package eu.dnetlib.doiboost.crossref

import eu.dnetlib.dhp.client.AbstractRestClient
import org.json4s.{DefaultFormats, JValue}
import org.json4s.jackson.JsonMethods.{compact, parse, render}

class CrossrefFunderRetriever(var cursor:String = "*") extends AbstractRestClient{




  override def extractInfo(input: String): Unit = {
    implicit lazy val formats: DefaultFormats.type = org.json4s.DefaultFormats
    lazy val json: org.json4s.JValue = parse(input)
    buffer = (json \\ "items").extract[List[JValue]].map(s => compact(render(s)))
    cursor = (json \ "message" \ "next-cursor").extractOrElse[String](null)
    if (cursor.isEmpty)
      complete = true
    current_index = 0
  }

  def get_url(): String = {

    println(s"cursor is $cursor")
    s"https://api.crossref.org/funders?rows=1000&cursor=$cursor"

  }

  override def getBufferData(): Unit = {
    if (!complete) {
      val response =
        if (scroll_value.isDefined) doHTTPGETRequest(scroll_value.get)
        else doHTTPGETRequest(get_url())
      extractInfo(response)
    }
  }
}
