import HelperUtil from '../../utils/HelperUtil'
import {USER_KEY} from '../../utils/constantCollection'
import UserAPI from '../../api/restApis/UserAPI'
import {USER_AUTHORITY_KEY} from '../../utils/constantCollection'
import WebSocketService from './WebSocketService'
import DeviceStateSocketApi from '../../api/websocketApis/DeviceStateSocketApi'

// 静态方法
let staticMethods = {
  // 获取全部用户
  getAllUserCollection: function () {
    return UserAPI.getAllUserCollection().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, USER_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 添加用户
  addUser: function (user) {
    return UserAPI.postAddUser(user).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 删除用户
  deleteUser: function (userKeyList) {
    console.log('调用删除函数')
    return UserAPI.deleteUser(userKeyList).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 修改用户
  modifyUser: function (user) {
    return UserAPI.putModifyUser(user).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 条件查询用户信息
  conditionQueryUser: function (selectList) {
    return UserAPI.putConditionQueryUser(selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, USER_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 重置用户密码
  resetUserPassword: function (user) {
    return UserAPI.putResetUserPassword(user).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 登录
  login: function (arr) {
    // return UserAPI.getLogin(userName,pwd).then(result => {
    //   if (result) {
    //     window.sessionStorage.setItem("token", '12');
    //   } else {
    //     return HelperUtil.returnPromiseRejectObj(result);
    //   }
    // })
    // return UserAPI.getLogin(userName, pwd).then(result => {
    return UserAPI.getLogin(arr).then(result => {
      if (result.data.result) {
        // 将token 存储在 sessionStorage 中
        window.sessionStorage.setItem('token', result.data.returnValue)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },
  // 注销
  logout: function () {
    return UserAPI.postLogout().then(result => {
      if (result.data.result) {
        // 将 token 从 sessionStorage 中去除
        window.sessionStorage.removeItem('token')
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    }).catch(err => {
      this.InfoTip.errorTip(this, err)
    })
  },

  // 用户权限获取
  getAllUserAuthority: function () {
    return UserAPI.getAllUserAuthority().then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue, USER_AUTHORITY_KEY)
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 获取个人信息
  getUserInfo: function () {
    return UserAPI.getUserInfo().then(result => {
      if (result.data.result) {
        return result.data.returnValue
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 修改个人信息
  modifyUserInfo: function (userInfo) {
    return UserAPI.putModifyUserInfo(userInfo).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 修改个人密码
  modifyUserPwd: function (arr) {
    return UserAPI.putModifyUserPwd(arr).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  },

  // 认证用户名、密码
  checkPwd: function (pwd) {
    return UserAPI.getCheckPwd(pwd).then(result => {
      if (result.data.result) {
      } else {
        return HelperUtil.returnPromiseRejectObj(result)
      }
    })
  }
}

function UserService (websocketService = null) {
  let listener = 'userServiceWs'
  // 修改用户权限
  function subscribeModifyAuthority (mutator = () => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_AUTHORITY, listener, (returnObject) => {
      mutator(returnObject)
    })
  }
  function unsubscribeModifyAuthority () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_AUTHORITY, listener)
  }
  // 已登录用户密码修改
  function subscribeModifyUserPwd (mutator = () => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_USER_PASSWORD, listener, (returnObject) => {
      mutator(returnObject)
    })
  }
  function unsubscribeModifyUserPwd () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_USER_PASSWORD, listener)
  }
  // 已登录用户用户组修改
  function subscribeModifyUserGroup (mutator = () => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.MODIFY_USER_GROUP, listener, (returnObject) => {
      mutator(returnObject)
    })
  }
  function unsubscribeModifyUserGroup () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.MODIFY_USER_GROUP, listener)
  }
  // 相同用户登录的时候把前一个用户主动退出
  function subscribeSameUserLogin (mutator = () => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.SAME_USER_LOGIN, listener, (returnObject) => {
      mutator(returnObject)
    })
  }
  function unsubscribeSameUserLogin () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.SAME_USER_LOGIN, listener)
  }
  // 用户被删除，主动退出
  function subscribeDeleteUser (mutator = () => {}) {
    websocketService.subscribe(DeviceStateSocketApi.MsgType.DELETE_USER, listener, (returnObject) => {
      mutator(returnObject)
    })
  }
  function unsubscribeDeleteUser () {
    websocketService.unsubscribe(DeviceStateSocketApi.MsgType.DELETE_USER, listener)
  }
  return Object.freeze(Object.assign(
    {
      // object methods comes here
      subscribeModifyAuthority,
      unsubscribeModifyAuthority,
      subscribeModifyUserPwd,
      unsubscribeModifyUserPwd,
      subscribeModifyUserGroup,
      unsubscribeModifyUserGroup,
      subscribeSameUserLogin,
      unsubscribeSameUserLogin,
      subscribeDeleteUser,
      unsubscribeDeleteUser
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(UserService, staticMethods)
export default UserService
