/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portlet.usersadmin.util;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.search.Hits;
import com.liferay.portal.kernel.util.Accessor;
import com.liferay.portal.kernel.util.LocaleThreadLocal;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Tuple;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Address;
import com.liferay.portal.model.Country;
import com.liferay.portal.model.EmailAddress;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.OrgLabor;
import com.liferay.portal.model.Organization;
import com.liferay.portal.model.Phone;
import com.liferay.portal.model.Region;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroup;
import com.liferay.portal.model.UserGroupGroupRole;
import com.liferay.portal.model.UserGroupRole;
import com.liferay.portal.model.Website;
import com.liferay.portal.security.permission.PermissionChecker;
import com.liferay.portal.service.CountryServiceUtil;
import com.liferay.portal.service.RegionServiceUtil;
import com.liferay.portal.service.RoleLocalServiceUtil;

import java.util.List;
import java.util.Locale;

import javax.portlet.ActionRequest;
import javax.portlet.PortletRequest;
import javax.portlet.RenderResponse;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Brian Wing Shun Chan
 * @author Jorge Ferrer
 * @author Julio Camarero
 */
public interface UsersAdmin {

	public static final String CUSTOM_QUESTION = "write-my-own-question";

	public static final Accessor<Organization, String>
		ORGANIZATION_COUNTRY_NAME_ACCESSOR =
			new Accessor<Organization, String>() {

			@Override
			public String get(Organization organization) {
				Address address = organization.getAddress();

				Country country = address.getCountry();

				String countryName = country.getName(
					LocaleThreadLocal.getThemeDisplayLocale());

				if (Validator.isNull(countryName)) {
					try {
						country = CountryServiceUtil.fetchCountry(
							organization.getCountryId());

						if (country != null) {
							countryName = country.getName(
								LocaleThreadLocal.getThemeDisplayLocale());
						}
					}
					catch (SystemException se) {
					}
				}

				return countryName;
			}

		};

	public static final Accessor<Organization, String>
		ORGANIZATION_REGION_NAME_ACCESSOR =
			new Accessor<Organization, String>() {

			@Override
			public String get(Organization organization) {
				Address address = organization.getAddress();

				Region region = address.getRegion();

				String regionName = region.getName();

				if (Validator.isNull(regionName)) {
					try {
						region = RegionServiceUtil.fetchRegion(
							organization.getRegionId());

						if (region != null) {
							regionName = LanguageUtil.get(
								LocaleThreadLocal.getThemeDisplayLocale(),
								region.getName());
						}
					}
					catch (SystemException se) {
					}
				}

				return regionName;
			}

		};

	public static final Accessor<UserGroupGroupRole, String>
		USER_GROUP_GROUP_ROLE_TITLE_ACCESSOR =
			new Accessor<UserGroupGroupRole, String>() {

				@Override
				public String get(UserGroupGroupRole userGroupGroupRole) {
					try {
						Role role = RoleLocalServiceUtil.fetchRole(
							userGroupGroupRole.getRoleId());

						if (role == null) {
							return StringPool.BLANK;
						}

						return role.getTitle(
							LocaleThreadLocal.getThemeDisplayLocale());
					}
					catch (SystemException se) {
						return StringPool.BLANK;
					}
				}

			};

	public static final Accessor<UserGroupRole, String>
		USER_GROUP_ROLE_TITLE_ACCESSOR =
			new Accessor<UserGroupRole, String>() {

				@Override
				public String get(UserGroupRole userGroupRole) {
					try {
						Role role = RoleLocalServiceUtil.fetchRole(
							userGroupRole.getRoleId());

						if (role == null) {
							return StringPool.BLANK;
						}

						return role.getTitle(
							LocaleThreadLocal.getThemeDisplayLocale());
					}
					catch (SystemException se) {
						return StringPool.BLANK;
					}
				}

			};

	public void addPortletBreadcrumbEntries(
			Organization organization, HttpServletRequest request,
			RenderResponse renderResponse)
		throws Exception;

	public long[] addRequiredRoles(long userId, long[] roleIds)
		throws PortalException, SystemException;

	public long[] addRequiredRoles(User user, long[] roleIds)
		throws PortalException, SystemException;

	public List<Role> filterGroupRoles(
			PermissionChecker permissionChecker, long groupId, List<Role> roles)
		throws PortalException, SystemException;

	public List<Group> filterGroups(
			PermissionChecker permissionChecker, List<Group> groups)
		throws PortalException, SystemException;

	public List<Organization> filterOrganizations(
			PermissionChecker permissionChecker,
			List<Organization> organizations)
		throws PortalException, SystemException;

	public List<Role> filterRoles(
		PermissionChecker permissionChecker, List<Role> roles);

