package com.ssj.aijia.kmt.knowledge.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.kmt.dto.KnowHowDTO;
import com.ssj.service.kmt.dto.KnowHowDetailsDTO;
import com.ssj.service.kmt.dto.KnowledgeGradeDto;
import com.ssj.service.kmt.request.KnowledgeRequest;
import com.ssj.service.kmt.request.TestPointRequest;
import com.ssj.service.kmt.service.KmtKnowledgeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 *
 * kmt知识点功能
 * @author chchen
 * 2019-01-14
 */
@Api(value = "kmt练习册知识点相关的接口", tags = "kmt练习册知识点相关的接口")
@RestController
@RequestMapping("/api/kmt/knowledge")
public class KmtKnowledgeController extends BaseController {

    @Reference(version = "1.0" ,timeout = 60000)
    private KmtKnowledgeService kmtKnowledgeService;

    @ApiOperation(value = "请求知识点筛选条件")
    @RequestMapping(value = "getKnowledgeFilter", method = RequestMethod.POST)
    public ResponseEntity getKnowledgeFilter(@RequestParam String accessToken) {
        ResponseEntity responseEntity = new ResponseEntity();
        try {
            List<KnowledgeGradeDto> data = new ArrayList<>();
            data.add(new KnowledgeGradeDto("一年级"));
            data.add(new KnowledgeGradeDto("二年级"));
            data.add(new KnowledgeGradeDto("三年级"));
            data.add(new KnowledgeGradeDto("四年级"));
            data.add(new KnowledgeGradeDto("五年级"));
            data.add(new KnowledgeGradeDto("六年级"));
            Map<String, Object> resultMap = new HashMap<>(16);
            resultMap.put("list", data);

            List<Map<String, String>> yearList = new LinkedList<>();
            String[] arrays = {"2016-2017", "2017-2018", "2018-2019", "2019-2020", "2020-2021"};
            for (String item : arrays){
                Map<String, String> yearItem = new HashMap<>();
                yearItem.put("yearName", item);
                yearList.add(yearItem);
            }
            resultMap.put("yearList", yearList);

            String progressListString="[{\"progress\":\"已录入\"},{\"progress\":\"已购买\"},{\"progress\":\"已对接\"},{\"progress\":\"已接收\"},{\"progress\":\"已上后台\"},{\"progress\":\"已发布\"}]";
            JSONArray progressList = JSONObject.parseArray(progressListString);
            resultMap.put("progressList", progressList);
            String semesterListString="[{\"name\":\"上册\",\"type\":1},{\"name\":\"下册\",\"type\":2}]";
            JSONArray semesterList = JSONObject.parseArray(semesterListString);
            resultMap.put("semesterList", semesterList);
            String bookTypeListString="[{\"name\":\"整本\",\"type\":0},{\"name\":\"上半本\",\"type\":1},{\"name\":\"下半本\",\"type\":2},{\"name\":\"不同\",\"type\":3}]";
            JSONArray bookTypeList = JSONObject.parseArray(bookTypeListString);
            resultMap.put("bookTypeList", bookTypeList);
            String bookYearListString="[{\"bookYear\":2019},{\"bookYear\":2020},{\"bookYear\":2021}]";
            JSONArray bookYearList = JSONObject.parseArray(bookYearListString);
            resultMap.put("bookYearList", bookYearList);

            responseEntity.success(resultMap, "查询成功");
        } catch (Exception e) {
            logger.error("请求知识点筛选条件异常：", e);
            responseEntity.failure(ResponseConstant.CODE_500,"服务器繁忙，请稍后重试");
        }
        return responseEntity;

    }

    @ApiOperation(value = "查询知识点")
    @RequestMapping(value = "getKnowledge", method = RequestMethod.POST)
    public ResponseEntity getKnowledge(@RequestParam String accessToken, @RequestBody KnowledgeRequest request) {
        ResponseEntity responseEntity = new ResponseEntity();
        try {
            List<KnowHowDTO> knowHowDTOList = new ArrayList<>();
            if (StringUtils.isNotBlank(request.getGrade()) && request.getVersionType() != null
                    && request.getSemester() != null  && StringUtils.isNotBlank(request.getSubject())){
                knowHowDTOList = kmtKnowledgeService.findSortKnowledgeByCondition(request);
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", knowHowDTOList);
            responseEntity.success(resultMap, "查询成功");
        } catch (Exception e) {
            logger.error("查询知识点异常：", e);
            responseEntity.failure(ResponseConstant.CODE_500,"服务器繁忙，请稍后重试");
        }
        return responseEntity;
    }


    @ApiOperation(value = "查询知识点列表的考点")
    @RequestMapping(value = "getTestingPoint", method = RequestMethod.POST)
    public ResponseEntity getTestPoint(@RequestParam String accessToken, @RequestBody TestPointRequest request) {
        ResponseEntity responseEntity = new ResponseEntity();
        try {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", kmtKnowledgeService.findDetailsListWithKd(request.getDetailIds()));
            responseEntity.success(resultMap, "查询成功");
        } catch (Exception e) {
            logger.error("查询知识点列表的考点异常：", e);
            responseEntity.failure(ResponseConstant.CODE_500,"服务器繁忙，请稍后重试");
        }
        return responseEntity;

    }
}
