/*
好友管理器中心 包含了 删除好友 修改好友名字等接口
*/
Namespace.register("U.MD.F.M"); //好友管理器的命名空间

//#region 添加好友分组

/**
* 弹出好友分组
*
*/
U.MD.F.M.createFriendGroupPopup = function () {
    var _tempel,
        _temponeel,
        _temptwoel;
    //创建好友分组的样式
    _tempel = $$("div", { "className": "U_MD_U_L_Box_I U_MD_F_Add_TNL U_MD_F_G" });
    _temponeel = $$("div", {}, _tempel)
    _temptwoel = $$("input",
     {
         "placeholder": "请输入你要创建的分组",
         "onfocus": function () {
             U.UF.MR.inputPlaceholder(this, '请输入你要创建的分组名', "")
         },
         "onblur": function () {
             U.UF.MR.inputPlaceholder(this, "", '请输入你要创建的分组名');
         }
     }, _temponeel);
    //弹出好友对话框
    U.UF.UI.confirm(_tempel, function () {
        U.MD.F.M.createFriendGroup(_temptwoel);
    }); //创建一个对话框
}

/**
* 确定添加好友
*
* @param  {string} 好友分组id
*/
U.MD.F.M.createFriendGroup = function (inputel) {
    var _value = inputel.value, //好友分组名字
        _groupid = Guid.newGuid(); //好友分组id
    //好友分组的名字不能大于20
    if (_value.length > 20) {
        U.alert("分组名不可超过20个字符");
    }
    //好友分组名字必须填写
    else if (!_value.isStrNull() && _value != inputel.defaultValue) {
        U.A.Request(US.CD, [US.DB, "UseStudio_Friends", "CreateFriendClass", _groupid, US.userInfo.UserId, _value, ""], U.MD.F.M.asynCreateFriendGroup, ["", _value, _groupid]); //发送添加分组的请求
    }
    //没有输入的提示
    else {
        U.alert("请输入正确的群名");
    }
}

//添加好友分组异步
U.MD.F.M.asynCreateFriendGroup = function (r) {
    var _groupinfo,
        _context = r.context;
    r = r.value; //得到服务器返回的值
    _groupinfo = new U.MD.F.T.friendGroupEntity(_context[2], _context[1], ""); //初始化一个好友分组
    //在好友数组的全局变量中添加好友分组的信息
    US.friend.allclass.push(_groupinfo);
    //重新点击打印好友分组,重新刷新分组
    $($("#U_MD_F_H_ZLTA").Child()[0]).Child()[0].onclick();
    //提示创建成功
    U.alert("创建" + _context[1] + "分组成功");
}

//#endregion

//#region 删除好友

/**
* 删除好友
*
* @param  {object} 删除用户信息
*/
U.MD.F.M.deleteFriend = function (friendinfo, callback) {
    //数据库中删除好友
    U.A.Request(US.FRIEND, ["DeleteFriend", US.userInfo.UserId, friendinfo.FriendsID, friendinfo.UserId], U.MD.F.M.asynDeleteFriend, ["", friendinfo, callback]); //ajax删除好友
}

/**
* 删除好友异步
*
* @param  {object} 删除用户信息
*/
U.MD.F.M.asynDeleteFriend = function (r) {

    var _context = r.context,
        _message = "好友已删除",
        _friendinfo = _context[1]; //好友的信息;
    U.Json.del(US.friend.friends, { "FriendsID": _friendinfo.FriendsID }); //好友集里删除好友

    var _friendclassinfo = U.Json.select(US.friend.allclass, { "FriendsGroupID": _friendinfo.FriendsGroupID })[0], //好友分组的信息
        _friendclassuserinfo = U.Json.select(US.friend.friends, { "FriendsGroupID": _friendinfo.FriendsGroupID }), //好友分组的信息
        _el = $(".U_MD_F_H_ZLZOCO"), //获取用户的所有好友
        _friendel = $('#g' + _friendclassinfo.FriendsGroupID).Child(),
        _switchbootom = $("#U_MD_F_H_ZLTA").Child(); //好友或者群切换按钮

    //判断是否在当前好友选取下
    if ($('#U_MD_F_H_ZLTA .U_MD_F_H_ZLTOO')[0]) {
        //删除好友元素
        for (var i = 0; i < _el.length; i++) {
            if (_el[i].TID == _friendinfo.UserId) {//获取指定的删除的好友
                $(_el[i]).remove();      //删除指定的好友元素
                U.MD.F.W.delteFriendChat(_el[i].TID, $("#U_MD_F")[0]);  //删除指定的好友聊天窗口
                _friendel[0].innerHTML = _friendclassinfo.FriendsGroupName + "(" + (_friendclassuserinfo.length) + ")"; //修改分组长度
                break;
            }
        }
        //修改好友信息
        //如果所有好友都清空了,那么则打印空好友的提示
        if (US.friend.allclass.length == 1 && !_friendclassuserinfo.length) {
            U.MD.F.printHasNoFriend(_friendel[1]);
        }
        _message = '好友' + (_friendinfo.UserNickName || _friendinfo.UserName) + '已从好友列表中删除';
    }

    var _win = $('.U_MD_F_TO[user="9dff9339-2d37-4072-b62e-3b0a027ae997"]');
    _win[0] && $('.U_MD_F_TOC', _win)[0].onclick();

    U.UF.C.isFunction(_context[2]) && _context[2]();

    U.alert(_message);
};

