var NBC_User = function() { 
};

NBC_User.prototype.loggedIn;
NBC_User.prototype.userId; 
NBC_User.prototype.userName;
NBC_User.prototype.email;
NBC_User.prototype.zipCode;
NBC_User.prototype.userAge;
NBC_User.prototype.userGender;
NBC_User.prototype.token;
NBC_User.prototype.facebookId;
NBC_User.prototype.role;
NBC_User.prototype.picUrl;

NBC_User.prototype.init = function() {
  var userData = this.readUserData();
  if (typeof userData != 'undefined' && typeof userData.userId != "undefined") {
    U.log("Reading user cookie...");
    this.userId = userData.userId;
    U.log("userId has value [" + this.userId + "]");
    
    if (typeof userData.userName != "undefined") {
      this.userName = userData.userName;      
      U.log("userName has value [" + this.userName + "]");      
    }

    if (typeof userData.email != "undefined") {
      this.email = userData.email;
      U.log("email has value [" + this.email + "]");      
    }
    
    if (typeof userData.zipCode != "undefined") {
      this.zipCode = userData.zipCode;
    }
    
    if (typeof userData.userAge != "undefined") {
        this.userAge = userData.userAge;
      }
    
    if (typeof userData.userGender != "undefined") {
        this.userGender = userData.userGender;
      }
    
    if (typeof userData.token != "undefined") {
      this.token = userData.token; 
      U.log("token has value [" + this.token + "]");      
    }
    
    if (typeof userData.fbId != "undefined" && userData.fbId != 'undefined') {
      this.facebookId = userData.fbId;
      U.log("facebookId has value [" + this.facebookId + "]");      
    }

    if (typeof userData.role != "undefined") {
      this.role = userData.role;
      U.log("role has value [" + this.role + "]");      
    }
    
    if (typeof userData.picUrl != "undefined" && userData.picUrl != "undefined") {
        this.picUrl = userData.picUrl;
        U.log("picUrl has value [" + this.picUrl + "]");      
    }
    
    if (this.isValid()) {
      this.loggedIn = true;
    }
  }
  else {
    U.log("User not logged in: No cookie present.");
  }
  
  // wait till all components are loaded, and then fire user.login
  var self = this;
  $(document).ready(function() {
    if (self.isLoggedIn()) {
      U.log("user: firing user.login event");
      NBC_EventManager.fire("user.login");              
    }
  });
};

NBC_User.prototype.isValid = function() {
  return typeof this.userName != "undefined";
}

NBC_User.prototype.isEditor = function() {
  return typeof this.role != "undefined" && this.role == "EDITOR";
}

NBC_User.prototype.setValues = function(args) {
  for (property in args) {
    U.log("Setting user property [" + property + "] with value [" + args[property] + "]");
    this[property] = args[property];
    // login when userName is specified
    if (property === "userName" && args[property] != "undefined") {
      this.loggedIn = true;
    }
  }
  if (this.loggedIn) {
    NBC_EventManager.fire("user.login");      
  }
};

NBC_User.prototype.readUserData = function() {
  if (U.readCookie("u_ka")) { // logged in
    var cookieValue = unescape(U.readCookie("u_ka"));
    U.log("user: User cokie value [" + cookieValue + "]");
    var userCookie = eval("(" + cookieValue + ")");
    var userData = userCookie.user;
    return userData;
  }
};

NBC_User.prototype.persist = function(persistCookie) {
  if (!this.isValid()) {
    throw new Error("Invalid user state.");
  }
  
  // given a response in this JSON format
  // {"ka":{"TOKEN":"jYuha1rthMNYOYEJcscCHW2p6/UiHS5t","METHOD":"POST","PRIVILEGES":"RW","payload_type":"json","userId":4852011,"email":"stanley.kubasek@nbcuni.com"}} 
  // store it as {"user":{"userId":"...,"userName","...","email":"..."}}
  var cookieVal = '{"user":{"token":"' + this.token + '","userId":"' + this.userId + '","userName":"' + this.userName + '","email":"' + this.email + '","role":"' + this.role + '","picUrl":"'+ this.picUrl + '","zipCode":"'+ this.zipCode + '","userAge":"'+ this.userAge + '","userGender":"'+ this.userGender + '","fbId":"' + this.facebookId + '"}}';  

  U.log("user: Writing cookies value [" + cookieVal + "]");
  if (persistCookie) {
    U.createCookie("u_ka", cookieVal, 365); // expire after 1 year
  }
  else {
    U.log("Cookie saved as a session!");
    U.createCookie("u_ka", cookieVal); // session cookie
  }
};

NBC_User.prototype.isLoggedIn = function() {
  return this.loggedIn;
};

NBC_User.prototype.getUserName = function() {
  if (typeof this.userName != "undefined") {
    return this.userName;
  } else if (this.isFacebookUser()) {
    return this.facebookId;
  } else {
    U.log("Warning: user object has userName empty!");
    return this.userName;
  }
};

NBC_User.prototype.isFacebookUser = function() {
  return typeof this.facebookId != 'undefined' && this.facebookId != 'undefined' && this.facebookId != '';
};

NBC_User.prototype.logout = function() {
  U.eraseCookie("u_ka");
  this.loggedIn = false;
  NBC_EventManager.fire("user.logout");  
}; 
