// 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.datatypes;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.cloud.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;
import org.springframework.data.annotation.Transient;

/**
 * Contains the information retrieved from a single build bot. It is used as a member
 * of {@link BuildInfo}
 */
@Entity(name = "builder")
@JsonIgnoreProperties(ignoreUnknown = true)
public class BuildBotData {

  /**
   * The commit hash, it is fetched from the BuildBot API.
   * This field is transient because it is only needed to
   * determine which commit this data belongs to and put
   * it in the right {@link BuildInfo} in the storage.
   * Used in {@link com.google.graphgeckos.dashboard.storage.DatastoreRepository}.
   */
  @Transient
  private String commitHash;

  /* The timestamp of the build. */
  private Timestamp timestamp;

  /* Name of the buildbot. */
  private String name;

  /**
   * The logs of each compilation stage, stored as described by {@link Log}.
   */
  private final List<Log> logs = new ArrayList<>();

  /**
   * Builder compilation status, as described by {@link BuilderStatus}.
   */
  private BuilderStatus status;

  /**
   * Extracts nested commitHash("revision"), branch("branch") and timestamp("when")
   * fields from the json.
   * @param sourceStamp Representation of the json component, where the commitHash field is located.
   */
  @JsonProperty("sourceStamp")
  public void unpackSourceStamp(Map<String, Object> sourceStamp) {
    commitHash = sourceStamp.get("revision").toString();
    timestamp = Timestamp.of(new Date(sourceStamp.get("when").toString()));
  }

  /**
   * Defines builder status based on a phrase provided in parsed json.
   * If something failed, then the buildbot is considered failed {@code FAILED}.
   * If something is lost, then the buildbot is considered lost {@code LOST}.
   * Otherwise the buildbot is considered passed {@code PASSED}.
   * @param words Words of the "text" JSON field.
   */
  @JsonProperty("text")
  private void extractStatus(List<String> words) {
    for (String word : words) {
      if (word.equals("failed")) {
        status = BuilderStatus.FAILED;
        break;
      }
      if (word.equals("lost")) {
        status = BuilderStatus.LOST;
        break;
      }
      if (word.equals("successful")) {
        status = BuilderStatus.PASSED;
      }
    }
  }

  /**
   * Unpacks logs represented as list of lists of two strings,
   * where the first one is a type of the log and the second one
   * is a link to the log.
   * @param logs Representation of the json component, where the logs are located.
   */
  @JsonProperty("logs")
  private void unpackLogs(List<String[]> logs) {
    logs.forEach(x -> this.logs.add(new Log(x)));
  }

  public BuildBotData(@JsonProperty("builderName") String name) {
    this.name = name;
  }

  public BuildBotData(String commitHash, String name, List<Log> logs, BuilderStatus status) {
    this.commitHash = commitHash;
    this.name = name;
    this.logs.addAll(logs);
    this.status = status;
  }

  public BuildBotData() {}

  /**
   * Returns the name of the builder bot. Cannot be null.
   */
  public String getName() {
    return name;
  }

  /**
   * Returns the list of logs for each compilation stage. Cannot be null.
   */
  public List<Log> getLogs() {
    return logs;
  }

  /**
   * Returns the compilation status of the builder. Cannot be null.
   */
  public BuilderStatus getStatus() {
    return status;
  }

  /**
   * Returns the commit hash of the commit tested by the current buildbot.
   */
  public String getCommitHash() {
    return commitHash;
  }

  /**
   * Returns the timestamp of the build.
   */
  public Timestamp getTimestamp() {
    return timestamp;
  }

}

@@ -639,124 +639,8 @@
 s;%0A%0A
-import com.fasterxml.jackson.annotation.JsonIgnoreProperties;%0Aimport com.fasterxml.jackson.annotation.JsonProperty;%0A
 impo
@@ -670,16 +670,16 @@
 estamp;%0A
+
 import j
@@ -702,31 +702,8 @@
 st;%0A
-import java.util.Date;%0A
 impo
@@ -725,30 +725,8 @@
 st;%0A
-import java.util.Map;%0A
 impo
@@ -797,62 +797,8 @@
 ity;
-%0Aimport org.springframework.data.annotation.Transient;
 %0A%0A/*
@@ -943,52 +943,8 @@
 r%22)%0A
-@JsonIgnoreProperties(ignoreUnknown = true)%0A
 publ
@@ -1296,21 +1296,8 @@
  */%0A
-  @Transient%0A
   pr
@@ -1662,32 +1662,32 @@
 rStatus%7D.%0A   */%0A
+
   private Builde
@@ -1707,1690 +1707,8 @@
 s;%0A%0A
-  /**%0A   * Extracts nested commitHash(%22revision%22), branch(%22branch%22) and timestamp(%22when%22)%0A   * fields from the json.%0A   * @param sourceStamp Representation of the json component, where the commitHash field is located.%0A   */%0A  @JsonProperty(%22sourceStamp%22)%0A  public void unpackSourceStamp(Map%3CString, Object%3E sourceStamp) %7B%0A    commitHash = sourceStamp.get(%22revision%22).toString();%0A    timestamp = Timestamp.of(new Date(sourceStamp.get(%22when%22).toString()));%0A  %7D%0A%0A  /**%0A   * Defines builder status based on a phrase provided in parsed json.%0A   * If something failed, then the buildbot is considered failed %7B@code FAILED%7D.%0A   * If something is lost, then the buildbot is considered lost %7B@code LOST%7D.%0A   * Otherwise the buildbot is considered passed %7B@code PASSED%7D.%0A   * @param words Words of the %22text%22 JSON field.%0A   */%0A  @JsonProperty(%22text%22)%0A  private void extractStatus(List%3CString%3E words) %7B%0A    for (String word : words) %7B%0A      if (word.equals(%22failed%22)) %7B%0A        status = BuilderStatus.FAILED;%0A        break;%0A      %7D%0A      if (word.equals(%22lost%22)) %7B%0A        status = BuilderStatus.LOST;%0A        break;%0A      %7D%0A      if (word.equals(%22successful%22)) %7B%0A        status = BuilderStatus.PASSED;%0A      %7D%0A    %7D%0A  %7D%0A%0A  /**%0A   * Unpacks logs represented as list of lists of two strings,%0A   * where the first one is a type of the log and the second one%0A   * is a link to the log.%0A   * @param logs Representation of the json component, where the logs are located.%0A   */%0A  @JsonProperty(%22logs%22)%0A  private void unpackLogs(List%3CString%5B%5D%3E logs) %7B%0A    logs.forEach(x -%3E this.logs.add(new Log(x)));%0A  %7D%0A%0A  public BuildBotData(@JsonProperty(%22builderName%22) String name) %7B%0A    this.name = name;%0A  %7D%0A%0A
   pu

remove unused json parsing functionality