package com.temporaryteam.noticeditor.model;

import java.util.ArrayList;
import java.util.Random;

import org.pegdown.PegDownProcessor;

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

// TODO: remove this class (use NoticeTreeItem)
/**
 * Model representation of notice. Contains notice data or branch data
 *
 * @author naik, setser, annimon, kalter
 */
public class NoticeItem {

	public static final String KEY_NAME = "name";
	public static final String KEY_CONTENT = "content";
	public static final String KEY_CHILDS = "childs";

	private static final Random RND = new Random();
	
	private String name;
	private ArrayList<NoticeItem> childs = new ArrayList<>();
	private String content;
	private long id;

	public NoticeItem(String name, String content) {
		this.name = name;
		this.content = content;
		genId();
	}

	public NoticeItem(String name, ArrayList<NoticeItem> childs) {
		this.name = name;
		this.childs = childs;
		genId();
	}

	public NoticeItem(JSONObject json) throws JSONException {
		name = json.getString(KEY_NAME);
		if (json.has(KEY_CONTENT)) {
			content = json.getString(KEY_CONTENT);
		}
		JSONArray arr = json.getJSONArray(KEY_CHILDS);
		childs = new ArrayList();
		if (arr.length() != 0) {
			for (int i = 0; i < arr.length(); i++) {
				childs.add(new NoticeItem(arr.getJSONObject(i)));
			}
		}
		genId();
	}
	
	private void genId() {
		id = System.nanoTime() + RND.nextInt(100);
	}

	public long getId() {
		return id;
	}

	/**
	 *
	 * @return notice content or null if its a branch
	 */
	public String getContent() {
		return content;
	}

	public String getName() {
		return name;
	}

	public ArrayList<NoticeItem> childrens() {
		return childs;
	}

	public boolean isBranch() {
		return content == null;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setSubCategories(ArrayList<NoticeItem> childs) {
		this.childs = childs;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String toHTML(PegDownProcessor processor) {
		StringBuilder html = new StringBuilder();
		if (isBranch()) {
			for (NoticeItem child : childs) {
				html.append("<a href=\"").append(child.getId()).append(".html\">");
				html.append(child.getName()).append(".html</a><br/>\n");
			}
			html.append("<br/><br/>\n");
		} else {
			html.append(processor.markdownToHtml(content));
		}
		return html.toString();
	}

	public JSONObject toJson() throws JSONException {
		JSONObject obj = new JSONObject();
		obj.put(KEY_NAME, name);
		if (content != null) {
			obj.put(KEY_CONTENT, content);
		}
		ArrayList list = new ArrayList();
		if (childs != null) {
			for (NoticeItem subcategory : childs) {
				list.add(subcategory.toJson());
			}
		}
		obj.put(KEY_CHILDS, new JSONArray(list));
		return obj;
	}

}

@@ -617,17 +617,16 @@
 ndom();%0A
-%09
 %0A%09privat
@@ -1229,36 +1229,8 @@
 ();%0A
-%09%09if (arr.length() != 0) %7B%0A%09
 %09%09fo
@@ -1268,17 +1268,16 @@
  i++) %7B%0A
-%09
 %09%09%09child
@@ -1317,29 +1317,24 @@
 bject(i)));%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%09genId(
@@ -1343,10 +1343,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09pri
@@ -1409,24 +1409,150 @@
 t(100);%0A%09%7D%0A%0A
+%09/** %0A%09 * id used for diff nodes with equals name (for example when exporting to HTML). %0A%09 * @return generated unique id%0A%09 */%0A
 %09public long

Clear code & comment