// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.graphgeckos.dashboard.fetchers.github;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.util.Preconditions;
import com.google.graphgeckos.dashboard.datatypes.GitHubData;
import com.google.graphgeckos.dashboard.storage.DatastoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.NonNull;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.logging.Logger;

public class GitHubClient {

  /** Provides access to the storage. */
  @Autowired
  private DatastoreRepository datastoreRepository;

  private String baseUrl;

  private static final Logger logger = Logger.getLogger(GitHubClient.class.getName());

  private GitHubClient(@NonNull String baseUrl) {
    this.baseUrl = Preconditions.checkNotNull(baseUrl);
  }

  public GitHubClient() {
    this("https://api.github.com/repos/llvm/llvm-project/commits/master");
  }

  public void run(@NonNull long delay) {

    logger.info(String.format("GitHub: started fetching from the base url: %s",
                              baseUrl));     
    WebClient.builder().baseUrl(baseUrl)
      .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/json")
      .build()
      .get()
      .accept(MediaType.APPLICATION_JSON)
      .retrieve()
      .bodyToFlux(GitHubData.class)
      .delaySubscription(Duration.ofSeconds(delay))
      .onErrorResume(e -> {
        logger.info("Ignoring error: " + e.getMessage());
        return Mono.empty();
      })
      .repeat()
      .subscribe(response -> {
        if (response == null) {
          logger.info(
            String.format("GitHub: Error occurred, waiting for %d seconds", delay));
          return;
        }

        logger.info(String.format("GitHub: trying to deserialize valid JSON"));
        try {
          //GitHubData gitHubData = new ObjectMapper().readValue(response, GitHubData.class);
          datastoreRepository.createRevisionEntry(response);
        } catch (Exception e) {
          logger.info(String.format("GitHub: can't deserialize JSON"));
          e.printStackTrace();
        }
        logger.info(
               String.format(
                 "GitHub: Performing re-request in %d seconds", delay));
      });
  }

  public void setBaseUrl(String baseUrl) {
    this.baseUrl = baseUrl;
  }

}

@@ -1381,21 +1381,17 @@
  String 
-baseU
+u
 rl;%0A%0A  p
@@ -1530,29 +1530,25 @@
  %7B%0A    this.
-baseU
+u
 rl = Precond
@@ -1844,23 +1844,14 @@
     
-baseUrl));     
+url));
 %0A   
@@ -1879,21 +1879,17 @@
 baseUrl(
-baseU
+u
 rl)%0A    
@@ -1975,24 +1975,40 @@
      .get()%0A
+      .uri(url)%0A
       .accep
@@ -2072,12 +2072,12 @@
 dyTo
-Flux
+Mono
 (Git
@@ -2486,402 +2486,66 @@
   %7D%0A
-%0A        logger.info(String.format(%22GitHub: trying to deserialize valid JSON%22));%0A        try %7B%0A          //GitHubData gitHubData = new ObjectMapper().readValue(response, GitHubData.class);%0A          datastoreRepository.createRevisionEntry(response);%0A        %7D catch (Exception e) %7B%0A          logger.info(String.format(%22GitHub: can't deserialize JSON%22));%0A          e.printStackTrace();%0A        %7D
+        datastoreRepository.createRevisionEntry(response);
 %0A   
@@ -2708,29 +2708,25 @@
 eUrl(String 
-baseU
+u
 rl) %7B%0A    th
@@ -2732,23 +2732,15 @@
 his.
-baseUrl = baseU
+url = u
 rl;%0A

add uri