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.PreviewStatus;
import com.qxgmat.data.dao.entity.PreviewPaper;
import com.qxgmat.dto.admin.request.PreviewDto;
import com.qxgmat.dto.admin.response.PreviewDetailDto;
import com.qxgmat.dto.admin.response.PreviewListDto;
import com.qxgmat.service.PreviewService;
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 PreviewService previewService;

    @Autowired
    private QuestionNoService questionNoService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加预习作业", httpMethod = "POST")
    public Response<PreviewPaper> add(@RequestBody @Validated PreviewDto dto, HttpServletRequest request) {
        PreviewPaper entity = Transform.dtoToEntity(dto);
        entity = previewService.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 PreviewDto dto, HttpServletRequest request) {
        PreviewPaper entity = Transform.dtoToEntity(dto);
        entity = previewService.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(previewService.delete(id));
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取预习作业", httpMethod = "GET")
    public Response<PreviewDetailDto> detail(@RequestParam int id, HttpSession session) {
        PreviewPaper entity = previewService.get(id);
        PreviewDetailDto dto = Transform.convert(entity, PreviewDetailDto.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<PreviewListDto>> 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<PreviewPaper> p = previewService.select(page, size, category, PreviewStatus.ValueOf(status));
        List<PreviewListDto> pr = Transform.convert(p, PreviewListDto.class);

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