/*
 * Copyright 2018 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epam.ta.reportportal.core.widget.content.filter;

import com.epam.ta.reportportal.auth.ReportPortalUser;
import com.epam.ta.reportportal.commons.querygen.CriteriaHolder;
import com.epam.ta.reportportal.commons.querygen.Filter;
import com.epam.ta.reportportal.core.widget.content.BuildFilterStrategy;
import com.epam.ta.reportportal.core.widget.content.LoadContentStrategy;
import com.epam.ta.reportportal.entity.filter.FilterSort;
import com.epam.ta.reportportal.entity.filter.UserFilter;
import com.epam.ta.reportportal.entity.widget.Widget;
import com.epam.ta.reportportal.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.model.ErrorType;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.data.domain.Sort;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Optional.ofNullable;

/**
 * @author <a href="mailto:ivan_budayeu@epam.com">Ivan Budayeu</a>
 */
public abstract class AbstractStatisticsFilterStrategy implements BuildFilterStrategy {

	@Override
	public Map<String, ?> buildFilterAndLoadContent(LoadContentStrategy loadContentStrategy, ReportPortalUser.ProjectDetails projectDetails,
			Widget widget) {
		Map<Filter, Sort> filterSortMap = buildFilterSortMap(widget, projectDetails.getProjectId());
		return loadContentStrategy.loadContent(Lists.newArrayList(widget.getContentFields()),
				filterSortMap,
				widget.getWidgetOptions(),
				widget.getItemsCount()
		);
	}

	protected Map<Filter, Sort> buildFilterSortMap(Widget widget, Long projectId) {
		Map<Filter, Sort> filterSortMap = Maps.newLinkedHashMap();
		Set<UserFilter> filters = Optional.ofNullable(widget.getFilters()).orElse(Collections.emptySet());
		Filter defaultFilter = buildDefaultFilter(widget, projectId);
		Optional.ofNullable(defaultFilter).ifPresent(f -> filterSortMap.put(defaultFilter, Sort.unsorted()));

		filters.forEach(f -> {
			Filter filter = new Filter(f.getId(), f.getTargetClass().getClassObject(), Sets.newHashSet(f.getFilterCondition()));

			Optional<Set<FilterSort>> filterSorts = ofNullable(f.getFilterSorts());
			Sort sort = Sort.by(filterSorts.map(fs -> {

				Map<String, String> sortingCriteria = filter.getTarget()
						.getCriteriaHolders()
						.stream()
						.collect(Collectors.toMap(CriteriaHolder::getFilterCriteria, CriteriaHolder::getQueryCriteria));

				return fs.stream()
						.map(s -> new Sort.Order(s.getDirection(),
								ofNullable(sortingCriteria.get(s.getField())).orElseThrow(() -> new ReportPortalException(ErrorType.INCORRECT_FILTER_PARAMETERS,
										sortingCriteria.get(s.getField())
								))
						))
						.collect(Collectors.toList());

			}).orElseGet(Collections::emptyList));
			filterSortMap.put(filter, sort);
		});

		return filterSortMap;
	}

	protected abstract Filter buildDefaultFilter(Widget widget, Long projectId);
}

@@ -707,73 +707,8 @@
 er;%0A
-import com.epam.ta.reportportal.commons.querygen.CriteriaHolder;%0A
 impo
@@ -752,32 +752,32 @@
 uerygen.Filter;%0A
+
 import com.epam.
@@ -1080,125 +1080,8 @@
 et;%0A
-import com.epam.ta.reportportal.exception.ReportPortalException;%0Aimport com.epam.ta.reportportal.ws.model.ErrorType;%0A
 impo
@@ -2186,17 +2186,21 @@
 Filter%3E 
-f
+userF
 ilters =
@@ -2445,12 +2445,15 @@
 ));%0A
-%0A
 %09%09
-f
+userF
 ilte
@@ -2463,17 +2463,26 @@
 forEach(
-f
+userFilter
  -%3E %7B%0A%09%09
@@ -2513,20 +2513,43 @@
 ter(
-f.getId(), f
+userFilter.getId(),%0A%09%09%09%09%09userFilter
 .get
@@ -2579,17 +2579,22 @@
 bject(),
- 
+%0A%09%09%09%09%09
 Sets.new
@@ -2601,17 +2601,26 @@
 HashSet(
-f
+userFilter
 .getFilt
@@ -2633,20 +2633,23 @@
 ition())
+%0A%09%09%09
 );%0A
-%0A
 %09%09%09Optio
@@ -2694,17 +2694,26 @@
 ullable(
-f
+userFilter
 .getFilt
@@ -2751,255 +2751,61 @@
 .by(
-filterSorts.map(fs -%3E %7B%0A%0A%09%09%09%09Map%3CString, String%3E sortingCriteria = filter.getTarget()%0A%09%09%09%09%09%09.getCriteriaHolders()%0A%09%09%09%09%09%09.stream()%0A%09%09%09%09%09%09.collect(Collectors.toMap(CriteriaHolder::getFilterCriteria, CriteriaHolder::getQueryCriteria));%0A%0A%09%09%09%09return fs
+(Sort.Order) filterSorts.map(filterSort -%3E filterSort
 .str
@@ -2810,17 +2810,16 @@
 tream()%0A
-%09
 %09%09%09%09%09.ma
@@ -2828,23 +2828,16 @@
 s -%3E
- new
  Sort.
-Order
+by
 (s.g
@@ -2854,211 +2854,24 @@
 n(),
-%0A%09%09%09%09%09%09%09%09ofNullable(sortingCriteria.get(s.getField())).orElseThrow(() -%3E new ReportPortalException(ErrorType.INCORRECT_FILTER_PARAMETERS,%0A%09%09%09%09%09%09%09%09%09%09sortingCriteria.get(s.getField())%0A%09%09%09%09%09%09%09%09))%0A%09%09%09%09%09%09))%0A%09
+ s.getField()))%0A
 %09%09%09%09
@@ -2904,15 +2904,8 @@
 t())
-;%0A%0A%09%09%09%7D
 ).or

fix build sort map