package com.qxgmat.controller.admin;


import com.nuliji.tools.*;
import com.nuliji.tools.exception.ParameterException;
import com.qxgmat.data.dao.entity.Question;
import com.qxgmat.data.dao.entity.QuestionNo;
import com.qxgmat.data.relation.entity.QuestionNoRelation;
import com.qxgmat.dto.admin.extend.QuestionNoExtendDto;
import com.qxgmat.dto.admin.request.QuestionDto;
import com.qxgmat.dto.admin.request.QuestionNoDto;
import com.qxgmat.dto.admin.request.QuestionNoSearchDto;
import com.qxgmat.dto.admin.response.QuestionDetailDto;
import com.qxgmat.service.ExercisePaperService;
import com.qxgmat.service.inline.ManagerLogService;
import com.qxgmat.service.inline.QuestionNoService;
import com.qxgmat.service.inline.QuestionService;
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 java.util.List;

@RestController("AdminQuestionController")
@RequestMapping("/admin/question")
@Api(tags = "题目接口", description = "题目相关", produces = MediaType.APPLICATION_JSON_VALUE)
public class QuestionController {

    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private ExercisePaperService exercisePaperService;

    @Autowired
    private QuestionNoService questionNoService;

    @Autowired
    private QuestionService questionService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加题目", httpMethod = "POST")
    public Response<Question> add(@RequestBody @Validated QuestionDto dto, HttpServletRequest request) {
        Question entity = Transform.dtoToEntity(dto);
        entity = questionService.add(entity);
        // 更新编号绑定
        questionNoService.bindQuestion(dto.getQuestionNoIds(), entity.getId());

        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }


    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改题目", httpMethod = "PUT")
    public Response<Boolean> edit(@RequestBody @Validated QuestionDto dto, HttpServletRequest request) {
        Question entity = Transform.dtoToEntity(dto);
        entity = questionService.edit(entity);
        // 更新编号绑定
        questionNoService.bindQuestion(dto.getQuestionNoIds(), entity.getId());
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取题目", httpMethod = "GET")
    public Response<QuestionDetailDto> detail(int id, HttpServletRequest request) {
        Question entity = questionService.get(id);
        QuestionDetailDto dto = Transform.convert(entity, QuestionDetailDto.class);

        List<QuestionNo> questionNoList = questionNoService.listByQuestion(entity.getId());
        Integer[] ids = new Integer[questionNoList.size()];
        for (int index = 0; index < questionNoList.size(); index++) {
            ids[index] = questionNoList.get(index).getId();
        }
        dto.setQuestionNoIds(ids);

//        List<QuestionNo> questionNoList = questionNoService.listByQuestion(entity.getId());
//        List<QuestionNoExtendDto> questionNos = Transform.convert(questionNoList, QuestionNoExtendDto.class);
//        dto.setQuestionNos(questionNos);

        return ResponseHelp.success(dto);
    }

    @RequestMapping(value = "/add/no", method = RequestMethod.POST)
    @ApiOperation(value = "添加编号", httpMethod = "POST")
    public Response<QuestionNo> addNo(@RequestBody @Validated QuestionNoDto dto, HttpServletRequest request) {
        QuestionNo entity = Transform.dtoToEntity(dto);
        entity = questionNoService.add(entity);

        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }

    @RequestMapping(value = "/delete/no", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除题目编号", httpMethod = "DELETE")
    public Response<Boolean> delete(int id, HttpServletRequest request) {
        questionNoService.delete(id);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/list/no", method = RequestMethod.POST)
    @ApiOperation(value = "题目编号列表：通过id/编号", httpMethod = "POST")
    public Response<List<QuestionNoExtendDto>> listNo(@RequestBody @Validated QuestionNoSearchDto dto, HttpServletRequest request) {
        List<QuestionNoRelation> questionNos;
        if (dto.getIds() != null && dto.getIds().length > 0){
           questionNos = questionNoService.listWithRelationByIds(dto.getIds());
        }else if(dto.getNos() != null && dto.getNos().length > 0){
            questionNos = questionNoService.listWithRelationByNos(dto.getNos(), dto.getModule());
        }else{
            throw new ParameterException("需要id或编号");
        }
        List<QuestionNoExtendDto> questionNoExtendDtos = Transform.convert(questionNos, QuestionNoExtendDto.class);

        return ResponseHelp.success(questionNoExtendDtos);
    }

    @RequestMapping(value = "/search/stem", method = RequestMethod.POST)
    @ApiOperation(value = "搜索题目编号列表：题干搜索", httpMethod = "POST")
    public Response<PageMessage<QuestionNoExtendDto>> searchStem(@RequestBody @Validated QuestionNoSearchDto dto, HttpServletRequest request) {
        PageResult<QuestionNoRelation> p = questionNoService.searchStem(dto.getPage(), dto.getSize(), dto.getContent());
        List<QuestionNoExtendDto> pr = Transform.convert(p, QuestionNoExtendDto.class);

        return ResponseHelp.success(pr, dto.getPage(), dto.getSize(), p.getTotal());
    }

    @RequestMapping(value = "/search/no", method = RequestMethod.POST)
    @ApiOperation(value = "搜索题目编号列表：题目编号搜索", httpMethod = "POST")
    public Response<PageMessage<QuestionNoExtendDto>> searchNo(@RequestBody @Validated QuestionNoSearchDto dto, HttpServletRequest request) {
        PageResult<QuestionNoRelation> p = questionNoService.searchNo(dto.getPage(), dto.getSize(), dto.getNo(), dto.getModule());
        List<QuestionNoExtendDto> pr = Transform.convert(p, QuestionNoExtendDto.class);

        return ResponseHelp.success(pr, dto.getPage(), dto.getSize(), p.getTotal());
    }
}
