import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

/*
  包含n个mutation的type名称常量
 */
export const RECEIVE_USER_INFO = 'receive_user_info' // 接收用户信息
export const RESET_USER_INFO = 'reset_user_info' // 重置用户信息
export const RECEIVE_EXAM_CALENDAR = 'receive_exam_calendar' // 接收考试日历数组
export const RECORD_EXAM_CALENDAR = 'record_exam_calendar' // 同步考试日历数组

export const INIT_SINGLE_ANSWERS_LENGTH = 'init_single_answers_length' // 初始化单选题答案数组长度
export const INIT_MULTIPLE_ANSWERS_LENGTH = 'init_multiple_answers_length' // 初始化多选题答案数组长度
export const INIT_JUDGE_ANSWERS_LENGTH = 'init_judge_answers_length' // 初始化判断题答案数组长度
export const INIT_FILL_ANSWERS_LENGTH = 'init_fill_answers_length' // 初始化填空题答案数组长度

export const NEXT_QUE = 'next_que' //点击下一题
export const PREV_QUE = 'prev_que' //点击上一题
export const CARD_QUE = 'card_que' //点击答题卡序号
export const RECORD_FIRST_CURRENT_TIME = 'record_first_current_time' //记录最先进入考试的开始时间

export const RECORD_SINGLE_ANSWERS = 'record_single_answers' // 记录单选题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值
export const RECORD_MULTIPLE_ANSWERS = 'record_multiple_answers' // 记录多选题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值
export const RECORD_JUDGE_ANSWERS = 'record_judge_answers' // 记录判断题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值
export const RECORD_FILL_ANSWERS = 'record_fill_answers' // 记录填空题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值

export const REFRESH_CURRENT_INDEX = 'refresh_current_index' //刷新从sessionStorage取出放入
export const REFRESH_SINGLE_ANSWERS = 'refresh_single_answers' //刷新从sessionStorage取出放入
export const REFRESH_MULTIPLE_ANSWERS = 'refresh_multiple_answers' //刷新从sessionStorage取出放入
export const REFRESH_JUDGE_ANSWERS = 'refresh_judge_answers' //刷新从sessionStorage取出放入
export const REFRESH_FILL_ANSWERS = 'refresh_fill_answers' //刷新从sessionStorage取出放入
export const REFRESH_FIRST_CURRENT_TIME = 'refresh_first_current_time' //刷新从sessionStorage取出放入

