/**
 * Created by luyanan on 18/8/27.
 * bridge、server、device
 */
/* eslint-disable one-var */
import request from '@/utils/request'
import { queryUserOne } from '@/api/sysSetting'
import { queryNormalOne, queryBlackOne } from '@/api/userInfo'

var objCache = {
  adminUser: {},
  normalOne: {},
  blackOne: {}
}
var objHcache = {
  adminUser: {},
  normalOne: {},
  blackOne: {}
}
var objCacheHandler = {
  adminUser: function(id) {
    var hc = objHcache.adminUser[id]
    request.get(queryUserOne, {
      id: id
    }, function(data) {
      delete objHcache.adminUser[id]
      if (data.success) {
        objCache.adminUser[id] = data.data
        for (let i = 0; i < hc.length; ++i) {
          hc[i](true, data.data)
        }
      } else {
        for (let i = 0; i < hc.length; ++i) {
          hc[i](false)
        }
      }
    }, function() {
      for (let i = 0; i < hc.length; ++i) {
        hc[i](false)
      }
    })
  },
  normalOne: function(id) {
    var hc = objHcache.normalOne[id]
    request.get(queryNormalOne, {
      id: id
    }, function(data) {
      delete objHcache.normalOne[id]
      if (data.success) {
        objCache.normalOne[id] = data.data
        for (let i = 0; i < hc.length; ++i) {
          hc[i](true, data.data)
        }
      } else {
        for (let i = 0; i < hc.length; ++i) {
          hc[i](false)
        }
      }
    }, function() {
      for (let i = 0; i < hc.length; ++i) {
        hc[i](false)
      }
    })
  },
  blackOne: function(id) {
    var hc = objHcache.blackOne[id]
    request.get(queryBlackOne, {
      id: id
    }, function(data) {
      delete objHcache.blackOne[id]
      if (data.success) {
        objCache.blackOne[id] = data.data
        for (let i = 0; i < hc.length; ++i) {
          hc[i](true, data.data)
        }
      } else {
        for (let i = 0; i < hc.length; ++i) {
          hc[i](false)
        }
      }
    }, function() {
      for (let i = 0; i < hc.length; ++i) {
        hc[i](false)
      }
    })
  }
}
var cacheModel = {
  getAdminUser: function(id, handler) {
    var data = objCache.adminUser[id]
    if (data) {
      handler(true, data)
    } else {
      if (objHcache.adminUser[id]) {
        objHcache.adminUser[id].push(handler)
      } else {
        objHcache.adminUser[id] = []
        objHcache.adminUser[id].push(handler)
        objCacheHandler.adminUser(id)
      }
    }
  },
  getNormalOne: function(id, handler) {
    var data = objCache.normalOne[id]
    if (data) {
      handler(true, data)
    } else {
      if (objHcache.normalOne[id]) {
        objHcache.normalOne[id].push(handler)
      } else {
        objHcache.normalOne[id] = []
        objHcache.normalOne[id].push(handler)
        objCacheHandler.normalOne(id)
      }
    }
  },
  getBlackOne: function(id, handler) {
    var data = objCache.blackOne[id]
    if (data) {
      handler(true, data)
    } else {
      if (objHcache.blackOne[id]) {
        objHcache.blackOne[id].push(handler)
      } else {
        objHcache.blackOne[id] = []
        objHcache.blackOne[id].push(handler)
        objCacheHandler.blackOne(id)
      }
    }
  }
}

export default cacheModel
