package com.qxgmat.controller.admin;

import com.github.pagehelper.Page;
import com.nuliji.tools.PageMessage;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.qxgmat.data.constants.enums.status.HomeworkPreviewStatus;
import com.qxgmat.data.dao.entity.HomeworkPreview;
import com.qxgmat.data.relation.entity.QuestionNoRelation;
import com.qxgmat.dto.admin.extend.QuestionNoExtendDto;
import com.qxgmat.dto.admin.request.HomeworkPreviewDto;
import com.qxgmat.dto.admin.response.HomeworkPreviewDetailDto;
import com.qxgmat.dto.admin.response.HomeworkPreviewListDto;
import com.qxgmat.service.HomeworkPreviewService;
import com.qxgmat.service.inline.ManagerLogService;
import com.qxgmat.service.inline.QuestionNoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController("AdminPreviewController")
@RequestMapping("/admin/preview")
@Api(tags = "预习作业接口", description = "设置预习作业", produces = MediaType.APPLICATION_JSON_VALUE)
public class PreviewController {
    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private HomeworkPreviewService homeworkPreviewService;

    @Autowired
    private QuestionNoService questionNoService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加预习作业", httpMethod = "POST")
    public Response<HomeworkPreview> add(@RequestBody @Validated HomeworkPreviewDto dto, HttpServletRequest request) {
        HomeworkPreview entity = Transform.dtoToEntity(dto);
        entity = homeworkPreviewService.edit(entity);
        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }

    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "编辑预习作业", httpMethod = "PUT")
    public Response<Boolean> edit(@RequestBody @Validated HomeworkPreviewDto dto, HttpServletRequest request) {
        HomeworkPreview entity = Transform.dtoToEntity(dto);
        entity = homeworkPreviewService.edit(entity);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除预习作业", httpMethod = "DELETE")
    public Response<Boolean> delete(@RequestParam int id, HttpServletRequest request) {
        managerLogService.log(request);
        return ResponseHelp.success(homeworkPreviewService.delete(id));
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取预习作业", httpMethod = "GET")
    public Response<HomeworkPreviewDetailDto> detail(@RequestParam int id, HttpSession session) {
        HomeworkPreview entity = homeworkPreviewService.get(id);
        HomeworkPreviewDetailDto dto = Transform.convert(entity, HomeworkPreviewDetailDto.class);

//        List<QuestionNoRelation> questionNos = questionNoService.listWithRelationByIds(entity.getQuestionNoIds());
//        List<QuestionNoExtendDto> questionNoExtendDtos = Transform.convert(questionNos, QuestionNoExtendDto.class);
//
//        dto.setQuestionNos(questionNoExtendDtos);

        return ResponseHelp.success(dto);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "预习作业列表", httpMethod = "GET")
    public Response<PageMessage<HomeworkPreviewListDto>> list(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) Integer category,
            @RequestParam(required = false) Integer status,
            HttpSession session) {
        Page<HomeworkPreview> p = homeworkPreviewService.select(page, size, category, HomeworkPreviewStatus.ValueOf(status));
        List<HomeworkPreviewListDto> pr = Transform.convert(p, HomeworkPreviewListDto.class);

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