// import Vue from 'vue'
import { noticeUserList, hasRead, getUnreadCount } from '@/api/noticeUser'
export default {
  state: {
    websock: null,
    wsMessageData: null,
    wsMessageHomeData: null,
    unreadCount: 0,
    updateMessage: null, // 判断是否刷新未读消息数据
    heartInterId: null
  },
  getters: {
    wsMessageData: state => {
      return () => state.wsMessageData
    },
    wsMessageHomeData: state => {
      return () => state.wsMessageHomeData
    },
    unreadCount: state => state.unreadCount,
    updateMessage: state => {
      return () => state.updateMessage
    }
  },
  mutations: {
    WEBSOCKET_INIT (state, url) {
      const _this = this
      const baseUrl = process.env.VUE_APP_WX_BASE_URL
      const sid = 'pc/u' + this.getters.userInfo.userid + 'epc'
      const protocol = process.env.NODE_ENV == 'production' ? (window.location.protocol == 'http:' ? 'ws:' : 'wss:') : 'ws:'
      const wsUrl = protocol + baseUrl + sid
      // console.log(process.env.NODE_ENV, wsUrl)
      state.websock = new WebSocket(wsUrl)
      state.websock.onopen = function () {
        // console.log('连接成功！')
        _this.dispatch('getUnreadMessageCount')
      }
      state.websock.onmessage = function (e) {
        // console.log('ws接收！', e.data)
        if (e.data != 'pong') {
          state.wsMessageData = JSON.parse(e.data)
        }
      }
      state.websock.οnerrοr = function (e) { // 错误
        console.log('ws错误!', e)
      }
      state.websock.onclose = function (e) { // 关闭
        console.log('ws关闭！', e)
      }

      // 发送心跳包
      state.heartInterId = setInterval(function () {
        // console.log('ws发送心跳！')
        if (state.websock.readyState == 3) {
          clearInterval(state.heartInterId)
          _this.commit('WEBSOCKET_INIT', url)
        }
        state.websock.send('ping')
      }, 10000)
    },
    WEBSOCKET_CLOSE (state) {
      state.websock.close()
      clearInterval(state.heartInterId)
    },
    WEBSOCKET_SEND (state, p) {
      // console.log('ws发送！', p)
      state.websock.send(p)
    },
    UPDATE_HOME (state, p) {
      state.wsMessageHomeData = p
    },
    setMessageCount (state, count) {
      state.unreadCount = count
    }
  },
  actions: {
    WEBSOCKET_INIT ({ commit }, url) {
      commit('WEBSOCKET_INIT', url)
    },
    WEBSOCKET_SEND ({ commit }, p) {
      commit('WEBSOCKET_SEND', p)
    },
    UPDATE_HOME ({ commit }, p) {
      commit('UPDATE_HOME', p)
    },
    WEBSOCKET_CLOSE ({ commit }) {
      commit('WEBSOCKET_CLOSE')
    },
    // 此方法用来获取未读消息条数，接口只返回数值，不返回消息列表
    getUnreadMessageCount ({ state, commit }) {
      getUnreadCount().then(res => {
        // console.log(res, '---------------请求未读消息', res.data ? res.data.no_read_count : 0)
        commit('setMessageCount', res.data ? res.data.no_read_count : 0)
      })
    },
    // 获取消息列表，其中包含未读、已读、回收站三个列表
    getMessageList ({ state, commit }, params) {
      return new Promise((resolve, reject) => {
        noticeUserList(params).then(res => {
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 把一个未读消息标记为已读
    hasRead ({ state, commit }, { msg_id }) {
      return new Promise((resolve, reject) => {
        hasRead(msg_id).then((res) => {
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 更新未读数
    updateUnreadcount ({ state, commit }, count) {
      commit('setMessageCount', count)
    }
  }
}
