import http from '../http-request'

// 资源实体在URL中的占位符
const USER_PATH = ':user_id'
const SESSION_PATH = ':session_id'
const TOPIC_PATH = ':topic_id'
const PARTICIPANT_PATH = ':participant_id'

// REST API
var ENDPOINTS = {
    Application: {
        BadgeNo: '/application/badge_no'
    },
    Management: {
        Health: '/management/health',
        DbStatus: '/management/db'
    },
    Users: {
        Login: '/users/login',
        Logout: '/users/logout',

        User: '/users/:user_id',
        UserStatus: '/users/:user_id/status'
    },
    Sessions: {
        Sessions: '/sessions',
        getSessionsInfo: '/sessions/:session_id/session',

        Session: '/sessions/:session_id',
        SessionSticky: '/sessions/:session_id/sticky',

        RecentSessions: '/sessions/recent',

        Topics: '/sessions/:session_id/topics',
        Topic: '/sessions/:session_id/topics/:topic_id',
        TopicEnded: '/sessions/:session_id/topics/:topic_id/ended',

        Messages: '/sessions/:session_id/messages',
        MessagesByTopic: '/sessions/:session_id/topics/:topic_id/messages',
        Message: '/sessions/:session_id/messages/:message_id',

        SessionsUnreadMessageCount: '/sessions/unread_message_count',
        SessionUnreadMessageCount: '/sessions/:session_id/unread_message_count',
        SessionUnreadMessages: '/sessions/:session_id/messages/unread',

        Participants: '/sessions/:session_id/participants',
        ParticipantsAvatar: '/sessions/:session_id/participants/avatars',
        Participant: '/sessions/:session_id/participants/:participant_id',
        ParticipantAvatar: '/session/:session_id/participants/:participant_id/avatars'
    },
    Search: {
        Search: '/search'
    },
    Consult: {
        getHealthConsult: '/sessions/topics',
        updateConsultStatus: '/sessions/:session_id/topics'
    }
}

