import Vue from 'vue'
import nedb from '../../db/nedb'
import base64 from '@/utils/base64'

// state
const state = {
  luckMoneyMap: {} // 红包账单
}

// getters
const getters = {
  luckMoneyMap: (state) => state.luckMoneyMap
}

// actions
const actions = {
  // ===================== 会话页面用到的变量 ===================
  // 添加新的标签
  AddLabel({ dispatch }, message) {
    let label = message.Label
    let wechatId = message.WeChatId
    let newLable = {
      LabelId: label.LabelId,
      LabelName: label.LabelName
    }
    nedb.labels.findOne({ WeChatId: wechatId }, (err, doc) => {
      if (err) return
      let newDoc = {
        Labels: [newLable],
        WeChatId: wechatId
      }
      if (doc) {
        let labels = doc.Labels
        let flag = true
        for (const label of labels) {
          if (label.LabelId === newLable.LabelId) {
            label.LabelName = newLable.LabelName
            flag = false
            break
          }
        }
        if (flag) {
          labels.push(newLable)
        }
        newDoc.Labels = labels
      }
      dispatch('UpdateOrAddLabels', newDoc)
    })
  },
  // 删除标签
  DeleteLabel({ dispatch }, message) {
    let wechatId = message.WeChatId
    nedb.labels.findOne({ WeChatId: wechatId }, (err, doc) => {
      if (err) return
      if (doc) {
        let newLables = []
        for (const label of doc.Labels) {
          if (label.LabelId !== message.LabelId) {
            newLables.push(label)
          }
        }
        let newDoc = {
          Labels: newLables,
          WeChatId: wechatId
        }
        dispatch('UpdateOrAddLabels', newDoc)
      }
    })
  },
  // 从nedb获取标签数据
  SetLabels({ commit }, wechatId) {
    nedb.labels.findOne({ WeChatId: wechatId }, (err, doc) => {
      if (err) return
      if (doc) {
        commit('SET_LABELS', doc)
      }
    })
  },
  // 更新或插入nedb的labels
  UpdateOrAddLabels({ rootGetters, dispatch }, message) {
    let wechatId = message.WeChatId
    nedb.labels.update({ WeChatId: message.WeChatId }, message, { upsert: true }, (err, numReplaced) => {
      if (err) return
      dispatch('SetLabels', wechatId)
    })
  },

  //设置红包账单
  SetLuckMoneyMap({ rootGetters, commit }) {
    nedb.luckMoney.find({ userId: rootGetters.userId }, (err, docs) => {
      if (err) return
      commit('SET_LUCK_MONEY_MAP', docs)
    })
  },
  // 添加红包账单
  InsertLuckMoney({ rootGetters, dispatch }, doc) {
    doc.userId = rootGetters.userId
    doc.opened = false
    nedb.luckMoney.insert(doc, (err, newDoc) => {
      if (err) return
      dispatch('SetLuckMoneyMap')
    })
  },
  // 更新红包账单
  UpdateLuckMoney({ rootGetters, dispatch }, message) {
    nedb.luckMoney.update(
      {
        userId: rootGetters.userId,
        MsgKey: message.MsgKey,
        WeChatId: message.WeChatId
      },
      { $set: { opened: true, Amount: message.Amount } },
      {},
      (err, numReplaced) => {
        if (err) return
        dispatch('SetLuckMoneyMap')
      }
    )
  }
}

// mutations
const mutations = {
  // 会话页面所选微信的标签列表
  SET_LABELS(state, doc) {
    state.labels = doc.Labels
  },
  // 设置luckMoneyMap
  SET_LUCK_MONEY_MAP(state, docs) {
    for (const key in docs) {
      Vue.set(state.luckMoneyMap, docs[key].MsgSvrId, docs[key])
    }
  }
}

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