/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package com.celements.sajson;

import java.io.StringWriter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Stack;
import java.util.regex.Pattern;

import com.google.common.collect.ImmutableMap;

/**
 * Simple Api for JSON The JSON Builder helps do generate a valid JSON expression based on
 * event handling.
 *
 * @author fabian
 */

public class Builder {

  private static final Map<Pattern, String> JSON_REPLACEMENTS = ImmutableMap.of(Pattern.compile(
      "\\\\"), "\\\\\\\\", Pattern.compile("\""), "\\\\\"", Pattern.compile("\n"), "\\\\n",
      Pattern.compile("\r"), "\\\\r", Pattern.compile("\t"), "\\\\t");

  private Stack<ECommand> workerStack;
  private StringWriter jsonOutput;
  private boolean onFirstElement;

  public Builder() {
    workerStack = new Stack<>();
    jsonOutput = new StringWriter();
    onFirstElement = true;
  }

  public String getJSON() {
    if (!workerStack.isEmpty()) {
      throw new IllegalStateException(workerStack.peek() + " is still open.");
    }
    return jsonOutput.toString();
  }

  public void openArray() {
    checkNoDictionary();
    workerStack.push(ECommand.ARRAY_COMMAND);
    addOpeningPart("[");
    onFirstElement = true;
  }

  private void addOpeningPart(String openingPart) {
    if (!onFirstElement) {
      jsonOutput.append(", ");
    }
    jsonOutput.append(openingPart);
  }

  private void checkNoDictionary() {
    if (!workerStack.isEmpty() && (workerStack.peek() == ECommand.DICTIONARY_COMMAND)) {
      throw new IllegalStateException("Cannot added to a dictionary.");
    }
  }

  public void closeArray() {
    checkForOpenCommand(ECommand.ARRAY_COMMAND);
    jsonOutput.append("]");
    workerStack.pop();
    implicitCloseProperty();
  }

  public void openDictionary() {
    checkNoDictionary();
    workerStack.push(ECommand.DICTIONARY_COMMAND);
    addOpeningPart("{");
    onFirstElement = true;
  }

  public void closeDictionary() {
    checkForOpenCommand(ECommand.DICTIONARY_COMMAND);
    jsonOutput.append("}");
    workerStack.pop();
    implicitCloseProperty();
  }

  public void openProperty(String key) {
    if (workerStack.peek() != ECommand.DICTIONARY_COMMAND) {
      throw new IllegalStateException("Properties may only be added " + "to a dictionary but found "
          + workerStack.peek());
    }
    workerStack.push(ECommand.PROPERTY_COMMAND);
    addOpeningPart(toJSONString(key) + " : ");
    onFirstElement = true;
  }

  public void closeProperty() {
    checkForOpenCommand(ECommand.PROPERTY_COMMAND);
    if (jsonOutput.toString().endsWith(" : ")) {
      jsonOutput.append(toJSONString(null));
    }
    workerStack.pop();
    onFirstElement = false;
  }

  public void addStringProperty(String key, String value) {
    openProperty(key);
    addString(value);
  }

  public void addString(String value) {
    checkNoDictionary();
    addOpeningPart(toJSONString(value));
    implicitCloseProperty();
  }

  private void implicitCloseProperty() {
    if (!workerStack.isEmpty() && (workerStack.peek() == ECommand.PROPERTY_COMMAND)) {
      workerStack.pop();
    }
    onFirstElement = false;
  }

  public void addBoolean(Boolean value) {
    checkNoDictionary();
    if (value != null) {
      addOpeningPart(Boolean.toString(value));
    } else {
      addNull();
    }
    implicitCloseProperty();
  }

  public void addNumber(Number value) {
    checkNoDictionary();
    if (value != null) {
      addOpeningPart(value.toString());
    } else {
      addNull();
    }
    implicitCloseProperty();
  }

  /**
   * @deprecated instead use {@link #addNumber(Number)}
   */
  @Deprecated
  public void addFloat(Float value) {
    addNumber(value);
  }

  /**
   * @deprecated instead use {@link #addNumber(Number)}
   */
  @Deprecated
  public void addInteger(Integer value) {
    addNumber(value);
  }

  public void addNull() {
    checkNoDictionary();
    addOpeningPart(toJSONString(null));
    implicitCloseProperty();
  }

  /**
   * for internal use only (ONLY TESTS!!!)
   */
  Stack<ECommand> internal_getWorkerStack() {
    return workerStack;
  }

  /**
   * for internal use only (ONLY TESTS!!!)
   */
  String internal_getUnfinishedJSON() {
    return jsonOutput.toString();
  }

  private void checkForOpenCommand(ECommand expectedCmd) {
    if (workerStack.isEmpty()) {
      throw new IllegalStateException("Cannot be applied a close " + expectedCmd
          + " on an empty stack.");
    }
    if (workerStack.peek() != expectedCmd) {
      throw new IllegalStateException("Close " + expectedCmd + " cannot follow "
          + workerStack.peek());
    }
  }

  String toJSONString(String outStr) {
    if (outStr == null) {
      return "null";
    } else {
      for (Entry<Pattern, String> entry : JSON_REPLACEMENTS.entrySet()) {
        outStr = entry.getKey().matcher(outStr).replaceAll(entry.getValue());
      }
      return new StringBuilder("\"").append(outStr).append("\"").toString();
    }
  }

  /**
   * for internal use only (ONLY TESTS!!!)
   */
  void setOnFirstElement(boolean onFirstElement) {
    this.onFirstElement = onFirstElement;
  }

  boolean isOnFirstElement() {
    return onFirstElement;
  }

}

@@ -997,16 +997,42 @@
 .Entry;%0A
+import java.util.Objects;%0A
 import j
@@ -3649,22 +3649,22 @@
 ng key, 
-String
+Object
  value) 
@@ -3739,22 +3739,22 @@
 dString(
-String
+Object
  value) 
@@ -5554,29 +5554,28 @@
 ing(
-String outStr
+Object value
 ) %7B%0A    
 if (
@@ -5574,20 +5574,23 @@
     
-if (
+String 
 outStr =
 = nu
@@ -5589,49 +5589,56 @@
 tr =
-= null) %7B%0A      return %22null%22;%0A    %7D else
+ Objects.toString(value);%0A    if (value != null)
  %7B%0A 
@@ -5802,22 +5802,24 @@
 %7D%0A      
-return
+outStr =
  new Str
@@ -5877,24 +5877,43 @@
 ng();%0A    %7D%0A
+    return outStr;%0A
   %7D%0A%0A  /**%0A 

allow object for JsonBuilder#addString (#183)