import {Module} from "vuex";
import socket from "$utils/socket";

let storageData: Array<Record<string, any>> = [];
let storageDataArray = [];

export default <Module<any,any>>{

    state:{
        chatUserList:[],
        chatUserObject:[],
        chatSessionStatus:false,
        chatCount:0
    },

    mutations:{

        // 获取会话列表
        setSessionList(state,data = []){
            state.chatSessionStatus = true;
            let newData = [];
            let chatUserObject = [];
            let chatCount = 0;
            // 处理 data
            // @ts-ignore
            data = data.map((item)=>{
                let resultItem:Record<string, any> = {
                    count: item.unreadMessageCount,
                    uid: 0
                }

                if (item.latestMessage && item.latestMessage.content) {
                    resultItem = {
                        ...resultItem,
                        message:item.latestMessage.content.message,
                        ...((item.latestMessage.content.userInfo || {})[item.targetId] || {})
                    }
                }

                // @ts-ignore
                if (resultItem.uid) {
                    chatCount +=resultItem.count;
                    chatUserObject[newData.push(resultItem) -1] = resultItem.uid;
                }
            });

            state.chatUserList = newData;
            state.chatCount=chatCount;
            state.chatUserObject = chatUserObject;
        },

        // 移除某一个
        spliceSession(state,uid){

            uid = parseInt(uid);
            let index = state.chatUserObject.indexOf(uid);
            if (index >=0){

                if (state.chatUserList[index].count > 0) {
                    let count = state.chatCount -  state.chatUserList[index].count;
                    count = count<0 ? 0 : count;
                    if (state.chatCount !== count) {
                        state.chatCount = count;
                    }
                }

                state.chatUserList.splice(index,1);
                state.chatUserObject.splice(index,1);
            }

            let targetIndex = storageDataArray.indexOf(uid);
            if (targetIndex>=0) {
                storageDataArray.splice(targetIndex,1);
                storageData.splice(targetIndex,1);
            }

        },

        clearSession(state){
            if (state.chatSessionStatus) {
                state.chatSessionStatus = false;
                state.chatUserList = [];
                state.chatUserObject = [];
                state.chatCount = 0;
            }
        }

    },

    actions:{

        // 会话发生变化触发
        mergeSessionList({commit,state},event){

            if (!state.chatSessionStatus) return;

            const updatedConversationList = event.updatedConversationList;
            let conversationList = socket.mergeConversation({
                conversationList: storageData,
                updatedConversationList
            });

            if (conversationList) {
                storageData = JSON.parse(JSON.stringify(conversationList));
                storageDataArray = storageData.map((item)=>{
                    return parseInt(item.targetId);
                });
                commit('setSessionList',conversationList);
            }

        },

        getSessionListPromise({state,commit}){

            return new Promise((resolve, reject)=>{

                if (state.chatSessionStatus) {
                    resolve({
                        data:state.chatUserList,
                        object:state.chatUserObject
                    });
                } else {
                    socket.getConversationList({
                        count:200,
                        // @ts-ignore
                        startTime:+new Date(),
                        order:0
                    }).then((data)=>{
                        storageData = JSON.parse(JSON.stringify(data));
                        storageDataArray = storageData.map((item)=>{
                            return parseInt(item.targetId);
                        });
                        commit('setSessionList',data||[]);
                        resolve({
                            data:state.chatUserList,
                            object:state.chatUserObject
                        });
                    }).catch(reject);
                }

            });

        }
    }

}
