import {imService} from '@/utils/request'
import io from 'socket.io-client/dist/socket.io';
var baseURL = process.env.VUE_APP_SOCKET_URL
export default{
	sessions(params) {
		return imService({
			url: '/sessions/sessionListByType',
			method: 'get',
			params
		})
	},
	p2pSessions(params) {
		return imService({
			url: '/sessions',
			method: 'get',
			params
		})
	},
	createP2pSession(data) {
		var p = {};
		p[data.userId] = 0;
		p[data.peerId] = 0;
		data.participants= data.participants || JSON.stringify(p)
		return imService({
			url: '/sessions',
			method: 'post',
			data
		})
	},
	getMessages(sessionId, params) {
		return imService({
			url: `/sessions/${sessionId}/messages`,
			method: 'get',
			params
		})
	},
	sendMsg(sessionId, data) {
		return imService({
			url: `/sessions/${sessionId}/messages`,
			method: 'post',
			data,
			isModify: true
		})
	},
	getParticipants(sessionId, params) {
		return imService({
		    url: `/sessions/${sessionId}/participants`,
		    method: 'get',
		    params
		})
	}, 
	isTopicEnded(sessionId, topicId, params) {
		return imService({
		    url: `/sessions/${sessionId}/topics/${topicId}/ended`,
		    method: 'get',
		    params
		})
	}, 
	/**
	 * 按会话类型获取未读消息数
	 * @param {user_id: "", type: ""} params 
	 */
	unreadMessageCount(params) {
		return imService({
		    url: `/sessions/unread_message_count`,
		    method: 'get',
		    params
		})
    },
	loginIM(sessionId, userId) {
		console.log(io)
	    let socket = io.connect(baseURL)
      socket.emit('login', { userId: userId, password: userId, sessionId: sessionId, clientType: 'patient' })
	    return socket
	},
	loginSystemIM(userId, type) {
    let socket = io.connect(baseURL)
    //let socket = io.connect('http://192.168.131.140:3000')
		let userType = ""
		if(type == 1) {
			userType = "olderWx" // 老人公众号
		} else if(type == 2) {
			userType = "child" // 幼儿
		}
      socket.emit('login', { userId: userId, password: userId, sessionId: "system", clientType: 'patient_system', userType: userType })
	    return socket
	},
} 