Namespace.register("U.F.FM"); //好友聊天和群聊天消息命名空间

//#region 好友和群聊天记录

//#region 聊天记录


/**
* 好友或者群消息
* @param  {element} 聊天框
* @param  {object} 好友或者群信息
* @param  {int} 第几页
* @param  {string} 变化的样式
*/
U.F.FM.ViewFriendsChatRecords = function (el, friendandgroupinfo, page, isgroup) {
    //如果聊天记录在显示,那么隐藏聊天记录
    if (el.style.display == "block") {
        el.style.display = "none";
    }
    //如果聊天记录没有打开,那么打开聊天记录
    else {
        $(el).Child()[1].innerHTML = ""; //清空聊天记录
        U.F.FM.ChatRecord(el[0], friendandgroupinfo, page, isgroup);
        el.style.display = "block"; //显示聊天记录框
    }
    U.F.W.ChatBoxSize(el.Parent(3), el.Parent()); //调整大小
}

//
/**
* 聊天记录分页
* @param  {element} 聊天框
* @param  {object} 好友或者群信息
* @param  {int} 第几页
* @param  {string} 变化的样式
*/
U.F.FM.ChatRecord = function (el, friendandgroupinfo, page, isgroup) {
    var _end = page * 50, //分页的结束位置
        _start = _end - 50; //分页的开始位置
    //群的获取聊天记录
    if (isgroup) {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "GetGroupChatHistory",
                            friendandgroupinfo.GroupManageID, _start, _end], U.F.FM.AsynChatRecord, ["", el, friendandgroupinfo, page, isgroup]);
    }
    //好友的获取聊天记录
    else {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "GetFriendChatHistory",
                            US.userInfo.userid, friendandgroupinfo.UserId, _start, _end], U.F.FM.AsynChatRecord, ["", el, friendandgroupinfo, page, isgroup]);
    }
}

//获取聊天记录异步
U.F.FM.AsynChatRecord = function (r) {
    var context = r.context,
        _recentcontactsel = context[1], //聊天记录元素
        _friendandgroupinfo = context[2], //好友或者群的信息
        _page = context[3], //页数
        _isgroup = context[4], //是否群聊天记录
        _childs = $(_recentcontactsel).Child(); //0 聊天记录查看区域 1 分页区域
    r = r.value;
    _childs[0].innerText = ""; //清空聊天记录
    if (r.length) {
        //打印聊天记录
        U.F.FM.PrintChatRecord(_childs[0], r, _friendandgroupinfo);
        //聊天记录到底部
        _childs[0].scrollTop = _childs[0].scrollHeight;
        //如果没有加载分页控件,则加载
        if (!$("div", _childs[1]).length) {
            //调用分页接口打印分页
            U.PG.PPage(_childs[1], r[0].AllC, _page, 50,
                       { "fun": [[U.F.FM.ChatRecord, [_recentcontactsel, _friendandgroupinfo, _page, _isgroup]]], "page": [2]
                       });
        }
    }
    else { }
}

/**
* 聊天记录分页
* @param  {element} 聊天框
* @param  {object} 聊天信息
* @param  {object} 好友信息
*/
U.F.FM.PrintChatRecord = function (el, recordinfo, friendandgroupinfo) {
    var _tempel,
        _temponeel,
        _temptwoel,
    i = recordinfo.length,
    _frag = $$("frag");
    //循环打印群聊天记录
    while (i--) {
        _tempel = $$("div", { "className": "UD_SYFLO" }, _frag);
        _temponeel = $$("div", { "className": "UD_SYFLOT" }, _tempel);
        $$("div", { "className": "UD_SYFLOTN", "innerHTML": recordinfo[i].MessageDescript || recordinfo[i].GroupMessageSendUsername || friendandgroupinfo.FriendsDescript || friendandgroupinfo.GroupManageName }, _temponeel);
        $$("div", { "className": "UD_SYFLOTT", "innerHTML": U.MT.getDateInterval(recordinfo[i].MessageSendTime || recordinfo[i].GroupMessageSendTime) }, _temponeel);
        $$("div", { "className": "UD_SYFLOD", "innerHTML": recordinfo[i].MessageContent || recordinfo[i].GroupMessageContent }, _tempel);
    }
    el.appendChild(_frag);
}

//#endregion


/**
* 发送消息统一调用函数
* @param  {object} 统一发送好友信息id
----------好友id
----------群id
----------群发id
* @param  {element} 发送信息原
* @param  {element} 打印区域函数
* @param  {boolean} 判断消息
---------- true 获取群消息 或者发送 群消息
---------- flase 获取好友消息或者 获取好友消息
* @param  {boolean} 是否清空发送
*/
U.F.FM.SendFriendOrGroupChatMessage = function (friendandgroupinfo, chateditel, chatcontentnewsel, isgroup, isupload) {
    var _newsinfo, //发送消息的打印的变量
        _chatcontent = chateditel.innerHTML, //发送的内容
        _friendid = friendandgroupinfo.UserId || friendandgroupinfo.GroupManageID; //发送给好友或者群的id
    //不为空允许发送内容
    if (_chatcontent.isHtmlNull()) {
        U.F.FM.SendChatMessageToDb(_friendid, _chatcontent, null, isgroup); //调用发送数据库的函数
        //发送消息的信息
        _newsinfo = [
                     { "UserId": US.userInfo.userid,
                         "Time": null,
                         "Content": _chatcontent,
                         "IMG": US.userInfo.UserThumbnailImageHead,
                         "Name": US.userInfo.UserNickName
                     }
                    ]
        //内容显示在聊天区域
        if (isgroup) {
            U.F.W.PrintFriendChatMessage(chatcontentnewsel, _newsinfo, true); //打印在好友聊天
        }
        else {
            U.F.W.PrintGroupChatMessage(chatcontentnewsel, _newsinfo, true); //打印在群聊天
        }
        //判断是否是上传文件,如果是上传文件,那么不清空输入区域的内容直接显示到聊天区域。
        if (!isupload) {
            chateditel.innerText = ""
        }
    }
}

