class UserAuthentication {
  //--------------------------
  // constructor
  //--------------------------
  constructor() {
      var self = this;
  }

  //--------------------------
  // methods
  //--------------------------
  start()
  {

  }

  handleUserSignUp(userSignupForm)
  {
    //e.preventDefault();
    let signup_form = document.forms.namedItem(userSignupForm);

    let social_preferences = {"social":[
      {"service":"twitter", "url":""},
      {"service":"fb",  "url":""},
      {"service":"instagram", "url":""},
      {"service":"pinterest", "url":""}
    ]};

    let mail_preferences = {"mail":[
      {"category":"video", "enabled":"false"},
      {"category":"books",  "enabled":"false"},
      {"category":"graphic_novels", "enabled":"false"},
      {"category":"comics", "enabled":"false"}
    ]};

    //var oOutput = document.querySelector("div"),
    let signup_handle = signup_form.handle.value;
    let signup_firstname = signup_form.firstname.value;
    let signup_lastname = signup_form.lastname.value;
    let signup_email = signup_form.email.value;
    let signup_password = signup_form.password.value;
    let social = JSON.stringify(social_preferences);
    let mail = JSON.stringify(mail_preferences);

    //oData.append("CustomField", "This is some extra data");
    var request = new XMLHttpRequest();
    request.open("POST", "/signup", true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader('Cache-Control', 'max-age=0');
    request.setRequestHeader('Upgrade-Insecure-Requests', '1');
    request.onload = function(oEvent) {
      if (request.status == 200) {
        let response = JSON.parse(request.response);
        if(response.message == "USER ADDED")
        {
          document.body.dispatchEvent( new CustomEvent(USER_STATUS,
          {
              'detail':
                {
                  type:"USER_ADDED",
                  data:response.message
                }
          }) );
        }else{
          document.body.dispatchEvent( new CustomEvent(USER_STATUS,
          {
              'detail':
                {
                  type:"USER_NOT_ADDED",
                  data:response.message
                }
          }) );
        }

      } else {
        console.log(request);
      }
    };

    request.send('handle='+signup_handle+'&firstname='+signup_firstname+'&lastname='+signup_lastname+'&email='+signup_email+'&password='+signup_password+"&social="+social+"&preferences="+mail);
  }

  handleUserLogin(userLoginForm)
  {
    //e.preventDefault();
    let login_form = document.forms.namedItem(userLoginForm);

    //var oOutput = document.querySelector("div"),
    let login_handle = login_form.handle.value;
    let login_password = login_form.password.value;

    //oData.append("CustomField", "This is some extra data");
    var request = new XMLHttpRequest();
    request.open("POST", "/login", true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.setRequestHeader('Cache-Control', 'max-age=0');
    request.setRequestHeader('Upgrade-Insecure-Requests', '1');
    request.onload = function(oEvent) {
      if (request.status == 200) {
        let response = JSON.parse(request.response);
        if(response.message == "USER FOUND")
        {
          document.body.dispatchEvent( new CustomEvent(USER_STATUS,
          {
              'detail':
                {
                  type:"USER_LOGIN_GOOD",
                  data:login_handle
                }
          }) );
        }else{
          document.body.dispatchEvent( new CustomEvent(USER_STATUS,
          {
              'detail':
                {
                  type:"USER_LOGIN_BAD",
                  data:response.message
                }
          }) );
        }

      } else {
        console.log(request);
      }
    };

    request.send('handle='+login_handle+'&password='+login_password);
  }

  //--------------------------
  // event handlers
  //--------------------------


}
export {UserAuthentication as default}
export const USER_STATUS = "userStatus"
