package com.tandbergtv.watchpoint.pmm.web.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.tandbergtv.watchpoint.pmm.entities.ContainerProperty;
import com.tandbergtv.watchpoint.pmm.entities.IContainer;
import com.tandbergtv.watchpoint.pmm.web.formbeans.ContainerPropertyForm;
import com.tandbergtv.workflow.core.service.ServiceRegistry;
import com.tandbergtv.workflow.core.service.cache.ICacheService;

/**
 * Helper class for ui purposes.
 * 
 * @author spuranik
 *
 */
public class ContainerPropertyUIHelper {
	
	/**
	 * Gets all distinct existing properties in the db and prepares a comma separated
	 * list of these names.
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String getExistingPropertyNames() {
		Set<String> existingProperties = new HashSet<String>();
		ICacheService<IContainer> containerCache = (ICacheService<IContainer>) ServiceRegistry
				.getDefault().lookup("Container Cache");
		for (Serializable contextId : containerCache.getKeys()) {
			IContainer container = containerCache.get(contextId);
			for (ContainerProperty property : container.getProperties()) {
				existingProperties.add(property.getName());
			}
		}
		StringBuilder sb = new StringBuilder();
		for (String p : existingProperties) {
			if (sb.length() > 0) {
				sb.append(",");
			}
			sb.append(p);
		}
		return sb.toString();
	}

	/**
	 * Adds the properties represented in the name/value pairs to the given container.
	 * 
	 * @param names
	 * @param values
	 * @param container
	 */
	public static void createProperties(String[] names, String[] values, IContainer container) {
		for (int i = 0; i < names.length; i++) {
			container.addProperty(names[i], values[i]);
		}
	}

	/**
	 * Updates the properties in the container after based on the properties
	 * received in the name/value pairs.
	 * 
	 * @param names
	 * @param values
	 * @param container
	 */
	public static void updateProperties(String[] names, String[] values, IContainer container) {
		// all properties were removed from an existing partner.
		if (names == null || names.length == 0) {
			container.removeAllProperties();
			return;
		}

		// delete properties that have been removed
		List<ContainerProperty> toDelete = new ArrayList<ContainerProperty>();
		for (ContainerProperty p : container.getProperties()) {
			if (!isPropertyPresent(p.getName(), names)) {
				toDelete.add(p);
			}
		}
		for (ContainerProperty dp : toDelete) {
			container.removeProperty(dp.getName());
		}
		
		// create/update existing properties
		for (int i = 0; i < names.length; i++) {
			ContainerProperty p = findProperty(names[i], container.getProperties());
			// this is a new property
			if (p == null) {
				container.addProperty(names[i], values[i]);
			} else {
				p.setValue(values[i]);
			}
		}
	}

	private static ContainerProperty findProperty(String name, List<ContainerProperty> props) {
		for (ContainerProperty p : props) {
			if (p.getName().equalsIgnoreCase(name)) {
				return p;
			}
		}
		return null;
	}

	private static boolean isPropertyPresent(String name, String[] names) {
		for (String n : names) {
			if (n.equalsIgnoreCase(name)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * Prepares proeprty forms for the given list of properties.
	 * 
	 * @param properties
	 * @return
	 */
	public static List<ContainerPropertyForm> getPropertyForms(List<ContainerProperty> properties) {
		List<ContainerPropertyForm> propertyForms = new ArrayList<ContainerPropertyForm>();
		for (ContainerProperty property : properties) {
			propertyForms.add(new ContainerPropertyForm(property.getId(),
					property.getName(), property.getValue(), property.getContext().getId()));
		}
		return propertyForms;
	}
	
	/**
	 * Prepares container property forms corresponding to the given name/value pairs.
	 *  
	 * @param names
	 * @param values
	 * @return
	 */
	public static List<ContainerPropertyForm> getPropertyForms(String[] names, String[] values) {
		List<ContainerPropertyForm> propForms = new ArrayList<ContainerPropertyForm>();
		for (int i = 0; i < names.length; i++) {
			propForms.add(new ContainerPropertyForm(names[i], values[i]));
		}
		return propForms;
	}
}
