import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { isLogOut, notLogOut } from '@/utils/waitLogOut'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    username: '',
    phone: '',
    avatar: '',
    role: '',
    isOut: '',
    provinceId: '',
    cityId: '',
    areaId: '',
    streetId: '',
    interval: '',
    sysQueryConfiguration: '',
    sysQueryConfigurationStatus: '',
    whiteVisitorType: '',
    tagName: '',
    isControlEarlyWarning: null,
    adminId: '',
    placeType: '', // 场所类型  0通用1预约2楼宇
    placeCodeStatus: null, // 是否开启场所码，false：否，true: 是
    isOpenPrivateArea: null, // 是否开启私有区域 1-开启 2-不开启
    codeType: null, // 二维码类型记录标识，0：通用码，1：员工码
    threeCodeList: null, // 三码
    cardPhysics: null, // 卡号
    problemReportFlag: null, // 是否开启问题上报
    disinfectFlag: null, // 是否开启区域巡查
    isOpenExam: false, // 是否开启考试
    openInOutStatistics: false, // 是否启用人员进出统计数据
    buildingCheckType: 2, // 楼宇访客预约审核方式
    // 背景图片、标题等
    // title:'防控终端管理平台',
    // logo:'',
    // favicon:'',
    // bg:'',
    tenantEnterpriseName: null, // 上级名称-显示使用
    enterpriseName: null, // 下级名称-显示使用
    isOpenInoutWarning: false,
    isOpenLadder: false,
    WebSocketData: null
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_PHONE: (state, phone) => {
    state.phone = phone
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLE: (state, role) => {
    state.role = role
  },
  SET_ISOUT: (state, isOut) => {
    state.isOut = isOut
  },
  SET_PROVINCEID: (state, provinceId) => {
    state.provinceId = provinceId
  },
  SET_CITYID: (state, cityId) => {
    state.cityId = cityId
  },
  SET_AREAID: (state, areaId) => {
    state.areaId = areaId
  },
  SET_STREETID: (state, streetId) => {
    state.streetId = streetId
  },
  SET_ADDRESS: (state, address) => {
    state.address = address
  },
  SET_INTERVAL: (state, interval) => {
    state.interval = interval
  },
  SET_SQCONFIG: (state, sysQueryConfiguration) => {
    state.sysQueryConfiguration = sysQueryConfiguration
  },
  SET_SQCONFIGSTATUS: (state, sysQueryConfigurationStatus) => {
    state.sysQueryConfigurationStatus = sysQueryConfigurationStatus
  },
  SET_WHITEVISITORTYPE: (state, whiteVisitorType) => {
    state.whiteVisitorType = whiteVisitorType
  },
  SET_TAGNAME: (state, tagName) => {
    state.tagName = tagName
  },
  SET_ISCONTROLEARLYWARNING: (state, isControlEarlyWarning) => {
    state.isControlEarlyWarning = isControlEarlyWarning
  },
  SET_ADMINID: (state, adminId) => {
    state.adminId = adminId
  },
  SET_PLACETYPE: (state, placeType) => {
    state.placeType = placeType
  },
  SET_PLACECODESTATUS: (state, placeCodeStatus) => {
    state.placeCodeStatus = placeCodeStatus
  },
  SET_ISOPENPRIVATEAREA: (state, isOpenPrivateArea) => {
    state.isOpenPrivateArea = isOpenPrivateArea
  },
  SET_CODETYPE: (state, codeType) => {
    state.codeType = codeType
  },
  SET_THREECODELIST: (state, threeCodeList) => {
    state.threeCodeList = threeCodeList
  },
  SET_CARDPHYSICS: (state, cardPhysics) => {
    state.cardPhysics = cardPhysics
  },
  SET_PROBLEMREPORTFLAG: (state, problemReportFlag) => {
    state.problemReportFlag = problemReportFlag
  },
  SET_DISINFECTFIAG: (state, disinfectFlag) => {
    state.disinfectFlag = disinfectFlag
  },
  SET_ISOPENEXAM: (state, isOpenExam) => {
    state.isOpenExam = isOpenExam
  },
  SET_OPENINOUTSTATISTICS: (state, openInOutStatistics) => {
    state.openInOutStatistics = openInOutStatistics
  },
  SET_BUILDINGCHECKTYPE: (state, buildingCheckType) => {
    state.buildingCheckType = buildingCheckType
  },
  SET_TENANTENTERPRISENAME: (state, tenantEnterpriseName) => {
    state.tenantEnterpriseName = tenantEnterpriseName
  },
  SET_ENTERPRISENAME: (state, enterpriseName) => {
    state.enterpriseName = enterpriseName
  },
  SET_INOUTWARNING: (state, isOpenInoutWarning) => {
    state.isOpenInoutWarning = isOpenInoutWarning
  },
  SET_ISOPENLADDER: (state, isOpenLadder) => {
    state.isOpenLadder = isOpenLadder
  },
  SET_WEBSOCKETDATA: (state, WebSocketData) => {
    state.WebSocketData = WebSocketData
  }
  // SET_TITLE: (state, title) => {
  //   state.title = title
  // },
  // SET_LOGO: (state, logo) => {
  //   state.logo = logo
  // },
  // SET_FAVICON: (state, favicon) => {
  //   state.favicon = favicon
  // },
  // SET_BG: (state, bg) => {
  //   console.log(bg);
  //   state.bg = bg
  // },
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        // 标记登录,开始检测长时间未操作情况
        notLogOut()

        const { data } = response
        commit('SET_TAGNAME', data.tagName)
        commit('SET_TOKEN', data.token)
        commit('SET_USERNAME', data.username)
        commit('SET_NAME', data.name)
        commit('SET_AVATAR', data.avatar)
        commit('SET_ROLE', data.type)
        commit('SET_PHONE', data.phone)
        commit('SET_ISOUT', data.isOut)
        commit('SET_PROVINCEID', data.provinceId)
        commit('SET_CITYID', data.cityId)
        commit('SET_AREAID', data.areaId)
        commit('SET_STREETID', data.streetId)
        commit('SET_ADDRESS', data.address)
        commit('SET_SQCONFIG', data.sysQueryConfiguration)
        commit('SET_SQCONFIGSTATUS', data.sysQueryConfigurationStatus)
        commit('SET_WHITEVISITORTYPE', data.whiteVisitorType || [])
        commit('SET_ISCONTROLEARLYWARNING', data.isControlEarlyWarning)
        commit('SET_ADMINID', data.adminId)
        commit('SET_PLACETYPE', data.placeType)
        commit('SET_PLACECODESTATUS', data.placeCodeStatus)
        commit('SET_ISOPENPRIVATEAREA', data.isOpenPrivateArea)
        commit('SET_CODETYPE', data.codeType)
        commit('SET_THREECODELIST', data.threeCodeList)
        commit('SET_CARDPHYSICS', data.cardPhysics)
        commit('SET_DISINFECTFIAG', data.disinfectFlag)
        commit('SET_PROBLEMREPORTFLAG', data.problemReportFlag)
        commit('SET_ISOPENEXAM', data.isOpenExam)
        commit('SET_OPENINOUTSTATISTICS', data.openInOutStatistics)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { username, name, avatar, type, phone, provinceId, cityId, areaId, streetId, isOut, sysQueryConfiguration, sysQueryConfigurationStatus, whiteVisitorType, tagName, isControlEarlyWarning, adminId } = data
        commit('SET_ISCONTROLEARLYWARNING', isControlEarlyWarning)
        commit('SET_ADMINID', adminId)
        commit('SET_TAGNAME', tagName)
        commit('SET_USERNAME', username)
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_ROLE', type)
        commit('SET_PHONE', phone)
        commit('SET_ISOUT', isOut)
        commit('SET_PROVINCEID', provinceId)
        commit('SET_CITYID', cityId)
        commit('SET_AREAID', areaId)
        commit('SET_STREETID', streetId)
        commit('SET_ADDRESS', data.address)
        commit('SET_SQCONFIG', sysQueryConfiguration)
        commit('SET_SQCONFIGSTATUS', sysQueryConfigurationStatus)
        commit('SET_WHITEVISITORTYPE', whiteVisitorType || [])
        commit('SET_PLACETYPE', data.placeType)
        commit('SET_PLACECODESTATUS', data.placeCodeStatus)
        commit('SET_ISOPENPRIVATEAREA', data.isOpenPrivateArea)
        commit('SET_CODETYPE', data.codeType)
        commit('SET_THREECODELIST', data.threeCodeList)
        commit('SET_CARDPHYSICS', data.cardPhysics)
        commit('SET_DISINFECTFIAG', data.disinfectFlag)
        commit('SET_PROBLEMREPORTFLAG', data.problemReportFlag)
        commit('SET_ISOPENEXAM', data.isOpenExam)
        commit('SET_OPENINOUTSTATISTICS', data.openInOutStatistics)
        commit('SET_BUILDINGCHECKTYPE', data.buildingCheckType)
        commit('SET_ENTERPRISENAME', data.enterpriseName)
        commit('SET_TENANTENTERPRISENAME', data.tenantEnterpriseName)
        commit('SET_INOUTWARNING', data.isOpenInoutWarning)
        commit('SET_ISOPENLADDER', data.isOpenLadder)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getWebSocket({ commit, state }, WebSocketData) {
    return new Promise((resolve, reject) => {
      commit('SET_WEBSOCKETDATA', WebSocketData)
      resolve(state)
    })
  },
  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      // 标记已经退出,不用再检测长时间未操作情况
      isLogOut()

      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }

  // get user info
  // getPicture({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getPicture().then(response => {
  //       const { data } = response
  //       if (!data) {
  //         return reject('The request failed, please try again.')
  //       }
  //       console.log(data);
  //       console.log(data.title);
  //       const { title,logo,favicon,bg} = data
  //       commit('SET_TITLE', title)
  //       commit('SET_LOGO', logo)
  //       commit('SET_FAVICON', favicon)
  //       commit('SET_BG', bg)
  //       resolve(data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

