/**
 * Copyright (c) 2000-present 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.segments.internal.processor;

import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerList;
import com.liferay.osgi.service.tracker.collections.list.ServiceTrackerListFactory;
import com.liferay.osgi.service.tracker.collections.map.PropertyServiceReferenceComparator;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.segments.processor.SegmentsExperienceRequestProcessor;
import com.liferay.segments.processor.SegmentsExperienceRequestProcessorRegistry;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

/**
 * @author Eduardo García
 */
@Component(
	immediate = true, service = SegmentsExperienceRequestProcessorRegistry.class
)
public class SegmentsExperienceRequestProcessorRegistryImpl
	implements SegmentsExperienceRequestProcessorRegistry {

	@Override
	public long[] getSegmentsExperienceIds(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse, long groupId,
			long classNameId, long classPK)
		throws PortalException {

		long[] segmentsExperienceIds = new long[0];

		for (SegmentsExperienceRequestProcessor
				segmentsExperienceRequestProcessor :
					getSegmentsExperienceRequestProcessors()) {

			segmentsExperienceIds =
				segmentsExperienceRequestProcessor.getSegmentsExperienceIds(
					httpServletRequest, httpServletResponse, groupId,
					classNameId, classPK, segmentsExperienceIds);
		}

		return segmentsExperienceIds;
	}

	@Override
	public long[] getSegmentsExperienceIds(
			HttpServletRequest httpServletRequest,
			HttpServletResponse httpServletResponse, long groupId,
			long classNameId, long classPK, long[] segmentsEntryIds)
		throws PortalException {

		long[] segmentsExperienceIds = new long[0];

		for (SegmentsExperienceRequestProcessor
				segmentsExperienceRequestProcessor :
					getSegmentsExperienceRequestProcessors()) {

			segmentsExperienceIds =
				segmentsExperienceRequestProcessor.getSegmentsExperienceIds(
					httpServletRequest, httpServletResponse, groupId,
					classNameId, classPK, segmentsEntryIds,
					segmentsExperienceIds);
		}

		return segmentsExperienceIds;
	}

	@Override
	public List<SegmentsExperienceRequestProcessor>
		getSegmentsExperienceRequestProcessors() {

		List<SegmentsExperienceRequestProcessor>
			segmentsExperienceRequestProcessors = new ArrayList<>();

		_serviceTrackerList.forEach(segmentsExperienceRequestProcessors::add);

		return segmentsExperienceRequestProcessors;
	}

	@Activate
	protected void activate(final BundleContext bundleContext) {
		_serviceTrackerList = ServiceTrackerListFactory.open(
			bundleContext, SegmentsExperienceRequestProcessor.class,
			Collections.reverseOrder(
				new PropertyServiceReferenceComparator(
					"segments.experience.request.processor.priority")));
	}

	@Deactivate
	protected void deactivate() {
		_serviceTrackerList.close();
	}

	private ServiceTrackerList
		<SegmentsExperienceRequestProcessor, SegmentsExperienceRequestProcessor>
			_serviceTrackerList;

}