
layui.use('layim', function(layim){

    var connection = null;
    connection = new Strophe.Connection(BOSH_SERVICE);
    connection.connect(jid, jidpasswd, onConnect);

    function log(msg)
    {
        console.log(msg);
    }

    function onConnect(status)
    {
        if (status == Strophe.Status.CONNECTING) {
            log('Strophe is connecting.');
        } else if (status == Strophe.Status.CONNFAIL) {
            log('Strophe failed to connect.');
            $('#connect').get(0).value = 'connect';
        } else if (status == Strophe.Status.DISCONNECTING) {
            log('Strophe is disconnecting.');
        } else if (status == Strophe.Status.DISCONNECTED) {
            log('Strophe is disconnected.');
        } else if (status == Strophe.Status.CONNECTED) {
            log('Strophe is connected.');
            log('ECHOBOT: Send a message to ' + connection.jid +
                ' to talk to me.');

            connection.addHandler(onMessage, null, 'message', null, null,  null);
            connection.send($pres().tree());
        }
    }

    // 接收到消息的回调方法
    function onMessage(msg)
    {
        var to = msg.getAttribute('to');
        var from = msg.getAttribute('from');
        var type = msg.getAttribute('type');
        var elems = msg.getElementsByTagName('body');

        if (type == "chat" && elems.length > 0) {
            var body = elems[0];

            log('ECHOBOT: I got a message from ' + from + ': ' +
                Strophe.xmlunescape(Strophe.getText(body)));
            try {
                if(from.indexOf(currentId) == -1) {
                    var message = $.parseJSON(Strophe.xmlunescape(Strophe.getText(body)));
                    var messgeType = message['xmppChatType'];
                    if (messgeType == '_online_all_status_'){
                        var content = message['msg'];
                        updateOnlineStatusByid(content.id,content.status);
                    }else if (messgeType == 'sys') {
                        var content = message['msg'];
                        var obj = {
                            username: "系统通知"
                            , avatar: 'static/layer-v2.3/layim/layui/images/face/notify.png'
                            , id: currentId
                            , type: 'friend'
                            , content: content
                        }
                        layim.getMessage(obj);
                    }
                    else if (messgeType == 'friend' || messgeType == 'group') {
                        var fromId = Strophe.getNodeFromJid(from);
                        var groupid = fromId;
                        var content = message['msg'];
                        var avatar = message['fromPortrait'];
                        var fromUserName = message['fromUserName'];
                        if (messgeType == 'group') {
                            groupid = message['groupId'];
                            //fromUserName = message['groupName'];
                        }
                        var sendtime = message['timeStamp'];

                        if (sendtime != 'NAN') {//使用历史记录时间
                            layim.getMessage({
                                username: fromUserName
                                , avatar: avatar
                                , id: groupid
                                , fromid: fromId
                                , type: messgeType
                                , content: content
                                , timestamp: parseFloat(sendtime)
                            });
                        }
                        else {
                            layim.getMessage({
                                username: fromUserName
                                , avatar: avatar
                                , id: groupid
                                , fromid: fromId
                                , type: messgeType
                                , content: content
                            });
                        }
                    }
                }
            }catch(err)
            {
                log('parse json err');
            }

        }

        // we must return true to keep the handler alive.
        // returning false would remove it after it finishes.
        return true;
    }

/*
	  webSocket.onmessage = function(event) {
	    var res=event.data;
          var message = $.parseJSON(res);
          var messgeType = message['useType'];
          if (messgeType == '_online_all_status_'){
              setTimeout(function(){updateOnlineStatus(eval('('+res+')').body.data);},500);
          }else if (messgeType == 'sys'){
              var contents= message['msg'];
              var fromId= message['to'];
              if ("对方现在离线，他将在上线后收到你的消息!" != contents){
                  index = contents;
              }
              var obj = {
                  username: "系统通知"
                  ,avatar: 'static/layer-v2.3/layim/layui/images/face/notify.png'
                  ,id:fromId
                  ,type: 'friend'
                  ,content: contents
              }
              layim.getMessage(obj);
              return;
          }else if(messgeType == '_msg_'){
              var fromId = message['from'];
              var toUser = message['to'];
              var content = message['msg'];
              var avatar = message['fromPortrait'];
              var type = message['chatType'];
              if (type == 'group'){
                  fromId = message['groupId'];
              }
              var fromUserName = message['fromUserName'];
              var sendtime = message['sendTime'];
              if(sendtime != 'NAN'){//使用历史记录时间
                  layim.getMessage({
                      username: fromUserName
                      ,avatar: avatar
                      ,id: fromId
                      ,type: type
                      ,content: content
                      ,timestamp:parseFloat(sendtime)
                  });
              }else{

                  layim.getMessage({
                      username: fromUserName
                      ,avatar: avatar
                      ,id: fromId
                      ,type: type
                      ,content: content
                  });
              }
          }else if(messgeType == 'sys_me'){
              index = "sys_me";
          }if (messgeType == '_msgMe_'){
              alert("收到回执"+res);

          }

	  };*/

	  //发送消息的方法
	  function send(mine,To) {
	      if(To.type == 'group'){
              var message = {"groupId":To.id,
                  "msg":mine.content,
                  "fromPortrait":mine.avatar,
                  "xmppChatType":To.type,
                  "fromUserName":currentName,
                  "groupIcon":To.avatar,
                  "groupName":To.name,
                  "bodyType":"txt"};
              //webSocket.send(currentId+"_msg_"+To.id+"_msg_"+mine.content+"_msg_"+mine.avatar+"_msg_"+To.type+"_msg_"+currentName+"_msg_"+JSESSIONID+"_msg_NAN");
              var msg = $msg({
                  to: To.id+'@broadcast.'+window.document.domain,
                  from: jid,
                  type: 'chat'}).c("body", null, JSON.stringify(message));
              connection.send(msg.tree());
          }else {
              var message = {"msg": mine.content,
                             "fromPortrait":mine.avatar,
                            "xmppChatType":"friend",   //To.type,
                            "fromUserName":currentName,
                            "bodyType":"txt"};
              //webSocket.send(currentId+"_msg_"+To.id+"_msg_"+mine.content+"_msg_"+mine.avatar+"_msg_"+To.type+"_msg_"+currentName+"_msg_"+JSESSIONID+"_msg_NAN");
              // 创建一个<message>元素并发送
              var msg = $msg({
                  to: To.id+'@'+window.document.domain,
                  from: jid,
                  type: 'chat'}).c("body", null, JSON.stringify(message));
              connection.send(msg.tree());
          }

	  };

	//切换在线状态的方法
	  function setonline() {
			  var message = "{\"userId\":\""+currentId+"\",\"JSESSIONID\":\""+JSESSIONID+"\",\"useType\":\"_online_user_\"}";
          //webSocket.send(message);
          connection.send($pres().tree());
          sychStatus('true');
	  };
	//切换离线状态的方法
	  function sethide() {
          var message = "{\"userId\":\""+currentId+"\",\"JSESSIONID\":\""+JSESSIONID+"\",\"useType\":\"_leave_user_\"}";
          //webSocket.send(message);
          connection.send($pres({type:'unavailable'}).tree());
          sychStatus('true','offline');
	  };

	//更新在线用户信息
	  function updateOnlineStatus(arra)//更新在线用户信息
	  {
	  	 $("div.layui-layim-main ul.layim-list-friend li ul.layui-layim-list li").each(function(){//状态还原
	  		 if($(this).attr("id")){
		  		 var span = $(this).find("span");
		  		 var text=span.text();
		  		 var name = $(this).attr("id").substring(12);
		  		 if(text.indexOf("(")>=0){
		  			 text = text.substring(0,text.indexOf("("));
		  		 }
		  		 if((','+arra+",").indexOf(','+name+',')>=0){

		  			 span.html(text +"(<font color='green'>在线</font>)");
		  			 var $one_li = $(this).parent().find("li:eq(0)");    //  获取<ul>节点中第一个<li>元素节点
		  			 $(this).insertBefore($one_li);    //移动节点
		  		 }else{

		  			 span.html(text+"(<font color='color'>离线</font>)");
		  		 }
	  		 }
	  	 });
	  }

    function updateOnlineStatusByid(id,status)//更新在线用户信息
    {
        $("div.layui-layim-main ul.layim-list-friend li ul.layui-layim-list li[id='layim-friend"+id+"']").each(function(){//状态还原
            var span = $(this).find("span");
            console.log($(this).html());
            var text=span.text();
            if(text.indexOf("(")>=0){
                text = text.substring(0,text.indexOf("("));
            }
            if(status=='online') {
                span.html(text + "(<font color='green'>在线</font>)");
                var $one_li = $(this).parent().find("li:eq(0)");    //  获取<ul>节点中第一个<li>元素节点
                $(this).insertBefore($one_li);    //移动节点
            }else {
                span.html(text+"(<font color='color'>离线</font>)");
                var parentUl = $(this).parent();
                var size = $(parentUl).children().size();
                var $last_li = $(parentUl).find("li").eq(size-1);
                $(this).insertAfter($last_li);
            }
        });
    }

  //基础配置
  layim.config({

    //初始化接口
    init: {
	  url: url+'/iim/contact/friend'
      ,data: {}
    }

    //简约模式（不显示主面板）
    //,brief: true

    //查看群员接口
    ,members: {
    	url: url+'/iim/contact/getMembers'
      ,data: {}
    }

    ,uploadImage: {
      url: url+'/iim/contact/uploadImage'//（返回的数据格式见下文）
      ,type: 'post' //默认post
    }

    ,uploadFile: {
      url: url+'/iim/contact/uploadFile' //（返回的数据格式见下文）
      ,type: 'post' //默认post
    }

    //,skin: ['http://cdn.firstlinkapp.com/upload/2016_4/1461747766565_14690.jpg'] //皮肤
    //,isgroup: false //是否开启群组
    ,chatLog: url+'/iim/chatHistory?userid1='+currentId+"&userid2=" //聊天记录地址
    /*,find: url+'/iim/contact/searchUsers'*/
    //,copyright: true //是否授权
  });

  /*
  layim.chat({
    name: '在线客服-小苍'
    ,type: 'kefu'
    ,avatar: 'http://tva3.sinaimg.cn/crop.0.0.180.180.180/7f5f6861jw1e8qgp5bmzyj2050050aa8.jpg'
    ,id: -1
  });
  layim.chat({
    name: '在线客服-心心'
    ,type: 'kefu'
    ,avatar: 'http://tva1.sinaimg.cn/crop.219.144.555.555.180/0068iARejw8esk724mra6j30rs0rstap.jpg'
    ,id: -2
  });
  */
  //layim.setChatMin();

  //监听发送消息
  layim.on('sendMessage', function(data){
	  var mine = data.mine; //包含我发送的消息及我的信息
	    var To = data.to; //对方的信息
//	    console.log(mine);
//	    console.log(To);

	    //发送消息到WebSocket服务
	    send(mine,To);

  });

  //监听在线状态的切换事件
  layim.on('online', function(data){
    if(data=="online"){
    	setonline(); //用户上线
    }else{
    	sethide();//用户离线
    }

  });




  //监听收到的聊天消息
  /*
  socket.on('chatMessage', function (res) {
    layim.getMessage({
      username: res.name
      ,avatar: res.avatar
      ,id: res.id
      ,type: res.type
      ,content: res.content
    });
  });
  */

  //layim建立就绪
  layim.on('ready', function(res){

	  //更改签名
          var signObj = $('#layim_user_sign_box');
          signObj.focus(function () {
              signObj.removeClass('layim-sign-hide');
              console.log('准备更改签名');
          });
          signObj.blur(function () {
              console.log('更改签名完毕');
              signObj.addClass('layim-sign-hide');
          });
          //enter提交
          signObj.keydown(function (event) {
              if (event.which == 13) {
                  console.log('按下了Enter提交签名');
                  signObj.addClass('layim-sign-hide');
                  signObj.blur();
                  var value= $("#layim_user_sign_box").val();
                  $.post(url+"/sys/user/saveSign",{'sign':value},function(data){

                	  top.layer.alert(data.msg, {icon: 1});
                  })

              }
          });

      sychStatus('true');
      setInterval(sychStatus, 2*60*1000,'false');

//    //添加好友（如果检测到该socket）
//    layim.addList({
//      type: 'group'
//      ,avatar: "http://tva3.sinaimg.cn/crop.64.106.361.361.50/7181dbb3jw8evfbtem8edj20ci0dpq3a.jpg"
//      ,groupname: 'Angular开发'
//      ,id: "12333333"
//      ,members: 0
//    });
//    layim.addList({
//      type: 'friend'
//      ,avatar: "http://tp2.sinaimg.cn/2386568184/180/40050524279/0"
//      ,username: '冲田杏梨'
//      ,groupid: 2
//      ,id: "1233333312121212"
//      ,remark: "本人冲田杏梨将结束AV女优的工作"
//    });
//
//    //接受消息（如果检测到该socket）
//    setTimeout(function(){
//      //不在好友列表，则为临时会话
//      layim.getMessage({
//        username: "Hi"
//        ,avatar: "http://tva1.sinaimg.cn/crop.7.0.736.736.50/bd986d61jw8f5x8bqtp00j20ku0kgabx.jpg"
//        ,id: "198909151014"
//        ,type: "friend"
//        ,content: "临时："+ new Date().getTime()
//      });
//
//      //在好友列表
//      layim.getMessage({
//        username: "贤心"
//        ,avatar: "http://tp1.sinaimg.cn/1571889140/180/40030060651/1"
//        ,id: "100001"
//        ,type: "friend"
//        ,content: "嗨，你好！欢迎体验LayIM。演示标记："+ new Date().getTime()
//        ,timestamp: new Date().getTime()
//      });
//    }, 1000);
  });

  //监听查看群员
  layim.on('members', function(data){
    console.log(data);
  });

  //监听聊天窗口的切换
  layim.on('chatChange', function(data){
    console.log(data);
  });

    function sychStatus(flag,status) {
        $.ajax({
            url:url+'/iim/contact/friendStatus',
            data:{
                'flag':flag,
                'status':status
            },
            success:function(res){
                if(res.success){
                    updateOnlineStatus(res.body.userStr);
                }
            }
        })
    }

});