package org.itevents.service;

import org.itevents.model.Event;
import org.itevents.parameter.FilteredEventsParameter;

import java.util.*;

public class RatingServiceImpl implements RatingService {

    @Override
    public Map<Event, Integer> chooseMostPopularEvents(int quantity) {
        EventService eventService = new EventServiceImpl();
        VisitLogService visitLogService = new VisitLogServiceImpl();
        FilteredEventsParameter parameters = new FilteredEventsParameter();
        List<Event> filteredEvents = eventService.getFilteredEvents(parameters);
        Event futureEvent = null;
        Integer countViewers = null;
        Map<Event, Integer> eventMap = new HashMap<>();
        for (Event filteredEvent : filteredEvents) {
            futureEvent = eventService.getFutureEventById(7, filteredEvent.getId());
            if (futureEvent != null){
                countViewers = visitLogService.getCountViewByEventId(futureEvent.getId());
                eventMap.put(futureEvent, countViewers);
            }
        }
        eventMap = sortMapEvents( -1, eventMap);


        return eventMap;
    }

    /*
    parameter order -   +1 return sorted map by descending
                        -1 return sorted map by ascending
    */
    private Map<Event, Integer> sortMapEvents(int order, Map<Event, Integer> map){
        final int orderInner = order;
        List<Map.Entry<Event, Integer>> list = new LinkedList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Event, Integer>>() {
            @Override
            public int compare(Map.Entry<Event, Integer> o1, Map.Entry<Event, Integer> o2) {
                return orderInner * (o1.getValue()).compareTo(o2.getValue());
            }
        });

        Map<Event, Integer> resultMap = new LinkedHashMap<>();
        for (Map.Entry<Event, Integer> entry : list)
        {
            resultMap.put(entry.getKey(), entry.getValue());
        }
        return resultMap;
    }
}

@@ -193,16 +193,61 @@
 vice %7B%0A%0A
+    private final int orderDescending = -1;%0A%0A
     @Ove
@@ -248,24 +248,24 @@
   @Override%0A
-
     public M
@@ -672,32 +672,33 @@
 Viewers = null;%0A
+%0A
         Map%3CEven
@@ -1123,23 +1123,89 @@
 ts( 
--1, eventMap);%0A
+orderDescending, eventMap);%0A        eventMap = trimToSizeMap(quantity, eventMap);
 %0A%0A  
@@ -2053,32 +2053,32 @@
 e());%0A        %7D%0A
-
         return r
@@ -2092,11 +2092,517 @@
 p;%0A    %7D
+%0A%0A    private Map%3CEvent, Integer%3E trimToSizeMap(int quantity, Map%3CEvent, Integer%3E map)%7B%0A        if (map.size() %3C= quantity)%7B%0A            return map;%0A        %7D%0A        int count = 0;%0A        Map%3CEvent, Integer%3E returnedMap = new HashMap%3C%3E();%0A        for (Map.Entry%3CEvent, Integer%3E entry : map.entrySet()) %7B%0A            returnedMap.put(entry.getKey(), entry.getValue());%0A            count++;%0A            if (count %3E= quantity)%7B%0A                break;%0A            %7D%0A        %7D%0A        return returnedMap;%0A    %7D
 %0A%7D%0A

add method trim map to quantity of events