module.exports = function(io){
  var users = {};

  io.on('connection', function (socket) {
    socket.on('add_user', function(data){
      if(!data || !data.username){
        return console.log('data.username不能为空');
      }

      if(typeof data.username !== 'string' || !/^.+$/.test(data.username)
        || data.username.length > 20){
        return console.log('data.username格式错误');
      }

      var username = data.username;

      if(users[username]){
        socket.emit('user_existed', {});
      } else {
        users[username] = {};
        users[username].socketId = socket.id;

        socket.username = username;

        socket.emit('add_user_complete', {
        });
      }
    });

    socket.on('re_connect', function(data){
      if(!data || !data.username){
        return console.log('data.username不能为空');
      }

      if(users[data.username]){
        socket.emit('user_existed', {});
      } else {
        socket.username = data.username;

        users[data.username] = {};
        users[data.username].socketId = socket.id;
        console.log('reconnect success');
      }
    });

    //处理客户端断开连接
    socket.on('disconnect', function(){
      console.log('socket disconnect');

      if(socket.username){
        var friendUsername = users[socket.username].friendUsername;
        users[socket.username] = null;
        if(friendUsername){
          var friend = users[friendUsername];
          if(friend){
            var friendSocketId = friend.socketId;
            var friendSocket = io.sockets.connected[friendSocketId];
            if(friendSocket){
              friendSocket.emit('friend_quit', {});
            }
          }
        }          
      }
    });

    //处理错误
    // socket.on('error', function(err){
    //   console.log('socket error', err);
    // });

    //用户发送邀请
    socket.on('send_invite', function(data){
      if(!data || !data.username){
          return console.log('data.username不能为空');
      }

      if(!data || !data.friendUsername){
          return console.log('data.friendUsername不能为空');
      }

      var me = users[data.username];

      if(!me){
        return console.log('用户' + data.username +'不存在');
      }

      var friend = users[data.friendUsername];

      if(!friend){
        return console.log('用户' + data.friendUsername +'不存在');
      }

      me.friendUsername = data.friendUsername;

      var friendSocket =  io.sockets.connected[friend.socketId];
      
      friendSocket.emit('be_invited', {
        senderName: data.username
      });
    });

    //接受邀请
    socket.on('accept_invite', function(data){
      if(!data || !data.username){
        return console.log('data.username不能为空');
      }

      if(!data || !data.friendUsername){
        return console.log('data.friendUsername不能为空');
      }

      var me = users[data.username];
      var friend = users[data.friendUsername];

      if(!me){
        return console.log('用户' + data.username + '不存在.');
      }

      if(!friend){
        return console.log('用户' + data.friendUsername + '不存在.')
      }

      //设置对手的id
      me.friendUsername = data.friendUsername;

      var friendSocket = io.sockets.connected[friend.socketId];

      friendSocket.emit('invite_accepted', {
          receiverName: data.username
      });

      socket.emit('game_start', {});
    });

    //回答问题
    socket.on('answer', function(data){
      if(!data || !data.username){
        return console.log('data.username不能为空');
      }

      if(!data || !data.answerId){
        return console.log('data.answerId不能为空');
      }

      var me = users[data.username];
      if(!me){
        return console.log('user'+data.username+'不存在');
      }

      me.answerId = data.answerId;

      var friendUsername = me.friendUsername;

      if(!friendUsername){
        return console.log('朋友未加入游戏');
      }

      var friend = users[friendUsername];
      if(!friend){
        return console.log('用户'+friendUsername+'不存在');
      }

      var friendSocket = io.sockets.connected[friend.socketId];

      var friendAnswerId = friend.answerId;

      if(!friendAnswerId){
        return console.log('朋友还没有选择答案');
      }

      
      //双方都提交答案以后, 通知双方对方的答案
      socket.emit('show_result', {
        friendAnswerId: friend.answerId
      });
      friendSocket.emit('show_result', {
        friendAnswerId: me.answerId
      });
    });

    //答题正确以后把上次答案清除
    socket.on('reset_answer', function(data){
      if(!data || !data.username){
        return console.log('data.uid不能为空');
      }

      var me = users[data.username];
      if(!me){
        return console.log('用户' + data.username + '不存在');
      }

      me.answerId = null;
    });
  });
};
