/**
* 查看聊天 好友或者群
* @param  {element} 发送好友的id
* @param  {string} 发送的内容
----------如果这里为空 那么就是获取好友信息
* @param  {function} 发送成功回调函数
* @param  {boolean} 判断消息
---------- true 获取群消息 或者发送 群消息
---------- flase 获取好友消息或者 获取好友消息
*/
U.F.FM.SendChatMessageToDb = function (friendid, message, cb, isgroup) {
    var _friendorgroupinfo, //好友或者群信息
        _userinfo = US.userInfo; //用户的信息

    //发送群消息到数据库
    if (isgroup) {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "SendMessageToGroup", friendid, Guid.newGuid(),
                             _userinfo.UserId, message, _userinfo.UserNickNme]), U.M.apply(), [""];
    }
    //发送好友消息到数据库
    else {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "SentMessageToFriend", _userinfo.UserId, friendid,
                             _userinfo.UserNickName, message, 0], U.M.apply(), [""]);
    }

    //发送消息后根据id得到发送的消息的群或者用户id得到详细信息,然后把这个群或者用户的信息变成第一个最近联系人的第一个
    for (i = 1; i < US.Friend.length; i++) {
        //如果是群,则获取群用户信息
        if (isgroup) {
            _friendorgroupinfo = _$(US.Friend[i]).Select({ "GroupManageID": friendid })[0];
        }
        //如果是聊天好友,则获取群用户信息
        else {
            _friendorgroupinfo = _$(US.Friend[i]).Select({ "UserId": friendid })[0];
        }
        //如果判断发送的好友或者是群确实在用户好友里,则把该用户设置为最近联系人
        if (_friendorgroupinfo) {
            U.F.FM.RecentContacts(_friendorgroupinfo); //设置最近联系人
            break;
        }
    }

}

/**
* 查看聊天 好友或者群
* @param  {element} 发送好友的id
* @param  {string} 发送的内容
----------如果这里为空 那么就是获取好友信息
* @param  {function} 发送成功回调函数
* @param  {boolean} 判断消息
---------- true 获取群消息 或者发送 群消息
---------- flase 获取好友消息或者 获取好友消息
*/
U.F.FM.GetFriendOrGroupNews = function (friendid, cb, isgroup) {
    var _userinfo = US.userInfo; //获取用户的资料
    //如果是群信息获取
    if (isgroup) {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "GetGroupUnreadMessage", _userinfo.UserId, friendid],
                    cb, ["", friendid, isgroup]);
    }
    //如果是好友信息的获取
    else {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "GetFriendUnreadMessage", _userinfo.UserId, friendid],
                    cb, ["", friendid, isgroup]);
    }
}

/**
* 生成最近联系人
* @param  {object} 用户信息
*/
U.F.FM.RecentContacts = function (userinfo) {
    var _recentcontactsel,
        _num,
        _recentcontactsinfo,
        _contentel = $("#UD_SYHZLZ")[0];

    if (US.Friend[4]) {
        //判断联系人是否已经在最近联系人中
        _num = US.Friend[4].objIndexOf({ "UserId": userinfo.UserId || userinfo.GroupManageID });
        //如果存在最近联系人,那么直接从最近联系人中找到
        if (_num > -1) {
            _recentcontactsinfo = US.Friend[4].splice(_num, 1)[0];
        }
        //否则生成一个新的最近联系人
        else {
            //生成一个新的最近联系人
            _recentcontactsinfo = new U.F.T.RecentContactsEntity(userinfo.GroupManageID || userinfo.UserId,
                                      userinfo.GroupManageName || userinfo.UserName, userinfo.UserImageHead || "", userinfo.UserNickName || "",
                                      userinfo.UserThumbnailImageHead || "", userinfo.maxMessageSendTime || new Date(),
                                      userinfo.GroupManageDescript || userinfo.FriendsDescript, userinfo.GroupManageRemark || userinfo.UserIndividualitysignature || "",
                                      userinfo.IsLogin || 3, userinfo.GroupManageID ? 1 : 0);
        }

        //如果存在就直接置顶 不存在添加新的
        US.Friend[4].unshift(_recentcontactsinfo);

        //找到最近联系人按钮
        _recentcontactsel = $("#UD_SYHZLTA").childg()[2];
        _recentcontactsel = $(_recentcontactsel).Child()[0];
        //如果当前显示的不是好友、群而是最近联系人,那么则重新打印
        if (_recentcontactsel.className.indexOf("UD_SYHZLTSO") > -1) {
            _recentcontactsel = $("div@TID=" + (userinfo.GroupManageID || userinfo.UserId), $("#UD_SYHZLZ")[0])[0]; //获取最近联系人
            //如果最近联系人原本就存在,则直接追加到最前面
            if (_recentcontactsel) {
                ($(_recentcontactsel).appendTo(_contentel, 0, _contentel.firstChild)); //追加到第一个
            }
            //否则打印最近联系人
            else {
                U.F.PrintFriendOrGroup(_contentel, US.Friend[4][0], true); //插入最新联系人在最前面
            }
        }
    }
}



//#endregion