package me.yoqi.android.jkbd.biz;

import java.util.List;

import me.yoqi.android.jkbd.MyApp;
import me.yoqi.android.jkbd.bean.Question;
import me.yoqi.android.jkbd.dao.ExamDao;
import me.yoqi.android.jkbd.dao.IExamDao;


/**
 * Created by LEO on 2017/6/30.
 */

public class ExamBiz implements IExamBiz {
    IExamDao dao;

    int index=0;
    List<Question> questionList;

    public ExamBiz() {
        this.dao = new ExamDao();
    }
    @Override
    public int getIndex() {
        return index;
    }

    @Override
    public void ExamStart() {
        index=0;
        dao.loadExamInfo();
        dao.loadQuestionLists();
    }

    @Override
    public void Pre() {
        if(index>0){
            index--;
        }
    }

    @Override
    public void Next() {
        this.getQuestion();
        if(index>=0&&index<questionList.size()-1){
            index++;
        }
    }

    @Override
    public int commit() {
        int score=0;
        for(Question question:questionList){
            String userAnswer=question.getUserAnswer();
            if(userAnswer!=null&&!userAnswer.equals("")){
                if(question.getAnswer().equals(userAnswer)){
                    score++;
                }
            }
        }

        return score;
    }


    @Override
    public Question getQuestion() {
        questionList= MyApp.instance.getQuestionList();
        if(questionList.get(index)!=null){
            return questionList.get(index);
        }
        return null;
    }

    @Override
    public Question getQuestion(int index) {
        questionList=MyApp.instance.getQuestionList();
        this.index=index;
        if(questionList.get(this.index)!=null){
            return questionList.get(this.index);
        }
        return null;
    }
}
