package org.jivesoftware.spark.ui.history;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Vyacheslav Durin (nixspirit@gmail.com)
 * 
 *         Apr 15, 2013
 * @version 0.1
 */
public class HistoryEntry {

	private String name;
	private List<HistoryEntry> entries;
	private List<HistoryMessage> messages;
	private Date date;

	public HistoryEntry() {
	}

	public HistoryEntry(HistoryEntry orig) {
        Objects.requireNonNull(orig);
		date = orig.getDate();
		name = orig.getName();
		entries = new ArrayList<>(orig.getEntries());
		messages = new ArrayList<>(orig.getMessages());
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the entries
	 */
	public List<HistoryEntry> getEntries() {
		if (entries == null)
			entries = new ArrayList<>();
		return entries;
	}

	/**
	 * @param entries
	 *            the entries to set
	 */
	public void setEntries(List<HistoryEntry> entries) {
		this.entries = entries;
	}

	public List<HistoryMessage> getMessages() {
		if (null == messages)
			messages = new ArrayList<>();
		return messages;
	}

	/**
	 * @return true if there are any sub entries in it
	 */
	public boolean hasRecords() {
		return entries != null && !entries.isEmpty();
	}

	/**
	 * @return true if there are any messages in it
	 */
	public boolean isEmpty() {
		return messages == null || messages.isEmpty();
	}

	public String getHistory() {
		StringBuilder history = new StringBuilder();
		for (HistoryMessage msg : getMessages()) {
			history.append(msg.getContent());
		}
		return history.toString();
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

    /**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		return "HistoryEntry [ name=" + name + ", entries=" + entries;
	}

}
