import im from "@/utils/im/index.js"

export default {
    state: {
        contacts: {},
        myInfo: {}
    },
    getters: {
        contacts(state) {
            return state.contacts
        },
        contactInfo: (state) => (targetId) => {
            if (state.contacts[targetId]) {
                const info = state.contacts[targetId]
                
                return {
                    name: info.name,
                    hash: info.hash,
                    portraitUrl: info.localAvatar ? info.localAvatar : require('@/static/user/cover.png')
                }
            } else {
                return {
                    name: '',
                    hash: '',
                    portraitUrl: require('@/static/user/cover.png')
                }
            }
        },
        // 联系人是否存在
        contactIsExist: (state) => (targetId) => {
            return Boolean(state.contacts[targetId])
        },
        sender(state) {
            return state.myInfo
        }
    },
    mutations: {
        updateContactInfo(state, contactInfo) {
            Vue.set(state.contacts, contactInfo.targetId, contactInfo)
        },
        setSenderInfo(state, contactInfo) {
            state.myInfo = {
                userId: contactInfo.targetId,
                name: contactInfo.name,
                portraitUrl: contactInfo.portraitUrl
            }
        }
    },
    actions: {
        setSenderInfo({
            commit
        }, contactInfo) {
            commit('setSenderInfo', contactInfo)
        },
        // 载入好友信息
        launchContact({
            commit
        }, data) {
            commit('updateContactInfo', data)
        },
        // 更新好友信息，这个时候要校验hash值了
        updateContact({
            commit,
            dispatch
        }, contactInfo) {
            const model = uni.model.contactModel
            model.find('targetId="' + contactInfo.targetId + '"', (err, result) => {
                if (result.length == 0) {
                    // 没有数据，直接新增一条
                    dispatch('initContact', contactInfo)
                } else if (contactInfo.hash != result[0].hash) {
                    if (contactInfo.portraitUrl && contactInfo.portraitUrl != result[0].portraitUrl) {
                        saveAvatar(contactInfo, (savedFilePath) => {
                            const info = {
                                targetId: contactInfo.targetId,
                                name: contactInfo.name,
                                hash: contactInfo.hash,
                                portraitUrl: contactInfo.portraitUrl,
                                localAvatar: savedFilePath
                            }
                            model.update('targetId="' + contactInfo.targetId + '"', info, (err,
                                res) => {
                                console.log('UPDATE AVATAR, ERR', err, info);
                            })
                            commit('updateContactInfo', info)
                        })
                    } else {
                        const info = {
                            targetId: contactInfo.targetId,
                            name: contactInfo.name,
                            hash: contactInfo.hash,
                            portraitUrl: contactInfo.portraitUrl,
                            localAvatar: result[0].localAvatar
                        }
                        model.update('targetId="' + contactInfo.targetId + '"', info, (err, res) => {
                            console.log('UPDATE NAME, ERR', err, info);
                        })
                        commit('updateContactInfo', info)
                    }
                } else {
                    console.log('updateContact, 无操作');
                }
            })
        },
        // 初始化好友信息
        initContact({
            commit
        }, contactInfo) {
            // 将好友信息保存到vuex的内存中，方便立即使用
            commit('updateContactInfo', contactInfo)
            const model = uni.model.contactModel
            // 用户头像，是否需要下载到本地
            if (contactInfo.portraitUrl) {
                saveAvatar(contactInfo, (savedFilePath) => {
                    const info = {
                        targetId: contactInfo.targetId,
                        name: contactInfo.name,
                        hash: contactInfo.hash,
                        portraitUrl: contactInfo.portraitUrl,
                        localAvatar: savedFilePath
                    }
                    model.insert(info, (err, res) => {})
                    // 保存头像后，更新信息
                    commit('updateContactInfo', info)
                })
            } else {
                // 直接将信息，写入数据库
                const info = {
                    targetId: contactInfo.targetId,
                    name: contactInfo.name,
                    hash: contactInfo.hash,
                    portraitUrl: contactInfo.portraitUrl,
                    localAvatar: ''
                }
                model.insert(info, (err, res) => {})
            }
        }
    }
}

const saveAvatar = (contactInfo, callback) => {
    uni.downloadFile({
        url: contactInfo.portraitUrl,
        success: ({
            tempFilePath
        }) => {
            uni.saveFile({
                tempFilePath: tempFilePath,
                success: ({
                    savedFilePath
                }) => {
                    callback(savedFilePath)
                }
            })
        },
        fail: (err) => {}
    })
}