//#endregion

//#region 添加好友

/**
* 添加好友
*
* @param  {object} 用户信息
* @param  {object} 回调函数
* @param  {object} 删除用户信息
*/
U.MD.F.M.addFriend = function (userinfo, cb, typename) {
    if (!U.MD.U.L.isLogin()) {
        typename = typename || "1"; //判断是添加直接对方为好友还是添加对方为好友需好友验证。
        //无法加本人为好友 同时不可与本人对话
        if (userinfo.UserId != US.userInfo.UserId) {
            //在好友里获取用户
            var _friendinfo = U.Json.select(US.friend.friends, { "UserId": userinfo.UserId })[0];
            //判断是否已经添加了用户
            if (!_friendinfo) {
                U.MD.F.M.addFriendForm(userinfo, cb, typename); //弹出添加好友框
            }
            //如果用户已经是自己的好友,那么直接跳出聊天
            else {
                U.MD.F.W.popupFriendsForm(_friendinfo); //弹出聊天框
            }
        } else { U.Alert("不可以添加自己为好友哟!"); }
    }
    else { U.Alert("快速登录关注他(她)吧"); }
}

/**
* 添加好友
*
* @param  {object} 用户信息
* @param  {object} 删除用户信息
*/
U.MD.F.M.addFriendForm = function (userinfo, cb, typename) {
    var i,
        _formel,
        _tempel,
        _temponeel,
        _temptwoel,
        _tempthreeel,
        _tempfourel,
        _friendgroupinfo = US.friend.allclass; //好友分组
    var _date = new Date();
    //创建添加好友信息分组信息
    _tempel = $$("div", { "className": "U_MD_U_UV_IAC" });
    _temponeel = $$("div", { "className": "U_MD_U_UV_IACA" }, _tempel);
    _temptwoel = $$("div", { "className": "U_MD_U_UV_IACL" },
    _temponeel); _tempthreeel = $$("div", { "className": "U_MD_U_UV_IACLT" }, _temptwoel);
    $$("img",
     {
         "onerror": U.MD.C.imgError,
         "title": "点击查看",
         "src": U.MD.C.getHeadImage(userinfo.UserThumbnailImageHead)
     },
        _tempthreeel);
    _tempthreeel = $$("div", { "className": "U_MD_U_UV_IACLB" }, _temptwoel);
    $$("div", { "className": "U_MD_D_Text_Abbreviation", "innerHTML": userinfo.UserNickName || userinfo.UserName }, _tempthreeel);
    $$("div", { "className": "U_MD_D_Text_Abbreviation", "innerHTML": "性别:" + (userinfo.UserSex ? "男" : "女") }, _tempthreeel);
    $$("div", { "className": "U_MD_D_Text_Abbreviation", "innerHTML": "年龄:" + (U.UF.D.getYearMonthDay(_date.toLocaleDateString())[3] - U.UF.D.getYearMonthDay(userinfo.Birthday)[3]) }, _tempthreeel);
    _temptwoel = $$("div", { "className": "U_MD_U_UV_IACR" }, _temponeel);
    _tempthreeel = $$("div", { "className": "U_MD_U_UV_IACRT" }, _temptwoel);
    $$("div", { "className": "U_MD_U_UV_IACRTL", "innerHTML": "备注姓名:" }, _tempthreeel);
    _tempthreeel = $$("div", { "className": "U_MD_U_UV_IACRTR" }, _tempthreeel);
    _memoName = $$("input", { "value": userinfo.UserNickName || userinfo.UserName }, _tempthreeel);
    _tempthreeel = $$("div", { "className": "U_MD_U_UV_IACRB" }, _temptwoel);
    $$("div", { "className": "U_MD_U_UV_IACRBL", "innerHTML": "分&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp组:" }, _tempthreeel);
    _tempthreeel = $$("div", { "className": "U_MD_U_UV_IACRBR" },
     _tempthreeel);
    _tempfourel = $$("select", {}, _tempthreeel);
    //添加用户到制定的好友分组处理
    _tempfourel.options.length = _friendgroupinfo.length;
    for (i = 0; i < _friendgroupinfo.length; i++) {
        _tempfourel.options[i].text = _friendgroupinfo[i].FriendsGroupName;
    }
    //创建好友分组的弹框
    _formel = U.UF.UI.confirm(_tempel, function () {
        U.MD.F.M.addFriendToDb(userinfo, cb, _tempfourel, _temponeel, typename, _memoName.value);
    });
    _formel.form.style.width = "400px";
    return _formel;
}

