package org.gcube.portlets.widgets.workspacesharingwidget.server.util;

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

import org.apache.log4j.Logger;
import org.gcube.portlets.widgets.workspacesharingwidget.shared.InfoContactModel;
import org.gcube.vomanagement.usermanagement.UserManager;
import org.gcube.vomanagement.usermanagement.exception.UserManagementPortalException;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException;
import org.gcube.vomanagement.usermanagement.exception.UserRetrievalFault;
import org.gcube.vomanagement.usermanagement.impl.liferay.LiferayUserManager;
import org.gcube.vomanagement.usermanagement.model.UserModel;



public class UserUtil {
	
	static UserManager um = new LiferayUserManager();
	protected static Logger logger = Logger.getLogger(UserUtil.class);

	
	/**
	 * 
	 * @param user
	 * @return
	 */
	public static String getUserFullName(String portalLogin){
//		return user.getPortalLogin(); //for testing in eclipse
		
//		logger.trace("Finding full name for: "+portalLogin);
		
		if(portalLogin==null)
			return "";
		
		logger.info("Get user full name for: "+portalLogin);
		logger.info("Into portal: "+!WsUtil.withoutPortal);
		if (portalLogin.compareTo(WsUtil.TEST_USER) != 0 && !WsUtil.withoutPortal) { //skip test.user
			
			UserModel curr = null;
			
			try {
				try {
					curr = um.getUserByScreenName(portalLogin);
				
				} catch (UserManagementSystemException e) {
				
					logger.error("An error occurred in getUserFullName "+e,e);
				} catch (UserRetrievalFault e) {
					
					logger.error("An error occurred in getUserFullName "+e,e);
				}
			} catch (UserManagementPortalException ume) {
				logger.error("An error occurred in getUserFullName "+ume,ume);
			}catch (Exception e) {
				logger.error("An error occurred in getUserFullName "+e,e);
				logger.warn("Return portal login "+portalLogin);
				return portalLogin;
			}
			
			if (curr != null){
				
//				logger.trace("Return "+curr.getFullname() +" full name for: "+portalLogin);
				return curr.getFullname();
				
			}
		}else{
			
			logger.info("Return "+WsUtil.TEST_USER_FULL_NAME +" full name for: "+portalLogin);
			return WsUtil.TEST_USER_FULL_NAME;
		}
		
		logger.info("Return empty full name for: "+portalLogin);
		return "";
	}
	
	public static List<String> getListLoginByInfoContactModel(List<InfoContactModel> listContacts){
		
		List<String> listUsers = new ArrayList<String>();
		
		for (InfoContactModel infoContactModel : listContacts) {
			listUsers.add(infoContactModel.getLogin());
		}
		
		return listUsers;
	}
	
	public static String separateUsersNamesToComma(List<InfoContactModel> listContacts){
		
		String users = "";
		
		for (int i = 0; i < listContacts.size()-1; i++) {
			users+= listContacts.get(i).getName() + ", ";
		}
		
		if(listContacts.size()>1)
			users += listContacts.get(listContacts.size()-1).getName();
		
		return users;
	}
	
	
	public static String separateFullNameToCommaForPortalLogin(List<String> listLogin){
		
		String users = "";
		
		for (int i = 0; i < listLogin.size()-1; i++) {
			users+= getUserFullName(listLogin.get(i)) + ", ";
		}
		
		if(listLogin.size()>1)
			users += getUserFullName(listLogin.get(listLogin.size()-1));
		
		return users;
	}
	
}
