import Vue from 'vue'
import nedb from '../../db/nedb'

// state
const state = {
  addFriendsHistory: [], // 添加好友的列表
  newFriends: {
    WxId: '',
    Contacts: []
  }, // 新的朋友
  qrCode: {}, // 微信二维码
  wxFriends: {
    Count: 0,
    Friends: [],
    WxId: ''
  }, // 微信好友
  phoneState: {} // 手机状态
}

// getters
const getters = {
  addFriendsHistory: (state) => state.addFriendsHistory,
  newFriends: (state) => state.newFriends.Contacts,
  qrCode: (state) => state.qrCode,
  wxFriends: (state) => state.wxFriends,
  phoneState: (state) => state.phoneState
}

// actions
const actions = {
  // 更新或添加nedb中的 addCustomerFromSearchTask 数据
  UpdateAddCustomerFromSearchTask({ dispatch }, message) {
    let taskId = Number(message.TaskId)
    // 查数据库有没有记录
    nedb.addCustomerFromSearchTask.findOne({ TaskId: taskId }, (err, doc) => {
      if (err) return
      if (doc) {
        nedb.addCustomerFromSearchTask.update(
          { TaskId: taskId },
          { $set: { result: message.result } },
          {},
          (err, numReplaced) => {
            if (err) return
            dispatch('SetAddCustomerFromSearchTask', message.WxId)
          }
        )
      } else {
        nedb.addCustomerFromSearchTask.insert(message, (err, newDoc) => {
          if (err) return
          dispatch('SetAddCustomerFromSearchTask', message.WxId)
        })
      }
    })
  },
  // 从nedb获取 addCustomerFromSearchTask 数据
  SetAddCustomerFromSearchTask({ rootGetters, commit }, wxid) {
    nedb.addCustomerFromSearchTask
      .find({ userId: rootGetters.userId, WxId: wxid }) // 全部
      .sort({ TaskId: -1 }) // 降序
      // .limit(20) // 20条
      .exec((err, docs) => {
        if (!err && docs) {
          commit('SET_ADD_CUSTOMER_FROM_SEARCH_TASK', docs)
        }
      })
  }
}

// mutations
const mutations = {
  // 设置加好友历史记录
  SET_ADD_CUSTOMER_FROM_SEARCH_TASK(state, docs) {
    state.addFriendsHistory = docs
  },
  // 设置工具大全的微信二维码
  UPDATE_QR_CODE(state, doc) {
    if (doc.Success) {
      Vue.set(state.qrCode, doc.WxId, doc.Url)
    }
  },
  // 设置微信好友
  SET_WX_FRIENDS(state, friends) {
    console.log('SET_WX_FRIENDS', friends)
    state.wxFriends = friends
  },
  // 重置微信好友
  RESET_WX_FRIENDS(state, friends) {
    state.wxFriends = { Count: 0, Friends: [], WxId: '' }
  },
  // 重置新的朋友
  RESET_NEW_FRIENDS(state) {
    Vue.set(state.newFriends, 'WxId', '')
    Vue.set(state.newFriends, 'Contacts', [])
  },
  // 设置新的朋友
  SET_NEW_FRIENDS(state, data) {
    console.log('SET_NEW_FRIENDS', data)
    Vue.set(state.newFriends, 'WxId', data.WxId)
    Vue.set(state.newFriends, 'Contacts', data.Contacts || [])
  },
  // 更细本地newFriends数据
  UPDATE_NEW_FRIENDS(state, taskResult) {
    console.log('UPDATE_NEW_FRIENDS', taskResult)
    if (state.newFriends.WxId === taskResult.WxId && taskResult.Success) {
      Vue.set(
        state.newFriends,
        'Contacts',
        state.newFriends.Contacts.filter((x) => x.RemoteId !== taskResult.TaskId)
      )
    }
  },
  // 设置手机状态
  SET_PHONE_STATE: (state, data) => {
    Vue.set(state.phoneState, data.WxId, data)
  }
}

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