package com.datahub.graphql;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static java.nio.charset.StandardCharsets.*;


@Slf4j
@Controller
public class GraphiQLController {

  private final String graphiqlHtml;

  public GraphiQLController() {
    Resource graphiQLResource = new ClassPathResource("graphiql/index.html");
    try (Reader reader = new InputStreamReader(graphiQLResource.getInputStream(), UTF_8)) {
      this.graphiqlHtml = FileCopyUtils.copyToString(reader);
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  @GetMapping(value = "/graphiql", produces = MediaType.TEXT_HTML_VALUE)
  @ResponseBody
  CompletableFuture<String> graphiQL() {
    return CompletableFuture.supplyAsync(() -> this.graphiqlHtml);
  }
}