//js for first creation of event.
var sql = require('./sql');

exports.createNewEvent = function(req, res){
  console.log(req.user);
  if(req.user == null){
    res.redirect('/auth/facebook');
  }
  //should have a session and profile object.
  else{
    //generate new game: for now just load up current game
    var fbid = req.user.facebook.id;
    var uid;
    var gid;
    sql.getUserFromFbid(getUid, fbid);
    function getUid(err, rows){
        //take the rows and grab
        if(err){
            console.log(err);
        }
        uid = rows[0];
    }
    if (uid == null){//need to add this user
        sql.registerUser(function(err){}, fbid, req.user.facebook.name);
    } else{
        sql.getGroupsFromUid(getGid, uid);
        function getGid(err, rows){
            if(err){
                console.log(err);
            }
            gid = rows[0];
        }
    }
    if(gid == null){
        gid = 1;
    }
    res.redirect('/game/'+ gid);
  }
};
exports.joinEvent = function(req, res){
  console.log(req.user);
  if(req.user == null){
    res.redirect('/auth/facebook');
  }
  //should have a session and profile object.
  else{
    //generate new game: for now just load up current game
    var fbid = req.user.facebook.id;
    var uid;
    var gid;
    sql.getUserFromFbid(getUid, fbid);
    function getUid(err, rows){
        //take the rows and grab
        if(err){
            console.log(err);
        }
        uid = rows[0];
    }
    if (uid == null){//need to add this user
        sql.registerUser(function(err){}, fbid, req.user.facebook.name);
    } else{
        sql.getGroupsFromUid(getGid, uid);
        function getGid(err, rows){
            if(err){
                console.log(err);
            }
            gid = rows[0];
        }
    }
    if (gid == null){//this user needs a group
        //add user to some group?
        gid = 0;
    }
    res.redirect('/geo/'+ gid);
  }
};


@@ -540,32 +540,36 @@
    uid = rows%5B0%5D
+.uId
 ;%0A    %7D%0A    if (
@@ -648,41 +648,250 @@
 rr)%7B
-%7D, fbid, req.user.facebook.name);
+sql.getUserFromFbid(getUid, fbid);%7D, fbid, req.user.facebook.name);%0A        function getUid(err, rows)%7B%0A            //take the rows and grab%0A            if(err)%7B%0A            console.log(err);%0A            %7D%0A        uid = rows%5B0%5D.uId;%0A        %7D
 %0A   
@@ -1064,32 +1064,36 @@
    gid = rows%5B0%5D
+.gId
 ;%0A        %7D%0A    
@@ -1665,24 +1665,26 @@
  rows%5B0%5D
+.uId
 ;%0A    
-%7D%0A
     if (
@@ -1720,32 +1720,36 @@
 is user%0A        
+    
 sql.registerUser
@@ -1763,16 +1763,50 @@
 on(err)%7B
+sql.getUserFromFbid(getUid, fbid);
 %7D, fbid,
@@ -1839,95 +1839,89 @@
     
-%7D else%7B%0A        sql.getGroupsFromUid(getGid, uid);%0A        function getGid(err, rows)%7B%0A
+        function getUid(err, rows)%7B%0A                //take the rows and grab%0A    
     
@@ -1929,32 +1929,36 @@
        if(err)%7B%0A
+    
                 
@@ -1979,32 +1979,36 @@
 r);%0A            
+    
 %7D%0A            gi
@@ -1997,33 +1997,37 @@
   %7D%0A            
-g
+    u
 id = rows%5B0%5D;%0A  
@@ -2026,34 +2026,338 @@
 s%5B0%5D
-;%0A        %7D%0A    %7D%0A
+.uId;%0A                console.log(uid);%0A                if (uid == null)%7B//this user needs a group%0A                    //add user to some group?%0A                    uid = 0;%0A                %7D%0A                console.log(uid + 'sup homie');%0A                res.redirect('/geo/'+ uid);%0A            %7D%0A        %7D else%7B%0A        
     if (
 gid 
@@ -2352,17 +2352,17 @@
     if (
-g
+u
 id == nu
@@ -2391,32 +2391,40 @@
 a group%0A        
+   
+     
 //add user to so
@@ -2445,23 +2445,91 @@
     
-gid = 0;%0A    %7D%0A
+        uid = 0;%0A            %7D%0A            console.log(uid + 'sup homie');%0A        
     
@@ -2546,26 +2546,42 @@
 ct('/geo/'+ 
-g
+u
 id);%0A
+        %7D%0A    %7D%0A
   %7D%0A%7D;%0A%0A

fix passing uid into geo