import Vue from 'vue'
import Vuex from 'vuex'
import eventBus from '../eventBus'
import axios from 'axios'
import { stat } from 'fs';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        tableHeight: 0,
        fetchingData: false,
        userInfo: null,
        locationInfo: null,
        serviceType: [],
        hrefHistory: [],
        typeFlag: ['修图', '实体商品'], //产品分类 0.修图; 1.实体商品
        orderStatus: {
            '修图': ['待付款', '待上传', '待修图', '待审片', '返修中', '待评论', '订单完成', '退款中', '取消'],
            '实体商品': ['待付款', '待发货', '已发货', '', '', '待评论', '订单完成', '退款中', '取消']
        }, //订单状态
        IMConnected: false,
        conversationList: [],
        myStoreInfo: {},
        storeConversationList: [],
        brefName: '',
        msgCode: {
            '身份验证': 'SMS_144435052',
            '登录确认': 'SMS_144435051',
            '登录异常': 'SMS_144435050',
            '用户注册': 'SMS_144435049',
            '修改密码': 'SMS_144435049',
            '信息变更': 'SMS_144435049',
        }
    },
    mutations: {
        updateBrefName(state, brefName) {
            state.brefName = brefName
        },
        updateTableHeight(state, height) {
            state.tableHeight = height;
        },
        updateFetchingData(state, fetchingData) {
            state.fetchingData = fetchingData
        },
        updateUserInfo(state, userInfo) {
            state.userInfo = userInfo
        },
        updateStoreInfo(state, myStoreInfo) {
            state.myStoreInfo = myStoreInfo
        },
        updateLocationInfo(state, location) {
            state.locationInfo = location
        },
        updateServiceType(state, serviceType) {
            state.serviceType = serviceType
        },
        updateHrefHistory(state, list) {
            state.hrefHistory = list
        },
        updateIMConnected(state, connected) {
            state.IMConnected = connected
        },
        addConversation(state, conversation) {
            state.conversationList.unshift(conversation)
        },
        updateOrAddConversation(state, conversation) {
            let index = _.findIndex(state.conversationList, chr => {
                return Number(chr.id) === Number(conversation.id)
            })
            if (index !== -1) {
                Vue.set(state.conversationList, index, conversation)
            } else {
                state.conversationList.unshift(conversation)
            }
        },
        removeConversation(state, conversation) {
            console.log(conversation)
            let index = _.findIndex(state.conversationList, chr => {
                return Number(chr.id) === Number(conversation.id)
            })
            if (index !== -1) {
                state.conversationList.splice(index, 1)
            }
        },
        updateStoreConversationList(state, conversationList) {
            state.storeConversationList = conversationList
        },
        updateConversationList(state, conversationList) {
            // state.conversationList = conversationList.map(item => {
            //     return {
            //         id: item.id,
            //         time: item.chattime,
            //         unread: item.amount,
            //         content: item.chatInfo ? item.chatInfo.content : '',
            //         name: item.nickName || '无名氏',
            //         avatar: item.icon || require('../assets/default-avatar.png'),
            //         toUserId: item.toUserId,
            //         projectId: item.projectId,
            //         projectInfo: item.projectInfo
            //     }
            // })
            state.conversationList = conversationList
        }
    },
    actions: {
        receiveMessage({ commit, state }, message) {
            eventBus.$emit('receiveMessage', message)
            let projectId = message.projectId
            let index = _.findIndex(state.conversationList, i => {
                if (Number(message.userId) === 1) {
                    return Number(i.toUserId) === Number(message.userId)
                } else {
                    return Number(i.toUserId) === Number(message.userId) && Number(i.projectId) === Number(projectId)
                }
            })
            let conversation
            if (index !== -1) {
                conversation = _.clone(state.conversationList[index])
                conversation.unread += 1
                conversation.time = message.receivedTime
                conversation.content = message.content
                commit('updateOrAddConversation', conversation)
            } else {
                // let newCon = {
                //     userId: state.userInfo.id,
                //     toUserId: message.userId
                // }

                // if (projectId) {
                //     newCon.projectId = projectId
                // }

                // axios.post('/usersSession/save', newCon)
                //     .then(res => {
                //         if (res.success) {
                //             commit('updateConversationList', res.data)
                //         }
                //     })
                //     .catch(e => {
                //         console.log(e);
                //     })


            }
        }
    }
});