export default {
    Application: {
        // 获取应用角标数
        getBadgeNo(userId) {
            return http.imget(ENDPOINTS.Application.BadgeNo, { user_id: userId })
        }
    },
    Management: {
        getDbStatus() {
            return http.imget(ENDPOINTS.Management.DbStatus)
        }
    },
    Users: {
        // 登录
        login(userId, token, clientId, platform) {
            return http.impost(ENDPOINTS.Users.Login,
                { user_id: userId, token: token, client_id: clientId, platform: platform })
        },

        // 退出
        logout(userId) {
            return http.imdelete(ENDPOINTS.Users.Logout,
                { user_id: userId })
        },

        // 更新用户状态
        updateStatus(userId, appInBg) {
            return http.imput(ENDPOINTS.Users.UserStatus.replace(USER_PATH, userId),
                { app_in_bg: appInBg })
        }
    },
    Sessions: {
        // 创建MUC会话
        createMucSession(userId, peerId, sessionName) {
            var p = {}
            p[userId] = 0
            p[peerId] = 0
            return http.impost(ENDPOINTS.Sessions.Sessions,
                {
                    session_id: userId,
                    session_type: 1,
                    session_name: sessionName,
                    participants: JSON.stringify(p)
                })
        },

        // 创建P2P会话
        createP2pSession(userId, peerId, sessionName) {
            var p = {}
            p[userId] = 0
            p[peerId] = 0
            return http.impost(ENDPOINTS.Sessions.Sessions,
                { session_type: 2, session_name: sessionName, participants: JSON.stringify(p) })
        },

        createGroupSession(sessionId, sessionType, sessionName, participants, videoconferencing) {
            //sessionType: 3-团队群聊，4-临时群聊
            var p = {}
            for (var k in participants) {
                p[k] = 0
            }

            console.log("create group session");
            return http.impost(ENDPOINTS.Sessions.Sessions,
                { session_id: sessionId, session_type: sessionType, session_name: sessionName, participants: JSON.stringify(p), videoconferencing: videoconferencing? 1 : 0 })
        },

        /**
         * @api sessinos缓存读取
         * @param {string} userId 登录者的uid
         * @param {string} page 分页
         * @param {string} size 大小
         * @param {string} status isPatient判断是否为咨询病人1是0不是
         * @apiVersion 0.0.1
         * @desc 获取与患者发生的会话，实际上这些是MUC会话
         **/
        getSessionsWithPatient(userId, page, size, status) {
            return http.imget(ENDPOINTS.Sessions.Sessions,
                { user_id: userId, business_type: '2', status: status, page: page, size: size })
        },

        // 获取与医生相关的会话，实际上这些是P2P，群聊和讨论组
        getSessionsWithDoctor(userId, page, size) {
            return http.imget(ENDPOINTS.Sessions.Sessions,
                { user_id: userId, business_type: '1', page: page, size: size })
        },
        // 获取最近7天内的会话列表
        getRecentSessions(userId) {
            return http.imget(ENDPOINTS.Sessions.RecentSessions,
                { user_id: userId, date_span: 7 })
        },
        // 获取与患者的最近会话
        getRecentSessionsWithPatient(userId) {
            return http.imget(ENDPOINTS.Sessions.RecentSessions,
                { session_type: 1, user_id: userId })
        },

        // 获取与医生的最近会话，包括P2P、群聊与讨论组
        getRecentSessionsWithDoctor(userId) {
            return http.imget(ENDPOINTS.Sessions.RecentSessions,
                { session_type: '2, 3', userId: userId })
        },

        // 置顶会话
        stickSession(userId, sessionId) {
            return http.imput(ENDPOINTS.Sessions.SessionSticky.replace(SESSION_PATH, sessionId),
                { user_id: userId })
        },

        // 取消会话置顶
        unstickSession(userId, sessionId) {
            return http.imput(ENDPOINTS.Sessions.SessionSticky.replace(SESSION_PATH, sessionId),
                { user_id: userId })
        },

        // 获取所有咨询
        getTopics(sessionId) {
            return http.imget(ENDPOINTS.Sessions.Topics.replace(SESSION_PATH, sessionId))
        },

        // 创建咨询
        createTopic(sessionId, userId) {
            return http.impost(ENDPOINTS.Sessions.Topics.replace(SESSION_PATH, sessionId),
                { user_id: userId })
        },

        // 结束咨询
        endTopic(sessionId, userId) {
            return http.imput(ENDPOINTS.Sessions.Topics.replace(SESSION_PATH, sessionId),
                { user_id: userId })
        },

        // 咨询是否已结束
        isTopicEnded(sessionId, topicId) {
            return http.imget(ENDPOINTS.Sessions.TopicEnded.replace(SESSION_PATH, sessionId).replace(TOPIC_PATH, topicId),
                { session_id: sessionId, topic_id: topicId })
        },

        // 发送消息，不论是何种会话，均不需要指定会话对象是谁，只需要向会话本身发送消息即可
        sendMessage(sessionId, sendorCode, sendorName, content, contentType) {
            // var userAgent = plus.storage.getItem("userAgent");
            var view = 0 // 记录观察者模式
            // if(JSON.parse(userAgent).observer == "1"){
            //     view = 1;
            // }
            return http.impost(ENDPOINTS.Sessions.Messages.replace(SESSION_PATH, sessionId),
                { sender_id: sendorCode, sender_name: sendorName, content_type: contentType, content: content, view: view })
        },

        // 按会话获取消息
        getMessagesBySession(sessionId, userCode, page, pagesize, startMsgId, endMsgId, contentType, isoffset) {
            return http.imget(ENDPOINTS.Sessions.Messages.replace(SESSION_PATH, sessionId),
                {
                    page: page || 1,
                    pagesize: pagesize || 20,
                    end_msg_id: endMsgId || '',
                    start_msg_id: startMsgId || '',
                    user: userCode,
                    session_id: sessionId,
                    content_type: contentType || '',
                    isoffset: isoffset || ''
                })
        },

        // 按议题获取消息
        getMessagesByTopic(sessionId, topicId, startMessageId, count) {
            return http.imget(ENDPOINTS.Sessions.MessagesByTopic.replace(SESSION_PATH, sessionId),
                { start_message_id: startMessageId, count: count })
        },

        // 获取所有会话的未读消息数
        getAllSessionUnreadMessageCount(userId) {
            return http.imget(ENDPOINTS.Sessions.SessionsUnreadMessageCount,
                { user_id: userId })
        },

        // 获取指定会话的未读消息数
        getSessionUnreadMessageCount(sessionId, userId) {
            return http.imget(ENDPOINTS.Sessions.SessionUnreadMessageCount.replace(SESSION_PATH, sessionId),
                { user_id: userId })
        },

        // 获取指定会话的未读消息
        getSessionUnreadMessages(sessionId, userId) {
            return http.imget(ENDPOINTS.Sessions.SessionUnreadMessages.replace(SESSION_PATH, sessionId),
                { user_id: userId })
        },

        // 添加会话成员
        addParticipant(sessionId, participantId) {
            return http.imput(ENDPOINTS.Sessions.Participant.replace(SESSION_PATH, sessionId).replace(PARTICIPANT_PATH, participantId),
                { participant_id: participantId })
        },

        // 获取会话成员列表
        getParticipants(sessionId) {
            return http.imget(ENDPOINTS.Sessions.Participants.replace(SESSION_PATH, sessionId))
        },

        // 移除会话成员
        removeParticipant(sessionId, participantId) {
            return http.imdelete(ENDPOINTS.Sessions.Participant.replace(SESSION_PATH, sessionId).replace(PARTICIPANT_PATH, participantId))
        },

        // 获取会话成员头像
        getParticipantAvatar(sessionId, participantId) {
            return http.imget(ENDPOINTS.Sessions.ParticipantAvatar.replace(SESSION_PATH, sessionId).replace(PARTICIPANT_PATH, participantId))
        },

        // 获取会话成员头像
        getParticipantsAvatars(sessionId) {
            return http.imget(ENDPOINTS.Sessions.ParticipantsAvatar.replace(SESSION_PATH, sessionId))
        },

        // 根据session_id获取会话信息
        getSessionsInfo(sessionId, userId) {
            return http.imget(ENDPOINTS.Sessions.getSessionsInfo.replace(SESSION_PATH, sessionId),
                { user_id: userId })
        }
    },
    Search: {
        Doctor: {
            // 搜索所有对象：医生，会话及消息
            searchAll(userId, keyword, excludeTopicEndedSessions) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'doctor', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions })
            },

            // 搜索医生
            searchDoctors(userId, keyword, excludeTopicEndedSessions, page, size) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'doctor', entity_type: 'user', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size })
            },

            // 搜索会话
            searchSessions(userId, keyword, excludeTopicEndedSessions, page, size) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'doctor', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size })
            },

            // 搜索会话消息
            searchMessages(userId, keyword, page, size) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'doctor', entity_type: 'message', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size })
            }
        },

        Patient: {
            // 搜索所有对象：患者，会话及消息
            searchAll(userId, keyword, excludeTopicEndedSessions) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'patient', entity_type: 'all', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions })
            },

            // 搜索患者
            searchPatient(userId, keyword, excludeTopicEndedSessions, page, size) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'patient', entity_type: 'user', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size })
            },

            // 搜索会话
            searchSessions(userId, keyword, excludeTopicEndedSessions, page, size) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'patient', entity_type: 'session', keyword: keyword, exclude_topic_ended_sessions: excludeTopicEndedSessions, page: page, size: size })
            },

            // 搜索会话消息
            searchMessages(userId, keyword, page, size) {
                return http.imget(ENDPOINTS.Search.Search,
                    { user_id: userId, target_role: 'patient', entity_type: 'message', keyword: keyword, exclude_topic_ended_sessions: false, page: page, size: size })
            }
        }
    },

    Consult: {
        // 获取健康咨询列表
        getHealthConsult(user, status, page, pagesize) {
            return http.imget(ENDPOINTS.Consult.getHealthConsult,
                { user: user, status: status, page: page, pagesize: pagesize })
        },
        // 更新咨询状态
        updateConsultStatus(sessionId, id, status) {
            return http.imput(ENDPOINTS.Consult.updateConsultStatus.replace(SESSION_PATH, sessionId),
                { topic_id: id, data: JSON.stringify({ status: status }) })
        }
    }
}
