/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2019 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.tools.changelog;

import com.axelor.common.StringUtils;
import java.text.MessageFormat;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

public class ReleaseGenerator {

  private static final String NEW_LINE = System.lineSeparator();

  public String generate(Release release) {
    StringBuilder releaseContent = new StringBuilder();

    appendHeader(releaseContent, release);
    appendEntries(releaseContent, release);

    return releaseContent.toString();
  }

  private void appendEntries(StringBuilder content, Release release) {
    if (release.getEntries() == null) {
      return;
    }
    SortedMap<String, EntryType> sortedTypes = new TreeMap<>();
    for (EntryType type : EntryType.values()) {
      sortedTypes.put(type.getValue(), type);
    }

    for (EntryType type : sortedTypes.values()) {
      if (release.getEntries().containsKey(type)) {
        appendEntriesPerType(content, type, release.getEntries().get(type));
      }
    }
  }

  private void appendEntriesPerType(
      StringBuilder content, EntryType type, List<ChangelogEntry> entries) {
    if (entries == null || entries.isEmpty()) {
      return;
    }
    content.append("#### ").append(type.getValue()).append(NEW_LINE).append(NEW_LINE);
    for (ChangelogEntry entry : entries) {
      content.append(MessageFormat.format("* {0}", entry.getTitle())).append(NEW_LINE);
      if (!StringUtils.isEmpty(entry.getDescription())) {
        content
            .append(NEW_LINE)
            .append(MessageFormat.format("<details>{0}{1}{2}</details>", NEW_LINE, entry.getDescription(), NEW_LINE))
            .append(NEW_LINE)
            .append(NEW_LINE);
      }
    }
    content.append(NEW_LINE);
  }

  private void appendHeader(StringBuilder content, Release release) {
    content
        .append(MessageFormat.format("## {0} ({1})", release.getVersion(), release.getDate()))
        .append(NEW_LINE)
        .append(NEW_LINE);
  }
}

@@ -804,16 +804,44 @@
 Format;%0A
+import java.util.ArrayList;%0A
 import j
@@ -2205,176 +2205,369 @@
     
-content%0A            .append(NEW_LINE)%0A            .append(MessageFormat.format(%22%3Cdetails%3E%7B0%7D%7B1%7D%7B2%7D%3C/details%3E%22, NEW_LINE, entry.getDescription(), NEW_LINE))%0A            
+List%3CString%3E lines = new ArrayList%3C%3E();%0A        lines.add(%22  %3Cdetails%3E%22);%0A        for (String line : entry.getDescription().trim().split(%22%5Cn%22)) %7B%0A          lines.add(StringUtils.isBlank(line) ? %22%22 : %22  %22 + line);%0A        %7D%0A        lines.add(%22  %3C/details%3E%22);%0A        String details = String.join(NEW_LINE, lines);%0A        content.append(NEW_LINE).append(details)
 .app
@@ -2583,21 +2583,8 @@
 INE)
-%0A            
 .app

Indent change log details