package com.qxgmat.controller.api;


import com.nuliji.tools.*;
import com.qxgmat.data.constants.enums.module.PayModule;
import com.qxgmat.data.dao.entity.*;
import com.qxgmat.data.relation.entity.UserPreviewPaperRelation;
import com.qxgmat.dto.extend.UserPreviewPaperExtendDto;
import com.qxgmat.dto.response.*;
import com.qxgmat.help.ShiroHelp;
import com.qxgmat.service.*;
import com.qxgmat.service.inline.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/course")
@Api(tags = "课程", description = "课程接口")
public class CourseController {

    @Autowired
    private ShiroHelp shiroHelp;

    @Autowired
    private PreviewService previewService;

    @Autowired
    private QuestionNoService questionNoService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserClassService userClassService;

    @Autowired
    private UserPayService userPayService;


    @RequestMapping(value = "/process", method = RequestMethod.GET)
    @ApiOperation(value = "获取课程进度", notes = "获取所有课程及状态进度", httpMethod = "GET")
    public Response<List<UserClassDetailDto>> classProcess()  {
        User user = (User) shiroHelp.getLoginUser();
        List<UserClass> userClassList = userClassService.getByUser(user.getId());
        List<UserClassDetailDto> dtos = Transform.convert(userClassList, UserClassDetailDto.class);

        // 获取每个科目的最后2次作业
        Map<Object, Collection<UserPreviewPaperRelation>> previewMap = previewService.groupByCategory(user.getId(), 3);
        Transform.combine(dtos, previewMap, UserClassDetailDto.class, "category", "previews", UserPreviewPaperExtendDto.class);

        // 获取课程状态：已购买未开通
        List<UserPay> pays = userPayService.listUnUse(user.getId(), PayModule.CLASS);
        Collection ids = Transform.getIds(userClassList, UserClass.class, "category");
        for(UserPay pay : pays){
            Integer category = Integer.valueOf(pay.getModuleExtend());
            if (!ids.contains(category)){
                UserClassDetailDto dto = new UserClassDetailDto();
                dto.setCategory(category);
                dto.setPayed(true);
                dtos.add(dto);
            }
        }

        return ResponseHelp.success(dtos);
    }

    @RequestMapping(value = "/preview/list", method = RequestMethod.GET)
    @ApiOperation(value = "获取预习作业列表", notes = "获取预习作业列表", httpMethod = "GET")
    public Response<PageMessage<UserPreviewPaperExtendDto>> listPreview(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) Number category,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Boolean finish
    )  {
        User user = (User) shiroHelp.getLoginUser();
        PageResult<UserPreviewPaperRelation> p = previewService.list(page, size, category, user.getId(), endTime, finish);

        List<UserPreviewPaperExtendDto> pr = Transform.convert(p, UserPreviewPaperExtendDto.class);

        // 获取试卷统计信息
        Map map = Transform.getMap(p, UserPreviewPaperRelation.class, "id", "paper");
        Map<Integer, Integer[]> questionNoIdsMap = new HashMap<>();
        for(Object value : map.keySet()){
            Integer key = (Integer) value;
            PreviewPaper preview = (PreviewPaper) map.get(key);
            questionNoIdsMap.put(key, preview.getQuestionNoIds());
        }
        Map statMap = questionNoService.statPaperMap(questionNoIdsMap);
        Transform.combine(pr, statMap, UserPreviewPaperExtendDto.class, "id", "stat");

        return ResponseHelp.success(pr, page, size, p.getTotal());
    }

    @RequestMapping(value = "/preview/paper", method = RequestMethod.GET)
    @ApiOperation(value = "获取预习作业", notes = "获取预习作业", httpMethod = "GET")
    public Response<PaperBaseDto> detailPreview(
            @RequestParam(required = true, name="id") Integer paperId
    )  {
        User user = (User) shiroHelp.getLoginUser();
        PreviewPaper preview = previewService.get(paperId);
        PaperBaseDto paperDto = Transform.convert(preview, PaperBaseDto.class);

        return ResponseHelp.success(paperDto);
    }
}
