package com.qxgmat.controller.admin;

import com.alibaba.fastjson.JSONObject;
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.nuliji.tools.exception.ParameterException;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.dao.entity.Question;
import com.qxgmat.data.dao.entity.SentenceArticle;
import com.qxgmat.data.dao.entity.SentenceQuestion;
import com.qxgmat.data.dao.entity.Setting;
import com.qxgmat.data.relation.entity.SentenceQuestionRelation;
import com.qxgmat.dto.admin.request.ExerciseStructDto;
import com.qxgmat.dto.admin.request.ManagerDto;
import com.qxgmat.dto.admin.response.SentenceQuestionListDto;
import com.qxgmat.service.SentencePaperService;
import com.qxgmat.service.inline.*;
import com.qxgmat.task.AsyncTask;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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("AdminSentenceController")
@RequestMapping("/admin/sentence")
public class SentenceController {

    @Autowired
    private ManagerLogService managerLogService;
    @Autowired
    private SentenceQuestionService sentenceQuestionService;

    @Autowired
    private SentencePaperService sentencePaperService;

    @Autowired
    private SentenceArticleService sentenceArticleService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private SettingService settingService;

    @Autowired
    private AsyncTask asyncTask;

    @RequestMapping(value = "/article/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加长难句文章", httpMethod = "POST")
    public Response<SentenceArticle> addArticle(@RequestBody @Validated SentenceArticle dto, HttpServletRequest request) {
        SentenceArticle entity = Transform.convert(dto, SentenceArticle.class);
        entity = sentenceArticleService.add(entity);
        managerLogService.log(request);
        return ResponseHelp.success(Transform.convert(entity, SentenceArticle.class));
    }
    @RequestMapping(value = "/article/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "编辑长难句文章", httpMethod = "PUT")
    public Response<Boolean> editArticle(@RequestBody @Validated SentenceArticle dto, HttpServletRequest request) {
        SentenceArticle entity = Transform.convert(dto, SentenceArticle.class);
        entity = sentenceArticleService.edit(entity);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/article/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除长难句文章", httpMethod = "DELETE")
    public Response<Boolean> deleteArticle(@RequestParam int id, HttpServletRequest request) {
        sentenceArticleService.delete(id);
        managerLogService.log(request);
        return ResponseHelp.success(sentenceArticleService.delete(id));
    }

    @RequestMapping(value = "/article/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取长难句文章", httpMethod = "GET")
    public Response<SentenceArticle> detailArticle(@RequestParam int id, HttpSession session) {
        SentenceArticle entity = sentenceArticleService.get(id);
        return ResponseHelp.success(Transform.convert(entity, SentenceArticle.class));
    }
    @RequestMapping(value = "/article/list", method = RequestMethod.GET)
    @ApiOperation(value = "长难句文章列表", httpMethod = "GET")
    public Response<PageMessage<SentenceArticle>> listArticle(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) Number chapter,
            @RequestParam(required = false) Number part,
            HttpSession session) {
        Page<SentenceArticle> p = sentenceArticleService.select(page, size, chapter, part);
        List<SentenceArticle> pr = Transform.convert(p, SentenceArticle.class);
        return ResponseHelp.success(pr, page, size, p.getTotal());
    }

    @RequestMapping(value = "/question/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加长难句题目", httpMethod = "POST")
    public Response<SentenceQuestion> addQuestion(@RequestBody @Validated SentenceQuestionRelation dto, HttpServletRequest request) {
//        SentenceQuestion entity = Transform.dtoToEntity(dto);
        SentenceQuestion entity = sentencePaperService.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 SentenceQuestionRelation dto, HttpServletRequest request) {
//        SentenceQuestion entity = Transform.dtoToEntity(dto);
        SentenceQuestion entity = sentencePaperService.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<SentenceQuestionRelation> detailQuestion(@RequestParam int id, HttpSession session) {
        SentenceQuestion entity = sentenceQuestionService.get(id);
        SentenceQuestionRelation relation = sentenceQuestionService.relation(entity);
        return ResponseHelp.success(relation);
    }

    @RequestMapping(value = "/question/list", method = RequestMethod.GET)
    @ApiOperation(value = "长难句题目列表", httpMethod = "GET")
    public Response<PageMessage<SentenceQuestionListDto>> listQuestion(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            HttpSession session) {
        Page<SentenceQuestion> p = sentenceQuestionService.select(page, size);
        List<SentenceQuestionListDto> pr = Transform.convert(p, SentenceQuestionListDto.class);

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

    @RequestMapping(value = "/paper/auto", method = RequestMethod.POST)
    @ApiOperation(value = "自动组卷", httpMethod = "POST")
    public Response<Boolean> autoPaper(@RequestBody @Validated ExerciseStructDto dto, HttpServletRequest request) {
        // 判断当前组卷状态
        Setting setting = settingService.getByKey(SettingKey.SENTENCE_PAPER_STATUS);
        JSONObject status = setting.getValue();
        if (status.getInteger("process")<100){
            throw new ParameterException("组卷进行中，请稍后再试");
        }
        asyncTask.autoSentencePaper();

        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/paper/auto", method = RequestMethod.GET)
    @ApiOperation(value = "自动组卷状态", httpMethod = "GET")
    public Response<JSONObject> autoPaperStatus(HttpServletRequest request) {
        // 判断当前组卷状态
        Setting setting = settingService.getByKey(SettingKey.SENTENCE_PAPER_STATUS);
        JSONObject status = setting.getValue();

        return ResponseHelp.success(status);
    }
}
