package cn.com.qmth.examcloud.core.questions.api.controller;

import cn.com.qmth.examcloud.commons.util.JsonMapper;
import cn.com.qmth.examcloud.core.questions.service.ExtractConfigProviderService;
import cn.com.qmth.examcloud.core.questions.service.bean.extract.ExtractConfigPaper;
import cn.com.qmth.examcloud.core.questions.service.cache.*;
import cn.com.qmth.examcloud.support.CacheConstants;
import cn.com.qmth.examcloud.support.cache.CacheHelper;
import cn.com.qmth.examcloud.support.cache.bean.*;
import cn.com.qmth.examcloud.web.redis.RedisClient;
import cn.com.qmth.examcloud.web.support.Naked;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller
@RequestMapping("${api_cqb}/")
public class DemoController {

    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisClient redisClient;

    @Autowired
    private ExtractConfigCache extractConfigCache;

    @Autowired
    private ExtractConfigPaperCache extractConfigPaperCache;

    @Autowired
    private BasePaperCache basePaperCache;

    @Autowired
    private QuestionCache questionCache;

    @Autowired
    private QuestionAnswerCache questionAnswerCache;

    @Autowired
    private ExtractConfigProviderService extractConfigProviderService;

    @Naked
    @ResponseBody
    @RequestMapping(value = "/demo", method = RequestMethod.GET)
    public String demo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Long examId = 319L;
        String courseCode = "course-fds";
        String groupCode = "X";
        String paperId = "f4e8b8a3-9f2f-4455-a3c1-62942f1ed171";
        String questionId = "5d5e109aef8fce4eeda1ee12";

        ExtractConfigPaper extractConfigPaper = extractConfigProviderService.getDefaultPaper(examId, courseCode, groupCode);
        log.info("调卷的试卷结构缓存：" + extractConfigPaper.getDefaultPaper().getName());

        ExtractConfigCacheBean extractConfigCacheBean = CacheHelper.getExtractConfig(examId, courseCode);
        log.info("--->调卷规则缓存: " + extractConfigCacheBean.getDetails().size());
        //extractConfigCache.remove(examId, courseCode);

        ExtractConfigPaperCacheBean extractConfigPaperCacheBean = CacheHelper.getExtractConfigPaper(examId, courseCode, groupCode, paperId);
        log.info("--->调卷规则的试卷结构缓存: " + extractConfigPaperCacheBean.getDefaultPaper().getName());
        //extractConfigPaperCache.remove(examId, courseCode, groupCode, paperId);

        BasePaperCacheBean basePaperCacheBean = CacheHelper.getBasePaper(paperId);
        log.info("--->试卷结构缓存: " + basePaperCacheBean.getDefaultPaper().getName());
        //basePaperCache.remove(paperId);

        QuestionCacheBean questionCacheBean = CacheHelper.getQuestion(examId, courseCode, groupCode, questionId);
        log.info("--->试题缓存: " + questionCacheBean.getDefaultQuestion().getId());
        //questionCache.remove(examId, courseCode, groupCode, questionId);

        QuestionAnswerCacheBean questionAnswer = CacheHelper.getQuestionAnswer("5d5e109aef8fce4eeda1ee22");
        log.info("--->试题答案缓存: " + new JsonMapper().toJson(questionAnswer));
        //questionAnswerCache.remove(questionId);

        final String pKeys = CacheConstants.CACHE_Q_EXTRACT_CONFIG_PAPER + "*" + paperId;
        Set<String> paperKeys = redisTemplate.keys(pKeys);
        log.info(StringUtils.join(paperKeys, ","));

        final String qKeys = CacheConstants.CACHE_Q_QUESTION + "*" + questionId;
        Set<String> questionKeys = redisTemplate.keys(qKeys);
        log.info(StringUtils.join(questionKeys, ","));

        return "ok";
    }

}