package it.mice.voila2.acceleogenerator.reports.javaservice;

import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;

import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Class;

import it.micegroup.voila2.cartridge.library.javaservice.AssociationUtils;
import it.micegroup.voila2.cartridge.library.javaservice.EntityUtils;

/**
 * @author Fabio Memoli <fabio.memoli@micegroup.it>
 */
public class ReportUtils {
	public static final int REPORT_COLUMN_WIDTH = 555;

	/**
	 * Generate an universally unique identifier by unique name. Example:
	 * 37ceaceb-7666-4d09-8ec4-ecddb703677e
	 * 
	 * @return set of characters.
	 */
	public static String getReportUUID(Class aClass, String name) {
		return UUID.nameUUIDFromBytes((aClass.getQualifiedName() + name).getBytes()).toString();
	}

	public static int getAttributeNumber(Class aClass) {
		return EntityUtils.getFiltrableAttributes(aClass).size();
	}

	public static int getParentNumber(Class aClass) {
		int size = AssociationUtils.getParentClasses(aClass).size();
		return (size > 0) ? (size - 1) : 0;
	}

	public static int getColumnWidthForRow(Class aClass) {
		int sum = getAttributeNumber(aClass) + (getParentNumber(aClass) * 2);
		if (sum < 1)
			return 0;
		return REPORT_COLUMN_WIDTH / sum;
	}

	public static Collection<Class> getParentsWithoutOwner(Class aClass, Class owner) {
		Collection<Class> parents = new ArrayList<Class>();
		for (Class cls : AssociationUtils.getParentClasses(aClass)) {
			if (!cls.equals(owner)) {
				parents.add(cls);
			}
		}
		return parents;
	}

	/**
	 * Ritorna la lista di associazioni padre della classe in input escludendo
	 * quella owner
	 * 
	 * @author Vittorio Niespolo <vittorio.niespolo@micegroup.it>
	 *
	 * @param aClass
	 * @param owner
	 * @return
	 */
	public static Collection<Association> getParentAssWithoutOwner(Class aClass, Class owner) {
		Collection<Association> parents = new ArrayList<>();
		for (Association parentAss : AssociationUtils.getParentAssociations(aClass)) {
			if (!AssociationUtils.getParentClass(aClass, parentAss).equals(owner)) {
				parents.add(parentAss);
			}
		}
		return parents;
	}
}
