package com.jh.radegast.api;

import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.jh.common.web.list.DefaultSorting;
import com.jh.common.web.list.ListHelper;
import com.jh.common.web.list.Page;
import com.jh.common.web.list.PagingInfo;
import com.jh.common.web.list.SortTrend;
import com.jh.radegast.model.Rating;
import com.jh.radegast.rating.RatingService;

@Controller
@RequestMapping("/api")
public class RatingApiController {

	@Autowired
	private RatingService service;
	
    @RequestMapping(path = "ratings", method = RequestMethod.GET)
    @DefaultSorting(field = "key", trend = SortTrend.DESCENDING)
    public ResponseEntity<List<Rating>> filterRatings(/*Date from, Date to, */PagingInfo pagingInfo) {
    	Page<Rating> page = service.filter(new RatingsFilter(/*from, to*/), pagingInfo);
    	return ListHelper.pagedResponse(page);
    }
	
	@RequestMapping(path = "ratings", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public void bulkRating(@RequestBody Collection<Rating> ratings) {
    	service.saveBulk(ratings);
    }

    @RequestMapping(path = "ratings/stats", method = RequestMethod.GET)
    public @ResponseBody RatingsStats ratingsStats(/*Date from, Date to */) {
    	return service.calculateStats(new RatingsFilter());
    }
    
}
