package com.qxgmat.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.nuliji.tools.AbstractService;
import com.nuliji.tools.PageResult;
import com.nuliji.tools.exception.ParameterException;
import com.nuliji.tools.exception.SystemException;
import com.nuliji.tools.mybatis.Example;
import com.qxgmat.data.constants.enums.module.QuestionModule;
import com.qxgmat.data.dao.UserQuestionMapper;
import com.qxgmat.data.dao.entity.UserQuestion;
import com.qxgmat.data.dao.entity.UserReport;
import com.qxgmat.service.annotation.InitQuestion;
import com.qxgmat.service.inline.QuestionNoService;
import com.qxgmat.service.inline.SentenceQuestionService;
import com.qxgmat.util.annotation.Callback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserQuestionService extends AbstractService {
    private static final Logger logger = LoggerFactory.getLogger(UserQuestionService.class);

    @Resource
    private UserQuestionMapper userQuestionMapper;

    /**
     * 用户错题记录：相同题目的最后一次做题记录及其编号
     * @param page
     * @param size
     * @param userId
     * @return
     */
    public PageResult<UserQuestion> listError(int page, int size, Integer userId){
        return new PageResult<>(null, 0);
    }

    /**
     * 获取当前报告最后一题
     * @param userId
     * @param userReportId
     * @return
     */
    public UserQuestion getLastByReport(Integer userId, Integer userReportId){
        Example example = new Example(UserQuestion.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("paper_id", userReportId)
        );
        example.orderBy("id").desc();
        return one(userQuestionMapper, example);
    }


    /**
     * 根据用户report创建下一题
     * @param report
     * @param lastQuestion
     * @param IInitQuestion
     * @return
     */
    public UserQuestion addByReport(UserReport report, UserQuestion lastQuestion, InitQuestion IInitQuestion){
        UserQuestion question = UserQuestion.builder()
                .reportId(report.getId())
                .userId(report.getUserId())
                // 添加题目序号
                .no(lastQuestion != null ? lastQuestion.getNo() + 1: 1)
                .build();
        // 回调，根据模块更新设置
        boolean result = IInitQuestion.callback(question, report, lastQuestion);
        if (!result) {
            // 做题结束
            return null;
        }
        return add(question);
    }

    /**
     * 获取报告的所有做题记录
     * @param userId
     * @param userReportId
     * @return
     */
    public List<UserQuestion> listByReport(Integer userId, Integer userReportId){
        Example example = new Example(UserQuestion.class);
        example.and(
                example.createCriteria()
                        .andEqualTo("userId", userId)
                        .andEqualTo("paper_id", userReportId)
        );
        example.orderBy("id").asc();
        return select(userQuestionMapper, example);
    }

    public UserQuestion add(UserQuestion question){
        int result = insert(userQuestionMapper, question);
        question = one(userQuestionMapper, question.getId());
        if(question == null){
            throw new SystemException("用户题目添加失败");
        }
        return question;
    }

    public UserQuestion edit(UserQuestion question){
        UserQuestion in = one(userQuestionMapper, question.getId());
        if(in == null){
            throw new ParameterException("用户题目不存在");
        }
        int result = update(userQuestionMapper, question);
        return question;
    }

    public boolean delete(Number id){
        UserQuestion in = one(userQuestionMapper, id);
        if(in == null){
            throw new ParameterException("用户题目不存在");
        }
        int result = delete(userQuestionMapper, id);
        return result > 0;
    }

    public UserQuestion get(Number id){
        UserQuestion in = one(userQuestionMapper, id);

        if(in == null){
            throw new ParameterException("用户题目不存在");
        }
        return in;
    }

    public Page<UserQuestion> select(int page, int pageSize){
        return select(userQuestionMapper, page, pageSize);
    }

    public List<UserQuestion> select(Collection ids){
        return select(userQuestionMapper, ids);
    }

}
