package cn.iselab.mooctest.user.web.ctrl;

import cn.iselab.mooctest.rpc.user.data.AddIntegralDTO;
import cn.iselab.mooctest.rpc.user.data.IntegralCaughtDTO;
import cn.iselab.mooctest.rpc.user.data.MedalDTO;
import cn.iselab.mooctest.rpc.user.data.UserIntegralDTO;
import cn.iselab.mooctest.user.configure.IntegralMap;
import cn.iselab.mooctest.user.constants.UrlConstants;
import cn.iselab.mooctest.user.model.Integral;
import cn.iselab.mooctest.user.web.logic.IntegralLogic;
import cn.iselab.mooctest.user.web.logic.MedalLogic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * @Author ROKG
 * @Description
 * @Date: Created in 下午7:28 2018/2/12
 * @Modified By:
 */
@RestController
public class IntegralController extends BaseController{

    @Autowired
    IntegralLogic integralLogic;

    @Autowired
    MedalLogic medalLogic;

    @Autowired
    IntegralMap integralMap;

    @RequestMapping(value = UrlConstants.API_INTEGRAL,method = RequestMethod.GET)
    public List<IntegralCaughtDTO> getIntegrals(@RequestParam(value = "userId")Long userId,
                                                @RequestParam(value = "startTime")Long startTime,
                                                @RequestParam(value = "endTime")Long endTime,
                                                HttpServletRequest request){
        Integer activePage = Integer.parseInt(request.getHeader("activePage"));
        Integer rowsOnPage = Integer.parseInt(request.getHeader("rowsOnPage"));
        Sort sort=new Sort(Sort.Direction.DESC,"total");
        Pageable pageable=new PageRequest(activePage-1, rowsOnPage,sort);
        return integralLogic.getIntegralCaughts(userId,startTime,endTime);
    }

    @RequestMapping(value = UrlConstants.API_MEDAL,method = RequestMethod.GET)
    public List<MedalDTO> getIntegrals(@RequestParam(value = "userId")Long userId,
                                       @RequestParam(value = "keyword",required = false)String keyword,
                                       HttpServletRequest request){
        return medalLogic.getMedals(keyword,userId);
    }

    @RequestMapping(value = UrlConstants.API_MEDAL,method = RequestMethod.DELETE)
    public MedalDTO getIntegrals(@RequestParam(value = "medalId")Long medalId,
                                       HttpServletRequest request){
        return medalLogic.getMedal(medalId);
    }

    @RequestMapping(value = UrlConstants.API_INTEGRAL,method = RequestMethod.PUT)
    public Map<String, Integral> getIntegrals(){
        return integralLogic.reloadIntegralMap();
    }

    @RequestMapping(value = "test",method = RequestMethod.GET)
    public Map<String, Integral> getIntegralMap(){
        return integralMap.getMap();
    }
}
