package com.ruoyi.web.controller.ie;

import com.google.common.collect.Lists;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.PaperquesType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ie.domain.Questions;
import com.ruoyi.ie.service.IPapersService;
import com.ruoyi.ie.service.IQuestionsService;
import com.ruoyi.mingxue.domain.QuestionCollection;
import com.ruoyi.mingxue.service.IQuestionCollectionService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "前端V2 题库")
@RestController
@RequestMapping("front/questions")
public class FrontQuestionsController extends BaseController {
    @Autowired
    private IQuestionsService questionsService;
    @Autowired
    private IQuestionCollectionService questionCollectionService;


    @ApiOperation("通用接口：根据知识点ID获取题目知识点树")
    @GetMapping("/getQuestionByCommonId")
    public TableDataInfo getQuestionByCommonId(@ApiParam("知识点Id") Long id,Integer paperType,Integer pageNum, Integer pageSize){
        Map map = new HashMap();
        map.put("paperId",id);

        if(paperType == null){
            map.put("type", paperType);
        }else {
            //分类:1 know_test-知识点,2 auto-基础题库,3 high_error-高频错题,4 smart_test-智能刷题,5 paper-真题,试卷
            switch (paperType){
                case 1:
                    map.put("type",PaperquesType.know_test.name());
                    break;
                case 2:
                    map.put("type",PaperquesType.auto.name());
                    break;
                case 3:
                    map.put("type",PaperquesType.high_error.name());
                    break;
                case 4:
                    map.put("type",PaperquesType.smart_test.name());
                    break;
                case 5:
                    map.put("type",PaperquesType.paper.name());
                    break;
            }
        }
        //两个参数传时才有分页
        if(null!=pageNum&&null!=pageSize){
            startPage();
        }
        List<Questions> questionsList = questionsService.selectQuestionsByZtPaperId(map);
        return getDataTable(questionsList);
    }



    @ApiOperation("根据章节获取题库列表")
    @GetMapping("/listByChapter")
    public TableDataInfo listByChapter(Integer chapterId, @RequestParam("subjectId") Long subjectId, Integer diff, String qtpye, Integer pageNum, Integer pageSize,
            Long[] knowledgeIds, String searchTerm) {
        if (isNumber(searchTerm)) {
            List<Questions> list = new ArrayList<Questions>();
            Questions questions = questionsService.selectQuestionsByIdNoChange(Long.valueOf(searchTerm));
            if (questions != null) {
                list.add(questions);
                questionsService.replaceImage(list);
                questionsService.fillCollectInfo(SecurityUtils.getLoginUser().getUser().getUserId(), list);
            }
            return getDataTable(list);
        }
        // knowledgeIds 这里的knowledgeId是knowledge_basic的id
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 15;
        }
        Questions query = new Questions();
        query.setParse("needed");
        query.setChapterId(chapterId);
        query.setDiff(diff);
        query.setTitle(searchTerm);
        query.setQtpye(qtpye);
        if (ArrayUtils.isNotEmpty(knowledgeIds)) {
            query.setKnowledgeBasicIds(Lists.newArrayList(knowledgeIds));
        }
        List<Questions> list = null;

        questionsService.replaceImage(list);
        questionsService.fillCollectInfo(SecurityUtils.getLoginUser().getUser().getUserId(), list);
        return getDataTable(list);
    }


    @ApiOperation("根据知识点获取题库列表")
    @GetMapping("/listByKnowledge")
    public TableDataInfo listByKnowledge(Long knowledgeId, Integer diff, String qtpye, Integer pageNum, Integer pageSize, String searchTerm) {
        if (isNumber(searchTerm)) {
            List<Questions> list = new ArrayList<Questions>();
            Questions questions = questionsService.selectQuestionsByIdNoChange(Long.valueOf(searchTerm));
            if (questions != null) {
                list.add(questions);
                questionsService.replaceImage(list);
                questionsService.fillCollectInfo(SecurityUtils.getLoginUser().getUser().getUserId(), list);
            }
            return getDataTable(list);
        }
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 15;
        }
        Questions query = new Questions();
        query.setKnowledgeId(knowledgeId);
        query.setDiff(diff);
        query.setTitle(searchTerm);
        query.setQtpye(qtpye);
        List<Questions> list = questionsService.selectByKnowledgeTree(query, pageNum, pageSize);
        questionsService.replaceImage(list);
        questionsService.fillCollectInfo(SecurityUtils.getLoginUser().getUser().getUserId(), list);
        return getDataTable(list);
    }

    private boolean isNumber(String searchTerm) {
        if (StringUtils.isBlank(searchTerm)) {
            return false;
        }
        try {
            Long.valueOf(searchTerm);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @ApiOperation("根据id数组获取题目内容")
    @GetMapping("getQuestionsByIds")
    public AjaxResult getQuestionsByIds(@ApiParam("ids, 用逗号分隔，如1,2,3") @RequestParam Long[] ids) {
        List<Questions> questions = questionsService.selectByIds(ids);
        questionsService.replaceImage(questions);
        return AjaxResult.success(questions);
    }


    @ApiOperation("获取题目信息")
    @GetMapping(value = "/getQuestionInfo")
    public AjaxResult getQuestionInfo(Long id) {
        Questions questions = questionsService.getQuestionInfo(id);
        questionsService.replaceImage(Lists.newArrayList(questions));
        return AjaxResult.success(questions);
    }

    @ApiOperation("收藏试题")
    @PostMapping("collect")
    public AjaxResult collect(Long questionId) {
        QuestionCollection questionCollection = new QuestionCollection();
        questionCollection.setUserId(SecurityUtils.getLoginUser().getUser().getUserId());
        questionCollection.setQuestionId(questionId);
        if(CollectionUtils.isEmpty(questionCollectionService.selectQuestionCollectionList(questionCollection))) {
            return toAjax(questionCollectionService.insertQuestionCollection(questionCollection));
        }
        return toAjax(1);
    }

    @ApiOperation("取消收藏试题")
    @PostMapping("cancelCollect")
    public AjaxResult cancelQuestionCollection(Long questionId) {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        return toAjax(questionCollectionService.deleteQuestionCollectionById(questionId, userId));
    }
}
