/*******************************************************************************
 * Copyright (c) 2012 Ericsson
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Miles Parker (Tasktop Technologies) - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.reviews.ui.providers;

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

import org.eclipse.emf.ecore.EObject;
import org.eclipse.mylyn.reviews.core.model.IFileItem;
import org.eclipse.mylyn.reviews.core.model.IReview;
import org.eclipse.mylyn.reviews.core.model.IReviewItem;
import org.eclipse.mylyn.reviews.core.model.IReviewItemSet;
import org.eclipse.mylyn.reviews.core.model.IComment;
import org.eclipse.mylyn.reviews.core.model.ICommentContainer;

/**
 * Provides a common tree hierarchy similar to Gerrit Web UI hierarchy, except that when used with ReviewsSorter, global
 * comments appear as members of a single "Global" node. Hierarchy is:
 * <ol>
 * <li>Global Node</li>
 * <li>-Comments</li>
 * <li>Patch Sets</li>
 * <li>-Artifacts</li>
 * <li>--Artifact Comments</li>
 * </ol>
 * 
 * @see ReviewsSorter
 * @author Miles Parker
 */
public class ReviewsTreeContentProvider extends GenericTreeContentProvider {

	public Object[] getElements(Object element) {
		if (element instanceof GlobalCommentsNode) {
			return ((GlobalCommentsNode) element).getReview().getComments().toArray();
		} else if (element instanceof EObject) {
			List<Object> children = new ArrayList<Object>();
			if (element instanceof IReview) {
				children.add(new GlobalCommentsNode((IReview) element));
				children.addAll(((IReview) element).getSets());
				return children.toArray();
			}
			if (element instanceof IReviewItemSet) {
				IReviewItemSet itemSet = (IReviewItemSet) element;
				if (itemSet.getItems().size() > 0) {
					children.addAll(itemSet.getComments());
					children.addAll(itemSet.getItems());
				} else {
					children.add(new RetrievingContentsNode(itemSet));
				}
			}
			if (element instanceof IComment) {
				IComment comment = (IComment) element;
				children.addAll(comment.getReplies());
			}
			if (element instanceof IFileItem) {
				IFileItem item = (IFileItem) element;
				for (IComment comment : item.getBase().getComments()) {
					children.add(comment);
				}
				for (IComment comment : item.getTarget().getComments()) {
					children.add(comment);
				}
			}
			return children.toArray();
		}
		return getCollectionChildren(element);
	}

	/**
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
	 */
	@Override
	public boolean hasChildren(Object element) {
		if (element instanceof IReview) {
			return ((IReview) element).getSets().size() > 0;
		}
		if (element instanceof GlobalCommentsNode) {
			return ((GlobalCommentsNode) element).getReview().getComments().size() > 0;
		}
		return ((element instanceof ICommentContainer) && ((ICommentContainer) element).getAllComments().size() > 0)
				|| (element instanceof IReview && ((IReview) element).getSets().size() > 0)
				|| (element instanceof GlobalCommentsNode && hasChildren(((GlobalCommentsNode) element).getReview()) || (element instanceof IReviewItemSet))
				|| hasCollectionChildren(element);
	}

	@Override
	public Object getParent(Object element) {
		if (element instanceof IReviewItem) {
			return ((IReviewItem) element).getReview();
		}
		return null;
	}
}
