import request from './request'
import { orgType, defaultSet, kexiuUrl } from './index'

const professorUrl = kexiuUrl + '/ajax/professor/editBaseInfo/'
const agencyUrl = kexiuUrl + '/ajax/org/'

const cacheModule = {
  catchProfessor: {},
  catchAgency: {},
  getUserInfo(id, callback) {
    if (this.catchProfessor[id]) {
      if (this.catchProfessor[id].hasHeadImage) {
        this.catchProfessor[id].img = `${kexiuUrl}/images/head/${id}_l.jpg`
      } else {
        this.catchProfessor[id].img = defaultSet.img.user
      }
      callback(this.catchProfessor[id])
    } else {
      request.get(professorUrl + id, {}, response => {
        if (response.success && response.data) {
          const info = response.data
          if (info.hasHeadImage) {
            info.img = `${kexiuUrl}/images/head/${id}_l.jpg`
          } else {
            info.img = defaultSet.img.user
          }
          this.catchProfessor[id] = info
          callback(info)
        }
      })
    }
  },
  getAgencyInfo(id, callback) {
    if (this.catchAgency[id]) {
      if (this.catchAgency[id].hasOrgLogo) {
        this.catchAgency[id].img = `${kexiuUrl}/images/org/${id}.jpg`
      } else {
        this.catchAgency[id].img = defaultSet.img.org
      }
      this.catchAgency[id].orgType = orgType[this.catchAgency[id].orgType]
      callback(this.catchAgency[id])
    } else {
      request.get(agencyUrl + id, {}, response => {
        if (response.success && response.data) {
          const info = response.data
          if (info.hasOrgLogo) {
            info.img = `${kexiuUrl}/images/org/${id}.jpg`
          } else {
            info.img = defaultSet.img.org
          }
          info.orgType = orgType[info.orgType]
          this.catchProfessor[id] = info
          callback(info)
        }
      })
    }
  }
}

export default cacheModule
