package com.sergiobatista.chalmers.thesis.userstoriesgenerator;

import org.json.JSONArray;
import org.json.JSONObject;

public class JSONGenerator {

	private JSONObject json = new JSONObject();

	/**
	 * input: [0] Timestamp, [1] Title, [2] Use Case, [3] Preconditions, [4]
	 * Actors, [5] Scenario, [6] Alternative Scenario, [7] Desired Outcome, [8]
	 * tags
	 */
	public JSONGenerator(String[] input) {

		this.setTitle(input[1]);
		this.setUseCase(input[2]);
		this.setPreConditions(input[3]);
		this.setActors(input[4]);
		this.setScenarios(input[5]);
		this.setAltScenarios(input[6]);
		this.setOutcome(input[7]);
		this.setTags(input[8]);
	}

	@Override
	public String toString() {
		return json.toString();
	}
	

	public JSONObject getJson() {
		return json;
	}

	private void setTags(String input) {
		JSONArray arr = new JSONArray();

		breakIntoListItems(input, ", ", arr);

		json.put("tags", arr);
	}

	private void setOutcome(String input) {
		json.put("outcome", input);
	}

	private void setAltScenarios(String input) {
		JSONArray arr = new JSONArray();

		breakIntoListItems(input, ", ", arr);

		json.put("alternativeScenarios", arr);
	}

	private void setScenarios(String input) {
		JSONArray arr = new JSONArray();

		breakIntoListItems(input, ", ", arr);

		json.put("scenarios", arr);
	}

	private void setActors(String input) {
		JSONArray arr = new JSONArray();

		breakIntoListItems(input, ", ", arr);

		json.put("actors", arr);
	}

	private void setPreConditions(String input) {
		JSONArray arr = new JSONArray();

		breakIntoListItems(input, ", ", arr);

		json.put("preConditions", arr);
	}

	private void setUseCase(String input) {
		json.put("useCase", input);
	}

	private void setTitle(String input) {
		json.put("title", input);
	}

	private void breakIntoListItems(String input, String delimiter,
			JSONArray arr) {
		String[] elements = input.trim().split(delimiter);
		for (String element : elements)
			arr.put(element);
	}

}

@@ -354,16 +354,48 @@
 %09 * tags
+, %5B9%5D Where, %5B10%5D How, %5B11%5D What
 %0A%09 */%0A%09p
@@ -652,156 +652,868 @@
 ;%0A%09%09
-this.setTags(input%5B8%5D);%0A%09%7D%0A%0A%09@Override%0A%09public String toString() %7B%0A%09%09return json.toString();%0A%09%7D%0A%09%0A%0A%09public JSONObject getJson() %7B%0A%09%09return json;%0A%09%7D%0A
+%0A%09%09/**%0A%09%09 * uncomment for  the old version%0A%09%09 */%0A%09%09//this.setTags(input%5B8%5D);%0A%09%09%0A%09%09JSONObject tags = new JSONObject();%0A%09%09this.setWhere(input%5B9%5D, tags);%0A%09%09this.setHow(input%5B10%5D, tags);%0A%09%09this.setWhat(input%5B11%5D, tags);%0A%09%09json.put(%22tags%22, tags);%0A%09%09%0A%09%7D%0A%0A%09@Override%0A%09public String toString() %7B%0A%09%09return json.toString();%0A%09%7D%0A%09%0A%0A%09public JSONObject getJson() %7B%0A%09%09return json;%0A%09%7D%0A%0A%09private void setWhat(String input, JSONObject tags) %7B%0A%09%09JSONArray arr = new JSONArray();%0A%0A%09%09breakIntoListItems(input, %22, %22, arr);%0A%0A%09%09tags.put(%22what%22, arr);%0A%09%09%0A%09%7D%0A%0A%09private void setHow(String input, JSONObject tags) %7B%0A%09%09JSONArray arr = new JSONArray();%0A%0A%09%09breakIntoListItems(input, %22, %22, arr);%0A%0A%09%09tags.put(%22how%22, arr);%0A%09%09%0A%09%7D%0A%0A%09private void setWhere(String input, JSONObject tags) %7B%0A%09%09JSONArray arr = new JSONArray();%0A%0A%09%09breakIntoListItems(input, %22, %22, arr);%0A%0A%09%09tags.put(%22where%22, arr);%0A%09%09%0A%09%7D
 %0A%09pr

prepare JSONGenerator for new csv tag structure with Who,What,How tags