import socket from "$utils/socket/index";
import {FlatListReload} from "$components/flat-list/const";

export default <LibMixins>{

    inject:['openUserContent'],

    computed: {
        useConversation() {

            if (this.user.uid) {

                let item = this.chatUserObject.indexOf(this.user.uid);

                // 如果存在
                if (item>=0) {
                    return this.chatUserList;
                } else {
                    return  [this.user,...this.chatUserList];
                }

            } else {
                return this.chatUserList;
            }


        }
    },

    methods:{
        fetch(obj){

            this.$store.dispatch('getSessionListPromise').then(({data})=>{

                if (this.user.uid) {
                    this.triggerOpenUserContent(this.user);
                } else if (data && data.length > 0){
                    this.triggerOpenUserContent(data[0]);
                }

            });
        },
        triggerOpenUserContent(item){

            if (item.uid !== this.uid) {
                // 获取
                let conversation = socket.getConversation(item.uid);
                // 清楚标记
                conversation && conversation.read();

                return this.openUserContent(item.uid,item.pid || 0,item);
            }

        },

        deleteConversation(item){

            let userIndex = this.chatUserObject.indexOf(item.uid);
            if (userIndex >= 0) {
                // 获取
                let conversation = socket.getConversation(item.uid);
                // 清楚历史消息
                conversation && conversation.clearMessages({
                    timestamp: +new Date()
                })
                // 删除制定会话
                conversation && conversation.destory().then(()=>{
                    // 执行删除会话
                    this.$store.commit('spliceSession',item.uid);
                });
            }

            if (this.user.uid === item.uid){
                this.user.uid = '';
            }



        }
    },

    created(){
        this.fetch();
    }

}
