package com.qxgmat.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.nuliji.tools.*;
import com.nuliji.tools.exception.ParameterException;
import com.qxgmat.data.constants.enums.QuestionType;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.dao.entity.*;
import com.qxgmat.data.relation.entity.TextbookQuestionRelation;
import com.qxgmat.dto.admin.extend.ExercisePaperExtendDto;
import com.qxgmat.dto.admin.extend.QuestionExtendDto;
import com.qxgmat.dto.admin.extend.QuestionNoExtendDto;
import com.qxgmat.dto.admin.request.ExerciseStructDto;
import com.qxgmat.dto.admin.request.QuestionNoSearchDto;
import com.qxgmat.dto.admin.response.ExercisePaperListDto;
import com.qxgmat.dto.admin.response.ExerciseQuestionListDto;
import com.qxgmat.dto.admin.response.TextbookPaperListDto;
import com.qxgmat.dto.admin.response.TextbookQuestionListDto;
import com.qxgmat.service.ExercisePaperService;
import com.qxgmat.service.TextbookPaperService;
import com.qxgmat.service.inline.*;
import com.qxgmat.task.AsyncTask;
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.Collection;
import java.util.List;

@RestController("AdminTextbookController")
@RequestMapping("/admin/textbookl")
@Api(tags = "机经接口", description = "机经相关", produces = MediaType.APPLICATION_JSON_VALUE)
public class TextbookController {

    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private TextbookQuestionService textbookQuestionService;

    @Autowired
    private TextbookPaperService textbookPaperService;

    @Autowired
    private AsyncTask asyncTask;

    @RequestMapping(value = "/question/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加机经题目", httpMethod = "POST")
    public Response<TextbookQuestion> addQuestion(@RequestBody @Validated TextbookQuestionRelation dto, HttpServletRequest request) {
//        SentenceQuestion entity = Transform.dtoToEntity(dto);
        TextbookQuestion entity = textbookPaperService.addQuestion(dto);
        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }
    @RequestMapping(value = "/question/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改机经题目", httpMethod = "PUT")
    public Response<Boolean> editQuestion(@RequestBody @Validated TextbookQuestionRelation dto, HttpServletRequest request) {
//        SentenceQuestion entity = Transform.dtoToEntity(dto);
        TextbookQuestion entity = textbookPaperService.editQuestion(dto);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

//    @RequestMapping(value = "/question/delete", method = RequestMethod.DELETE)
//    @ApiOperation(value = "删除机经题目", httpMethod = "DELETE")
//    public Response<Boolean> deleteQuestion(@RequestParam int id, HttpServletRequest request) {
//        managerLogService.log(request);
//        return ResponseHelp.success(sentenceQuestionService.delete(id));
//    }

    @RequestMapping(value = "/question/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取机经题目", httpMethod = "GET")
    public Response<TextbookQuestionRelation> detailQuestion(@RequestParam int id, HttpSession session) {
        TextbookQuestion entity = textbookQuestionService.get(id);
        TextbookQuestionRelation relation = textbookQuestionService.relation(entity);
        return ResponseHelp.success(relation);
    }

    @RequestMapping(value = "/question/list", method = RequestMethod.GET)
    @ApiOperation(value = "试题列表", httpMethod = "GET")
    public Response<PageMessage<TextbookQuestionListDto>> listQuestion(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) Integer paperId,
            @RequestParam(required = false) Integer questionNoId,
            @RequestParam(required = false, defaultValue = "id") String order,
            @RequestParam(required = false, defaultValue = "desc") String direction,
            HttpSession session) {
        Page<TextbookQuestion> p = textbookQuestionService.listAdmin(page, size, type, paperId, questionNoId, order, DirectionStatus.ValueOf(direction));
        List<TextbookQuestionListDto> pr = Transform.convert(p, TextbookQuestionListDto.class);

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

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

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

    @RequestMapping(value = "/paper/list", method = RequestMethod.GET)
    @ApiOperation(value = "练习册列表", httpMethod = "GET")
    public Response<PageMessage<TextbookPaperListDto>> listPaper(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false, defaultValue = "") String keyword,
            HttpSession session) {
        Page<TextbookPaper> p = textbookPaperService.select(page, size, keyword);
        List<TextbookPaperListDto> pr = Transform.convert(p, TextbookPaperListDto.class);

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