/*
 * Copyright (c) 2013 Codearte
 */
package eu.codearte.fairyland.producer.text;

import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import eu.codearte.fairyland.producer.RandomGenerator;

import java.util.ArrayList;
import java.util.List;

import static eu.codearte.fairyland.TextUtils.joinWithSpace;
import static org.apache.commons.lang3.StringUtils.*;

public class Text {

  private final TextProducer textProducer;
  private final RandomGenerator random;

  private int limit = 0;

  public Text(TextProducer textProducer, RandomGenerator random) {
    this.random = random;
    this.textProducer = textProducer;
  }

  public Text limitedTo(int limit) {
    this.limit = limit;
    return this;
  }

  public String result(String result) {
    if (limit > 0)
      return left(result, limit);
    else
      return result;
  }

  public String loremIpsum() {
    return result(textProducer.getLoremIpsum());
  }

  public String word() {
    return result(word(3));
  }

  public String word(int count) {
    return result(textProducer.cleanWords(count));
  }

  public String sentence() {
    return result(sentence(3));
  }

  public String sentence(int wordCount) {
    String randomWords = textProducer.rawWords(wordCount, 6);
    List<String> sentences = new ArrayList<>();
    for (String sentence : Splitter.on(". ").split(randomWords)) {
      sentences.add(capitalize(sentence));
    }
    String sentence = capitalize(Joiner.on(". ").join(sentences));
    sentence = removeEnd(sentence, ",");
    if (!endsWith(sentence, ".")) {
      sentence += ".";
    }
    return result(sentence);
  }

  private List<String> sentences(int sentenceCount) {
    List<String> sentences = new ArrayList<>(sentenceCount);
    for (int i = 0; i < sentenceCount; i++) {
      sentences.add(sentence());
    }
    return sentences;
  }

  public String paragraph(int sentenceCount) {
    return result(joinWithSpace(sentences(sentenceCount + random.randomBetween(1, 3))));
  }

  public String paragraph() {
    return result(paragraph(3));
  }

}

@@ -400,16 +400,373 @@
 Text %7B%0A%0A
+  private static final int DEFAULT_WORD_COUNT = 3;%0A  private static final int DEFAULT_WORD_COUNT_IN_SENTENCE = 3;%0A  private static final int DEFAULT_SENTENCE_COUNT = 3;%0A  private static final int WORD_COUNT_PRECISION_IN_SENTENCE = 6;%0A  private static final int SENTENCE_COUNT_PRECISION_MIN = 1;%0A  private static final int SENTENCE_COUNT_PRECISION_MAX = 3;%0A%0A
   privat
@@ -1347,17 +1347,34 @@
 lt(word(
-3
+DEFAULT_WORD_COUNT
 ));%0A  %7D%0A
@@ -1520,17 +1520,46 @@
 entence(
-3
+DEFAULT_WORD_COUNT_IN_SENTENCE
 ));%0A  %7D%0A
@@ -1659,17 +1659,48 @@
 dCount, 
-6
+WORD_COUNT_PRECISION_IN_SENTENCE
 );%0A    L
@@ -2284,32 +2284,121 @@
 sentences;%0A  %7D%0A%0A
+  public String paragraph() %7B%0A    return result(paragraph(DEFAULT_SENTENCE_COUNT));%0A  %7D%0A%0A
   public String 
@@ -2485,16 +2485,24 @@
 eCount +
+%0A       
  random.
@@ -2519,89 +2519,75 @@
 een(
-1, 3))));%0A  %7D%0A%0A  public String paragraph() %7B%0A    return result(paragraph(3
+SENTENCE_COUNT_PRECISION_MIN, SENTENCE_COUNT_PRECISION_MAX))
 ));%0A  %7D%0A
 %0A%7D%0A
@@ -2583,11 +2583,10 @@
 ));%0A  %7D%0A
-%0A
 %7D%0A

Extract magic numbers to constants