/**
* 确定添加好友
*
* @param  {object} 删除用户信息
* @param  {function} 删除用户信息
*/
U.MD.F.M.addFriendToDb = function (userinfo, cb, selectel, loadingel, typename, _memoName) {
    var _friendgroupinfo = US.friend.allclass[selectel.selectedIndex], //获取用户选择的分组
        _groupid = _friendgroupinfo.FriendsGroupID, //好友分组id
        _friendid = Guid.newGuid(), //用户在分组里面的唯一识别id
        _messageid = Guid.newGuid(); //用户发送的消息的唯一识别id
    _descript = $(".U_MD_U_UV_IACRTR")[0].childNodes[0].value; //用户备注
    //添加好友分组的ajax
    //对方发送添加好友信息给你,同意后把用户直接加到好友分组里
    if (typename == "0") {
        U.A.Request(US.FRIEND, ["AgreeAddFriend", US.userInfo.UserId, userinfo.UserId, _friendid, _groupid, (userinfo.UserNickName || userinfo.UserName), userinfo.MessageContent.toUpperCase()],
                 U.MD.F.M.asynAddFriendToDb, [loadingel, _groupid, userinfo, _friendgroupinfo, cb, _friendid]); //添加好友到默认分组
    }
    //添加用户为好友
    else {
        U.A.Request(US.CD, ([US.DB, "UseStudio_Friends", "AddFriend", US.userInfo.UserId, _friendid, userinfo.UserId, _groupid, _messageid, (userinfo.UserNickName || userinfo.UserName), ("" || _descript)]), U.MD.F.M.asynAddFriendToDb, [loadingel, _groupid, userinfo, _friendgroupinfo, cb, _friendid]); //添加好友到默认分组
    }
    //提示
    //U.alert("正在添加中...");
    return false;
}

/**
* 添加好友到分组异步
*
* @param  {object} 添加成功后的信息
*/
U.MD.F.M.asynAddFriendToDb = function (r) {
    var _context = r.context,
        _loadingel = _context[0], //loading元素
        _groupid = _context[1], //群id
        _userinfo = _context[2], //用户的信息
        _friendgroupinfo = _context[3], //
        _cb = _context[4],
        _friendid = _context[5];

    r = r.value;

    //同意添加对方为好友的时候处理
    if (r) {
        //设置添加好友
        U.MD.F.M.displayAddFriend(_userinfo, _friendid, _groupid, _friendgroupinfo, r);
        //添加好友的回调处理
        if (_cb) {
            _cb();
        }
        U.alert('添加好友成功,快和他发起聊天吧');


    }
    //添加对方为好友,给对方发送消息处理
    else {
        U.alert("您的请求已经发送,请等待对方的同意。");
    }
    //关闭loading的弹窗
    $(_loadingel).Parent({ usform: "true" }).remove();
}

/**
* 添加好友成功后的处理
*
* @param  {object} 添加成功后的信息
*/
U.MD.F.M.displayAddFriend = function (userinfo, friendid, classid, friendgroupinfo, r) {
    var _childs,
        _groupel,
    //好友分组的信息
        _friendinfo = new U.MD.F.T.friendEntity(userinfo.UserIndividualitysignature, userinfo.UserId, userinfo.UserImageHead,
                                             userinfo.UserThumbnailImageHead, userinfo.UserNickName || userinfo.UserName,
                                             userinfo.UserName, friendid, _descript, classid, 0);
    _friendinfo["IsLogin"] = r[1] == "True" ? 1 : 0; //设置用户在线
    US.friend.friends.unshift(_friendinfo); //添加好友到好友列表里

    //打印聊天显示窗体
    _groupel = $('#g8f128439-6323-47bb-bc68-86b97a3c4d11');
    //     $('div[tid="069dba1f-f6a4-4629-ba97-0459d9b041d7"]')
    if (_groupel[0]) {
        _childs = $(_groupel).Child(); //0 好友分组信息 1好友信息
        //如果是第一次添加好友,那么提示好友的信息删除
        //        if (US.friend.friends.length == 1) {
        //            $("#U_MD_F_H_ZR")[0].innerText = "";
        //        };
        //打印添加的好友信息
        U.MD.F.printFriendOrGroup(_childs[1], [_friendinfo], true); //打印好友
        //好友分组信息
        _childs[0].innerText = friendgroupinfo["FriendsGroupName"] + "(" + (U.Json.select(US.friend.friends, { "FriendsGroupID": classid }).length) + ")";
    };
    //添加成功后打开好友聊天对话
    U.MD.F.W.popupFriendsForm(_friendinfo);
    //重新刷新
    $($("#U_MD_F_H_ZLTA").Child()[0]).Child()[0].onclick();

    //右边的好友窗体也重新打印
    if (!US.friend.recentcontacts.length) {
        U.MD.F.printRightFormFriend(US.friend.friends, $("#U_MD_F_H_RC")[0]);
    }
}

//#endregion