import {
  getDicts
} from '@/api/system/dict/data'
import * as gradeAxios from '@/api/webApi/grade'
import * as system from '@/api/webApi/system'
import {
  listGroups
} from '@/api/webApi/selection.js'
import consts from '@/common/mx-const.js'
import { loadAllMajorSubjectTree } from '@/api/webApi/test-major'

export default {
  // 类似全局缓存
  methods: {
    //CORE API
    _generateCacheKey(type) {
      return consts.keys.keyCachePrefix + type
    },
    async _getDataByCache(type, rqFunc, rpHandle, useCache = true) {
      const key = this._generateCacheKey(type)
      let cache = localStorage.getItem(key)?.castObject()
      if (useCache && cache) return cache
      let res = await rqFunc()
      if (rpHandle && typeof rpHandle === 'function') {
        res = rpHandle(res)
      }
      const resStr = typeof res === 'string' ? res : JSON.stringify(res)
      localStorage.setItem(key, resStr)
      return res
    },
    _getDictsByCache(type) {
      return this._getDataByCache(type,
        _ => getDicts(type),
        res => res.data.map(item => ({
          label: item.dictLabel,
          value: item.dictValue
        })))
    },
    clearUserCache() {
      for (let localStorageKey in localStorage) {
        if (localStorageKey.startsWith(consts.keys.keyCachePrefix)) {
          localStorage.removeItem(localStorageKey)
        }
      }
    },
    //班主任申请相关
    getFormTypeListByCache(useCache = true) {
      return this._getDictsByCache('form_type', useCache)
    },
    getFormStatusListByCache(useCache = true) {
      return this._getDictsByCache('form_status', useCache)
    },
    getFormAuditListByCache(useCache = true) {
      return this._getDictsByCache('form_audit', useCache)
    },
    // 通用词典
    getFileUploadTypeListByCache(useCache = true) {
      return this._getDictsByCache('file_upload_type', useCache)
    },
    getEvaluationTypeListByCache(useCache = true) {
      return this._getDictsByCache('evaluation_type', useCache)
    },
    getClassTypeListByCache(useCache = true) {
      return this._getDictsByCache('ClassType', useCache)
    },
    //当前用户的年级树
    getClassTreeByCache(useCache = true) {
      return this._getDataByCache('full_class_tree',
        gradeAxios.getAllGradeClasses,
        res => res.data, //TODO: 这里可以考虑换算为uni-data标准格式
        useCache)
    },
    // 选科
    getListGroupsByCache(useCache = true) {
      // 22.1.15 hht 从今天开始加入的数据存储为uni-data标准格式
      return this._getDataByCache('select_list_groups',
        listGroups,
        res => res.data.map(g => ({
          text: g.name,
          value: g.groupId,
          course0: g.course0,
          course1: g.course1,
        })),
        useCache)
    },
    getRoundStateByCache(useCache = true) {
      return this._getDataByCache('select_round_state',
        _ => [{
          text: '开启',
          value: '1'
        }, {
          text: '关闭',
          value: '0'
        }],
        null,
        useCache)
    },
    getAllMajorSubjectTreeByCache(useCache = true) {
      return this._getDataByCache('all_major_subject_tree',
        loadAllMajorSubjectTree, res => res.data,
        useCache)
    },
    // 科目、根据当前用户获取科目
    getUserSubjectListByCache(useCache = true) {
      return this._getDataByCache('user_subject_list',
        system.getSubjectsList,
        res => res.rows,
        useCache)
    },
    getPharseGradeListByCache(useCache = true) {
      return this._getDataByCache('pharse_grade_list',
        system.getPharseGradeList,
        res => res.rows,
        useCache)
    }
  }
}
