package org.itevents.controller;

import io.swagger.annotations.Api;
import org.itevents.model.Event;
import org.itevents.service.EventService;
import org.itevents.wrapper.EventWrapper;
import org.springframework.beans.support.PagedListHolder;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.util.List;

@RestController
@Api("Events")
public class EventRestController {

    private final int DEFAULT_ITEMS_PER_PAGE = 10;
    @Inject
    private EventService eventService;

    @RequestMapping(method = RequestMethod.GET, value = "/events/{id}")
    public ResponseEntity<Event> getEvent(@PathVariable("id") int id) {
        Event event = eventService.getEvent(id);
        if (event == null) {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Event>(event, HttpStatus.OK);
    }


    private List<Event> getPaginatedEvents(int page, int itemsPerPage, List<Event> events) {

        int pages = events.size()/itemsPerPage;
        if (events.size() % itemsPerPage != 0) {
            pages++;
        }
        PagedListHolder<Event> paginatedEvents = new PagedListHolder<Event>(events);
        paginatedEvents.setPageSize(itemsPerPage);
        if (page <= 0) {
            return paginatedEvents.getPageList();
        }
        if (page > pages - 1) {
            paginatedEvents.setPage(pages - 1);
            return paginatedEvents.getPageList();
        }
        paginatedEvents.setPage(page);
        return paginatedEvents.getPageList();
    }

    @RequestMapping(method = RequestMethod.GET, value = "/events")
    public List<Event> getFilteredEvents(@RequestParam(required = false, value = "page") Integer page,
                                         @RequestParam(required = false, value = "itemsPerPage") Integer itemsPerPage,
                                         @ModelAttribute EventWrapper wrapper) {
        itemsPerPage = getItemsPerPage(itemsPerPage);
        List<Event> filteredEvents = eventService.getFilteredEvents(wrapper);
        return getPaginatedEvents(page, itemsPerPage, filteredEvents);
    }

    private int getItemsPerPage(Integer itemsPerPage) {
        if (itemsPerPage == null || itemsPerPage <= 0) {
            itemsPerPage = DEFAULT_ITEMS_PER_PAGE;
        }
        return itemsPerPage;
    }
}

@@ -1014,21 +1014,29 @@
 nts(
-i
+I
 nt
+eger
  page, 
-i
+I
 nt
+eger
  ite
@@ -1065,24 +1065,78 @@
  events) %7B%0A%0A
+        itemsPerPage = getItemsPerPage(itemsPerPage);%0A
         int 
@@ -1400,24 +1400,40 @@
    if (page 
+== null %7C%7C page 
 %3C= 0) %7B%0A    
@@ -1716,24 +1716,234 @@
 t();%0A    %7D%0A%0A
+    private int getItemsPerPage(Integer itemsPerPage) %7B%0A        if (itemsPerPage == null %7C%7C itemsPerPage %3C= 0) %7B%0A            itemsPerPage = DEFAULT_ITEMS_PER_PAGE;%0A        %7D%0A        return itemsPerPage;%0A    %7D%0A%0A
     @Request
@@ -2304,62 +2304,8 @@
 ) %7B%0A
-        itemsPerPage = getItemsPerPage(itemsPerPage);%0A
     
@@ -2458,217 +2458,7 @@
    %7D
-%0A%0A    private int getItemsPerPage(Integer itemsPerPage) %7B%0A        if (itemsPerPage == null %7C%7C itemsPerPage %3C= 0) %7B%0A            itemsPerPage = DEFAULT_ITEMS_PER_PAGE;%0A        %7D%0A        return itemsPerPage;%0A    %7D
 %0A%7D%0A

fix pagination