// 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
//
//      http://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.api.generator.engine.ast;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.stream.Collectors;

@AutoValue
public abstract class JavaDocComment implements Comment {

  public abstract ImmutableList<String> comments();

  public static Builder builder() {
    return new AutoValue_JavaDocComment.Builder();
  }

  public static JavaDocComment withComment(String comment) {
    return JavaDocComment.builder().addComment(comment).build();
  }

  @Override
  public String comment() {
    return String.join("\n", comments().stream().collect(Collectors.toList()));
  }

  public void accept(AstNodeVisitor visitor) {
    visitor.visit(this);
  }

  @AutoValue.Builder
  public abstract static class Builder {
    protected abstract ImmutableList.Builder<String> commentsBuilder();

    public Builder setDeprecated(String deprecatedText) {
      commentsBuilder().add(String.format("@deprecated %s", deprecatedText));
      return this;
    }

    public Builder addComment(String comment) {
      commentsBuilder().add(comment);
      return this;
    }

    public Builder addParam(String name, String description) {
      String parameter = String.format("@param %s %s", name, description);
      commentsBuilder().add(parameter);
      return this;
    }

    public Builder setThrows(String type, String description) {
      String throwsText = String.format("@throws %s %s", type, description);
      commentsBuilder().add(throwsText);
      return this;
    }

    public Builder addSampleCode(String sampleCode) {
      commentsBuilder().add("<pre><code>");
      String[] sampleLines = sampleCode.split("\\r?\\n");
      for (String sampleLine : sampleLines) {
        commentsBuilder().add(sampleLine);
      }
      commentsBuilder().add("</code></pre>");
      return this;
    }

    public Builder addParagraph(String paragraph) {
      commentsBuilder().add(String.format("<p> %s", paragraph));
      return this;
    }

    public Builder addOrderedList(List<String> oList) {
      commentsBuilder().add("<ol>");
      for (int i = 0; i < oList.size(); i++) {
        commentsBuilder().add(String.format("<li> %s", oList.get(i)));
      }
      commentsBuilder().add("</ol>");
      return this;
    }

    public Builder addUnorderedList(List<String> uList) {
      commentsBuilder().add("<ul>");
      for (int i = 0; i < uList.size(); i++) {
        commentsBuilder().add(String.format("<li> %s", uList.get(i)));
      }
      commentsBuilder().add("</ul>");
      return this;
    }

    public abstract JavaDocComment build();
  }
}

@@ -712,24 +712,49 @@
 utableList;%0A
+import java.util.Arrays;%0A
 import java.
@@ -2336,45 +2336,76 @@
     
-for (String sampleLine : sampleLines)
+Arrays.stream(sampleLines)%0A          .forEach(%0A              line -%3E
  %7B%0A 
@@ -2403,32 +2403,40 @@
 ne -%3E %7B%0A        
+  
+      
 commentsBuilder(
@@ -2441,23 +2441,17 @@
 r().add(
-sampleL
+l
 ine);%0A  
@@ -2446,33 +2446,43 @@
 dd(line);%0A      
-%7D
+        %7D);
 %0A      commentsB
@@ -2784,49 +2784,72 @@
     
-for (int i = 0; i %3C oList.size(); i++) %7B%0A
+oList.stream()%0A          .forEach(%0A              s -%3E %7B%0A        
     
@@ -2903,31 +2903,30 @@
 s%22, 
-oList.get(i)));%0A      %7D
+s));%0A              %7D);
 %0A   
@@ -3091,49 +3091,72 @@
     
-for (int i = 0; i %3C uList.size(); i++) %7B%0A
+uList.stream()%0A          .forEach(%0A              s -%3E %7B%0A        
     
@@ -3210,31 +3210,30 @@
 s%22, 
-uList.get(i)));%0A      %7D
+s));%0A              %7D);
 %0A   

use stream