const store = new Vuex.Store({
    state:{
        userInfo: {},// 用户信息
        examCalendar: [],//考试日历列表数组
        currentIndex: 0,//当前题目索引，默认为0
        singleAnswers: [],//单选题答案数
        multipleAnswers: [],//多选题答案数组
        judgeAnswers: [],//判断题答案数组
        fillAnswers: [],//填空题答案数组
        firstCurrentTime:0,//花费时间
    },
    getters:{

    },
    mutations:{
        [RECEIVE_USER_INFO] (state, {userInfo}) {
            state.userInfo = userInfo
        }, 
        [RESET_USER_INFO]:(state) => {
            sessionStorage.clear(),
            state.userInfo = {}
        },
        [RECEIVE_EXAM_CALENDAR] (state, {examCalendar}){
            state.examCalendar = examCalendar;
        },
        [RECORD_EXAM_CALENDAR] (state, {examCalendar}) {
            state.examCalendar = examCalendar
        },
        [INIT_SINGLE_ANSWERS_LENGTH] (state, {singleNum}) {
            state.singleAnswers.length = singleNum;
        },
        [INIT_MULTIPLE_ANSWERS_LENGTH] (state, {multipleNum}) {
            state.multipleAnswers.length = multipleNum;
        },
        [INIT_JUDGE_ANSWERS_LENGTH] (state, {judgeNum}) {
            state.judgeAnswers.length = judgeNum;
        },
        [INIT_FILL_ANSWERS_LENGTH] (state, {fillNum}) {
            state.fillAnswers.length = fillNum;
        },
        [NEXT_QUE] (state) {
            state.currentIndex += 1;
        },
        [PREV_QUE] (state) {
            state.currentIndex -= 1;
        },
        [CARD_QUE] (state, {index}) {
            state.currentIndex = index - 1;
        },
        [RECORD_FIRST_CURRENT_TIME] (state, {firstCurrentTime}) {
            state.firstCurrentTime = firstCurrentTime;
        },
        [RECORD_SINGLE_ANSWERS] (state, {currentIndex, singleAnswer}) {
            state.singleAnswers.splice(currentIndex, 1, singleAnswer);
            // state.singleAnswers[currentIndex] = singleAnswer;
        },
        [RECORD_MULTIPLE_ANSWERS] (state, {currentIndex, multipleAnswer}) {
            // 注意下标要减去前面单选题数量
            state.multipleAnswers.splice(currentIndex - state.singleAnswers.length, 1, multipleAnswer);
            // state.multipleAnswers[currentIndex - state.singleAnswers.length] = multipleAnswer;
        },
        [RECORD_JUDGE_ANSWERS] (state, {currentIndex, judgeAnswer}) {
            // 注意下标要减去前面单选题数量和多选题数量
            state.judgeAnswers.splice(currentIndex - state.singleAnswers.length - state.multipleAnswers.length, 1, judgeAnswer);
            // state.judgeAnswers[currentIndex - state.singleAnswers.length - state.multipleAnswers.length] = judgeAnswer;
        },
        [RECORD_FILL_ANSWERS] (state, {currentIndex, fillAnswer}) {
            // 注意下标要减去前面单选题数量和多选题数量和判断题数量
            state.fillAnswers.splice(currentIndex - state.singleAnswers.length - state.multipleAnswers.length - state.judgeAnswers.length, 1, fillAnswer);
            // state.fillAnswers[currentIndex - state.singleAnswers.length - state.multipleAnswers.length - state.judgeAnswers.length] = fillAnswer;
        },
        [REFRESH_CURRENT_INDEX] (state, {currentIndex}) {
            state.currentIndex = currentIndex;
        },
        [REFRESH_SINGLE_ANSWERS] (state, {singleAnswers}) {
            state.singleAnswers = singleAnswers;
        },
        [REFRESH_MULTIPLE_ANSWERS] (state, {multipleAnswers}) {
            state.multipleAnswers = multipleAnswers;
        },
        [REFRESH_JUDGE_ANSWERS] (state, {judgeAnswers}) {
            state.judgeAnswers = judgeAnswers;
        },
        [REFRESH_FILL_ANSWERS] (state, {fillAnswers}) {
            state.fillAnswers = fillAnswers;
        },
        [REFRESH_FIRST_CURRENT_TIME] (state, {firstCurrentTime}) {
            state.firstCurrentTime = firstCurrentTime;
        }
    },
    actions:{
        // 同步记录用户信息
        login ({commit}, userInfo) {
             commit(RECEIVE_USER_INFO, {userInfo})
        },
        // 退出登录
        logout({commit}){
            commit(RESET_USER_INFO)
        },
        // //异步获取考试日历数组
        // async getExamCalendar({commit}, setStore){
        //     // 发送异步ajax请求
        //     const result = await reqExamCalendar()
        //     // 提交一个mutation
        //     if (result.statu === 0) {
        //     const examCalendar = result.data
        //     sessionStorage.setItem("examCalendar",JSON.stringify(examCalendar))
        //     commit(RECEIVE_EXAM_CALENDAR, {examCalendar})
        //     setStore && setStore()
        //     }
        // },
        //同步记录考试日历
        recordExamCalendar ({commit}, examCalendar) {
            commit(RECORD_EXAM_CALENDAR, {examCalendar})
        },

        //初始化单选题答案数组长度
        initSingleAnswersLength({commit}, singleNum) {
            commit(INIT_SINGLE_ANSWERS_LENGTH, {singleNum})
        },
        //初始化多选题答案数组长度
        initMultipleAnswersLength({commit}, multipleNum) {
            commit(INIT_MULTIPLE_ANSWERS_LENGTH, {multipleNum})
        },
        //初始化判断题答案数组长度
        initJudgeAnswersLength({commit}, judgeNum) {
            commit(INIT_JUDGE_ANSWERS_LENGTH, {judgeNum})
        },
        //初始化填空题答案数组长度
        initFillAnswersLength({commit}, fillNum) {
            commit(INIT_FILL_ANSWERS_LENGTH, {fillNum})
        },

        //点击下一题
        nextQue({commit}) {
            commit(NEXT_QUE)
        },
        //点击上一题
        prevQue({commit}) {
            commit(PREV_QUE)
        },
        //点击答题卡序号
        cardQue({commit}, index) {
            commit(CARD_QUE, {index})
        },
        //记录当前花费时间
        recordFirstCurrentTime({commit}, firstCurrentTime) {
            commit(RECORD_FIRST_CURRENT_TIME, {firstCurrentTime})
        },
        //记录单选题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值
        recordSingleAnswers({commit}, {currentIndex, singleAnswer}) {
            commit(RECORD_SINGLE_ANSWERS, {currentIndex, singleAnswer})
        },
        //记录多选题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值
        recordMultipleAnswers({commit}, {currentIndex, multipleAnswer}) {
            commit(RECORD_MULTIPLE_ANSWERS, {currentIndex, multipleAnswer})
        },
        //记录判断题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值
        recordJudgeAnswers({commit}, {currentIndex, judgeAnswer}) {
            commit(RECORD_JUDGE_ANSWERS, {currentIndex, judgeAnswer})
        },
        //记录填空题答案到数组，第一个参数为数组下标，第二个参数为当前下标的值
        recordFillAnswers({commit}, {currentIndex, fillAnswer}) {
            commit(RECORD_FILL_ANSWERS, {currentIndex, fillAnswer})
        },
        //刷新从sessionStorage取出放入
        refreshCurrentIndex({commit}, currentIndex) {
            commit(REFRESH_CURRENT_INDEX, {currentIndex})
        },
        //刷新从sessionStorage取出放入
        refreshSingleAnswers({commit}, singleAnswers) {
            commit(REFRESH_SINGLE_ANSWERS, {singleAnswers})
        },
        //刷新从sessionStorage取出放入
        refreshMultipleAnswers({commit}, multipleAnswers) {
            commit(REFRESH_MULTIPLE_ANSWERS, {multipleAnswers})
        },
        //刷新从sessionStorage取出放入
        refreshJudgeAnswers({commit}, judgeAnswers) {
            commit(REFRESH_JUDGE_ANSWERS, {judgeAnswers})
        },
        //刷新从sessionStorage取出放入
        refreshFillAnswers({commit}, fillAnswers) {
            commit(REFRESH_FILL_ANSWERS, {fillAnswers})
        },
        //刷新从sessionStorage取出放入
        refreshFirstCurrentTime({commit}, firstCurrentTime) {
            commit(REFRESH_FIRST_CURRENT_TIME, {firstCurrentTime})
        }
    },
})

export default store