	public long[] filterUnsetGroupUserIds(
			PermissionChecker permissionChecker, long groupId, long[] userIds)
		throws PortalException, SystemException;

	public long[] filterUnsetOrganizationUserIds(
			PermissionChecker permissionChecker, long organizationId,
			long[] userIds)
		throws PortalException, SystemException;

	public List<UserGroupRole> filterUserGroupRoles(
			PermissionChecker permissionChecker,
			List<UserGroupRole> userGroupRoles)
		throws PortalException, SystemException;

	public List<UserGroup> filterUserGroups(
		PermissionChecker permissionChecker, List<UserGroup> userGroups);

	public List<Address> getAddresses(ActionRequest actionRequest);

	public List<Address> getAddresses(
		ActionRequest actionRequest, List<Address> defaultAddresses);

	public List<EmailAddress> getEmailAddresses(ActionRequest actionRequest);

	public List<EmailAddress> getEmailAddresses(
		ActionRequest actionRequest, List<EmailAddress> defaultEmailAddresses);

	public long[] getGroupIds(PortletRequest portletRequest)
		throws PortalException, SystemException;

	public OrderByComparator getGroupOrderByComparator(
		String orderByCol, String orderByType);

	public Long[] getOrganizationIds(List<Organization> organizations);

	public long[] getOrganizationIds(PortletRequest portletRequest)
		throws PortalException, SystemException;

	public OrderByComparator getOrganizationOrderByComparator(
		String orderByCol, String orderByType);

	public Tuple getOrganizations(Hits hits)
		throws PortalException, SystemException;

	public List<OrgLabor> getOrgLabors(ActionRequest actionRequest);

	public List<Phone> getPhones(ActionRequest actionRequest);

	public List<Phone> getPhones(
		ActionRequest actionRequest, List<Phone> defaultPhones);

	public long[] getRoleIds(PortletRequest portletRequest)
		throws PortalException, SystemException;

	public OrderByComparator getRoleOrderByComparator(
		String orderByCol, String orderByType);

	public <T> String getUserColumnText(
		Locale locale, List<T> list, Accessor<T, String> accessor, int count);

	public long[] getUserGroupIds(PortletRequest portletRequest)
		throws PortalException, SystemException;

	public OrderByComparator getUserGroupOrderByComparator(
		String orderByCol, String orderByType);

	public List<UserGroupRole> getUserGroupRoles(PortletRequest portletRequest)
		throws PortalException, SystemException;

	public Tuple getUserGroups(Hits hits)
		throws PortalException, SystemException;

	public OrderByComparator getUserOrderByComparator(
		String orderByCol, String orderByType);

	public Tuple getUsers(Hits hits) throws PortalException, SystemException;

	public List<Website> getWebsites(ActionRequest actionRequest);

	public List<Website> getWebsites(
		ActionRequest actionRequest, List<Website> defaultWebsites);

	/**
	 * @deprecated As of 6.2.0, replaced by {@link
	 *             #hasUpdateFieldPermission(PermissionChecker, User, User,
	 *             String)}
	 */
	public boolean hasUpdateEmailAddress(
			PermissionChecker permissionChecker, User user)
		throws PortalException, SystemException;

	public boolean hasUpdateFieldPermission(
			PermissionChecker permissionChecker, User updatingUser,
			User updatedUser, String field)
		throws PortalException, SystemException;

	/**
	 * @deprecated As of 6.2.0, replaced by {@link
	 *             #hasUpdateFieldPermission(PermissionChecker, User, User,
	 *             String)}
	 */
	@Deprecated
	public boolean hasUpdateFieldPermission(User user, String field)
		throws PortalException, SystemException;

	/**
	 * @deprecated As of 6.2.0, replaced by {@link
	 *             #hasUpdateFieldPermission(PermissionChecker, User, User,
	 *             String)}
	 */
	public boolean hasUpdateScreenName(
			PermissionChecker permissionChecker, User user)
		throws PortalException, SystemException;

	public long[] removeRequiredRoles(long userId, long[] roleIds)
		throws PortalException, SystemException;

	public long[] removeRequiredRoles(User user, long[] roleIds)
		throws PortalException, SystemException;

	public void updateAddresses(
			String className, long classPK, List<Address> addresses)
		throws PortalException, SystemException;

	public void updateEmailAddresses(
			String className, long classPK, List<EmailAddress> emailAddresses)
		throws PortalException, SystemException;

	public void updateOrgLabors(long classPK, List<OrgLabor> orgLabors)
		throws PortalException, SystemException;

	public void updatePhones(String className, long classPK, List<Phone> phones)
		throws PortalException, SystemException;

	public void updateWebsites(
			String className, long classPK, List<Website> websites)
		throws PortalException, SystemException;

}