import Vue from 'vue'
import _ from 'lodash'
import Session from '@/store/db/Session.js'
import { initChatData } from '@/store/state'
import {
  decryptoMsg
} from '@/util/util.js'

const objSession = new Session()
const mutations = {
  setSessionList (state, data) {
    data.forEach(value => {
      let lastMsg = value['last_msg']
      value.cont = lastMsg ? decryptoMsg(lastMsg.content) : ''
      // console.log(value)
    })

    // 按is_pin(是否置顶) 属性排序
    state.sessionList = data
  },
  addSession (state, data) {
    state.sessionList.push(data)
  },
  chatAppLogin (state, flag) {
    state.isLogin = flag
  },
  toApp (state, flag) {
    state.toApp = flag
  },
  addSessionItem (state, data) {
    let pinSession = state.sessionList.filter(item => {
      return item.is_pin === 1
    })
    let dataPos = pinSession.length || 0
    state.sessionList.splice(dataPos, 0, data)

    objSession.recover(data.session_id)
  },
  // 会话列表未读消息设置
  setSessionItemUnread (state, data) {
    let itemIndex = 0
    state.sessionList.forEach((item, index) => {
      if (item.session_id == `${data.session_id}`) {
        let unread = item.unread
        if (data.unread && data.curSession != data.session_id) {
          unread += data.unread
        } else {
          unread = 0
        }

        Vue.set(item, 'unread', unread)
        objSession.setUnread(item.session_id, unread)
        itemIndex = index
      }
    })
    // 重新排序 非pin的会话
    let temp = state.sessionList[itemIndex]
    if (itemIndex && !temp.is_pin && data.cont) {
      state.sessionList.splice(itemIndex, 1)
      mutations.addSessionItem(state, temp)
    }
  },
  setFriendList (state, data) {
    state.friendList = data
  },
  // 左侧会话撤回-用于私聊主动撤回
  setSessionRepeal (state, data) {
    state.sessionList.forEach((item) => {
      if (item.session_id == (data.sessionId || data.group_id)) {
        Vue.set(item.last_msg, 'msg_type', -1)
        Vue.set(item, 'update_time_int', data.timestamp)
      }
    })
  },
  setSessionItem (state, data) {
    state.sessionList.forEach(item => {
      if (item.session_id == data.session_id) {
        item = data
      }
    })
  },
  /**
   * @des 根据置顶目标更新会话列表顺序
   * @param {Object} state
   * @param {String} sessionId  待置顶的会话
   */
  updateSessionListByPin (state, data) {
    objSession.setPin(data.session_id, 1)
    state.sessionList.forEach((item, index) => {
      if (item.session_id == data.session_id) {
        item.is_pin = data.is_pin
        item.pin_time_int = data.pin_time_int
        state.sessionList.unshift(state.sessionList.splice(index, 1)[0])
      }
    })
  },
  /**
   * @des 根据取消置顶目标更新会话列表顺序
   * @param {Object} state
   * @param {String} sessionId  待取消置顶的会话
   */
  cancelSessionListByPin (state, data) {
    objSession.setPin(data.session_id, 0)
    let targetItem = null
    state.sessionList.forEach((item, index) => {
      if (item.session_id == data.session_id) {
        item.is_pin = data.is_pin
        item.pin_time_int = data.pin_time_int
        targetItem = state.sessionList.splice(index, 1)[0]
      } else if (targetItem && item.is_pin < 1) {
        // 插入到合适的位置
        state.sessionList.splice(index, 0, targetItem)
        targetItem = null
      }
    })

    // state.sessionList.push(targetItem)
  },
  /**
   * @des 根据免打扰更新会话列表
   * @param {Object} state
   * @param {String} sessionId  免打扰的会话
   */
  updateSessionListByMute (state, sessionId) {
    objSession.setMute(sessionId, 1)
    state.sessionList.forEach((item, index) => {
      if (item.session_id == sessionId) {
        item.is_mute = 1
      }
    })
  },
  /**
   * @des 根据免打扰置顶目标更新会话列表
   * @param {Object} state
   * @param {String} sessionId  待取消免打扰的会话
   */
  cancelSessionListByMute (state, sessionId) {
    objSession.setMute(sessionId, 0)
    state.sessionList.forEach((item, index) => {
      if (item.session_id == sessionId) {
        item.is_mute = 0
      }
    })
  },
  /**
   * @des 根据群id删除会话列表item
   * @param {Object} state
   * @param {String} sessionId  待取消免打扰的会话
   */
  removeSessionListById (state, sessionId) {
    objSession.remove(sessionId)
    state.sessionList = _.filter(state.sessionList, (item, index) => {
      return item.session_id != sessionId
    })
  },
  /**
   * @des 更新用户群主列表中的last_msg内容
   * @param {String} params.sessionId 待更新的sessionId
   * @param {Object} params.data 待更新的内容
   */
  updateSessionLastmsg (state, params) {
    let targetObj = state.sessionList.find(n => {
      return n.session_id == (params.group_id || params.sessionId || params.session_id)
    })

    if (targetObj) {
      mutations.setSessionLastmsg(state, targetObj, params)
    }
  },
  updateSessionLastMsgNoDecode (state, params) {
    mutations.updateSessionLastmsg(state, Object.assign(params, { noDecryptoMsg: true }))
  },
  setSessionLastmsg (state, targetObj, params) {
    if (!targetObj.last_msg) {
      Vue.set(targetObj, 'last_msg', {})
    }

    let lastMsg = targetObj.last_msg
    Vue.set(lastMsg, 'content', params.content)
    Vue.set(lastMsg, 'from', params.from)
    Vue.set(lastMsg, 'msg_type', params.msg_type)
    Vue.set(lastMsg, 'name', params.name)
    Vue.set(lastMsg, 'nick_name', params.nick_name)
    Vue.set(lastMsg, 'time', params.timestamp)

    Vue.set(targetObj, 'update_time_int', params.timestamp)
    Vue.set(targetObj, 'cont', params.noDecryptoMsg ? params.content : decryptoMsg(params.content))

    if (params.ext_info && params.ext_info.event_type)Vue.set(lastMsg, 'event_type', params.ext_info.event_type)
  },
  initChatData (state) {
    let data = initChatData()

    for (let i in state) {
      if (data.hasOwnProperty(i)) state[i] = data[i]
    }
  }
}

export default mutations
