"use strict";

var testConfig = require('../config');
var APIv1 = require('../../../src/doctor/include/endpoints').APIv1;

var should = require("should");
var restTemplate = require('supertest').agent(testConfig.host);

var userId = '0de7295862dd11e69faffa163e8aee56';            //
var targetUserId = '37959ddf86f211e6b394fa163e424525';      //

var adminTeamId = 5;                                        // 行政团队ID: 黄庄家庭医生团队
var consultGroupId = 'bcfd47c57ea74ba19fa049222c76befd';    // 讨论组ID

var at = '';

describe('API: Chat', function () {
    // 发送系统消息
    describe('when send SYSTEM message correctly', function () {
        it('should return 200', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.SM;
            restTemplate.post(path)
                .send({
                    to: userId,
                    title: "系统消息",
                    summary: "您有一条新的工作记录",
                    contentType: "1",
                    content: "患者已经签约，可以制定随访计划。"
                })
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log(response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        });
    });

    describe('when send SYSTEM message that user not exist', function () {
        it('should return 404', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.SM;
            restTemplate.post(path)
                .send({
                    to: "Rose Unknown",
                    title: "System Message",
                    summary: "You have new job",
                    contentType: "1",
                    content: "The patient has been followed in the scheduler, please make new follow plan as soon as possible."
                })
                .end(function (err, response) {
                    response.status.should.equal(404);

                    done();
                });
        });
    });

    // 发送私信
    describe('when send PRIVATE message correctly', function () {
        it('should return 200', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.PM;
            restTemplate.post(path)
                .send({
                    from: userId,
                    to: targetUserId,
                    contentType: 1,
                    content: "此患者白细胞如此高，中性粒值也高，同时感染细菌与病毒吗？"
                })
                .end(function (err, response) {
                    response.status.should.equal(200);

                    done();
                });
        });
    });

    // 向行政团队发送群消息
    describe('when send GROUP of admin team message correctly', function () {
        it('should return 200', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.GM;
            restTemplate.post(path)
                .send({
                    from: userId,
                    at: '',
                    group: adminTeamId,
                    groupType: 1,
                    contentType: 1,
                    content: "请问：居民签约人数是否达标"
                })
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log(response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        });
    });

    // 向讨论组发送群消息
    describe('when send GROUP of consult team message correctly', function () {
        it('should return 200', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.GM;
            restTemplate.post(path)
                .send({
                    from: '0de705f962dd11e69faffa163e8aee56',
                    at: '794f637bd3c64b948b112f16911a7883',
                    group: consultGroupId,
                    groupType: 2,
                    contentType: 1,
                    content: "庄医生，患者体征数据是否正常？"
                })
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log(response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        });
    });

    // 所有聊天列表
    describe('when get ALL CHAT list', function () {
        it('should return 200 and all chat list', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.List + "?user_id=" + userId;
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User " + userId + "'s all chat list: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        });
    });

    // 最近联系对象
    describe('when get recent chat list, including ', function () {
        it('should return 200 and recent chat list in date span', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.Recent + "?user_id=" + userId + "&days=7";
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User " + userId + "'s recent chats: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                })
        });
    });

    // 与居民的聊天列表，包括含有居民的群
    describe('when get patient contained chat list', function () {
        it('should return 200 and chat list with patients', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.ListWithPatient + "?user_id=" + 'D2016082203';
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User " + userId + "'s chats with patients: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                })
        });
    });

    // 与医生的聊天列表，不包含有居民的群
    describe('when get doctor contained only chat list', function () {
        it('should return 200 and chat list with doctors', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.ListWithDoctor + "?user_id=0de7421c62dd11e69faffa163e8aee56";
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User " + userId + "'s chats with doctors: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                })
        });
    });

    // 所有未读组消息数
    describe('when get ALL GROUP unread chat count', function () {
        it('should return 200 and group chat count', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.GMUnreadCount + "?user_id=" + userId;
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200) {
                        console.log("User " + userId + "'s group chat count: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        })
    });

    // 所有未读私信数
    describe('when get ALL PRIVATE unread chat list', function () {
        it('should return 200 and private chat count', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.PMUnreadCount + "?user_id=" + userId;
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User " + userId + "'s private chat count: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        })
    });

    // 所有未读消息数
    describe('when get ALL unread message count', function () {
        it('should return 200 and all unread message count', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.UnreadMsgCount + "?user_id=" + userId;
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User " + userId + "'s all unread message count: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        });
    });

    // 获取未读私信
    describe('when get unread private messages', function () {
        it('should return 200 and unread private messages', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.PMUnread + "?user_id=D2016008240002&peer_id=D2016008240003";
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User D2016008240002's unread private message with D2016008240003: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        })
    });

    // 获取未群消息
    describe('when get unread group messages', function () {
        it('should return 200 and unread group messages', function (done) {
            var path = APIv1.Chats.Base + APIv1.Chats.GMUnread + "?group_id=2&user_id=D20160322000003";
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User D20160322000003's unread messages in group 2: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        })
    });

    // 获取角标数
    describe('when get badge number', function () {
        it('shout return 200 and badge number', function (done) {
            var path = APIv1.Application.Base + APIv1.Application.BadgeNo + "?user_id=" + userId;
            restTemplate.get(path)
                .end(function (err, response) {
                    if(response.status === 200){
                        console.log("User " + userId + "'s badge number: \n", response.body);
                    }

                    response.status.should.equal(200);

                    done();
                });
        });
    });
});