package com.qmth.qrzk.repository.utils.zujuan;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import com.qmth.qrzk.repository.user.model.Question;
import com.qmth.qrzk.repository.utils.DateHelper;

/**
 * 针对旧题的承载集合
 * 
 * @author luoshi
 * 
 */
public class OldQuestionSet {

    /**
     * 按启用日期的年+月判断同一套试卷
     */
    private Map<String, ZujuanQuestionGroup> dateMap;

    /**
     * 根据外部指定的主索引对所有试题进行分类去重
     */
    private Map<String, Set<Integer>> primaryMap;

    private MapCounter<Integer> counter;

    private double usePercent;

    public OldQuestionSet(double usePercent) {
        this.dateMap = new HashMap<String, ZujuanQuestionGroup>();
        this.primaryMap = new HashMap<String, Set<Integer>>();
        this.counter = new MapCounter<Integer>();
        this.usePercent = usePercent;
    }

    public void addQuestion(Question question, String primaryKey) {
        if (question == null || question.getId() == null || question.getEnableDate() == null) {
            return;
        }

        String date = DateHelper.formatDate(question.getEnableDate(), DateHelper.yearMonthFormat);
        ZujuanQuestionGroup group = dateMap.get(date);
        if (group == null) {
            group = new ZujuanQuestionGroup(usePercent);
            dateMap.put(date, group);
        }
        // 根据时间分类去重后，才可添加本试题
        if (group.addQuestion(question)) {
            counter.incr(question.getId());

            Set<Integer> set = primaryMap.get(primaryKey);
            if (set == null) {
                set = new HashSet<Integer>();
                primaryMap.put(primaryKey, set);
            }
            set.add(question.getId());
        }
    }

    public Question selectQuestion(String primaryKey) {
        // 根据主索引获取ID库
        Set<Integer> set = primaryMap.get(primaryKey);
        if (set == null) {
            return null;
        }
        Random rand = new Random();
        Question q = null;
        int index = rand.nextInt(set.size());
        int times = 0;
        while(q == null && times < set.size()) {
        	times++;
            boolean flag = true;
            for (ZujuanQuestionGroup group : dateMap.values()) {
                // 循环在每个分组集合中尝试该ID
                flag = group.test(index) && flag;
            }
            if (flag) {
                // 该ID可用，则返回对应的试题对象
                for (ZujuanQuestionGroup group : dateMap.values()) {
                    Question used = group.useQuestion(index);
                    if (used != null) {
                        q = used;
                    }
                }
                return q;
            } else {
                continue;
            }
        }
        /*for (Integer id : set) {
            // 循环判断所有ID
            boolean flag = true;
            for (ZujuanQuestionGroup group : dateMap.values()) {
                // 循环在每个分组集合中尝试该ID
                flag = group.test(id) && flag;
            }
            if (flag) {
                // 该ID可用，则返回对应的试题对象
                Question q = null;
                for (ZujuanQuestionGroup group : dateMap.values()) {
                    Question used = group.useQuestion(id);
                    if (used != null) {
                        q = used;
                    }
                }
                return q;
            } else {
                continue;
            }
        }*/
        return null;
    }
}
