<script src="https://apis.google.com/js/api:client.js"></script>

<script type="text/javascript">
  window.addEventListener('message', function(event) {
    console.log(event.data, event);
  });
  
  gapi.load('auth2', function () {
    var auth2;

    auth2 = gapi.auth2.init({
      client_id: "{{settings.SOCIAL_AUTH_GOOGLE_PLUS_KEY}}",
      scope: "{{settings.SOCIAL_AUTH_GOOGLE_PLUS_SCOPE}}"
    });

    auth2.then(function () {
      var button = document.getElementById("google-plus-button");
      console.log("User is signed-in in Google+ platform?", auth2.isSignedIn.get() ? "Yes" : "No");

      auth2.attachClickHandler(button, {}, function (googleUser) {
        var authResponse = googleUser.getAuthResponse();
        // Send access-token to backend to finish the authenticate
        // with your application
        var action = {
          method: 'POST',
          url : '/complete/google-plus/',
          // waitMsg: "{{_('Please wait...')}}",
          failure: function(response, options) {
              console.log('Ajax failure:', response, options);},
          success: function() {
              console.log('Ajax success:', arguments);},
          params: { 'id_token': authResponse.id_token}
        };
        Ext.Ajax.request(action);
  
        // var $form;
        // var $input;
        // $form = $("<form>");
        // $form.attr("action", "/complete/google-plus");
        // $form.attr("method", "post");
        // $input = $("<input>");
        // $input.attr("name", "id_token");
        // $input.attr("value", authResponse.id_token);
        // $form.append($input);
        // // Add csrf-token if needed
        // $(document.body).append($form);
        // $form.submit();

      });
    });
  });
</script>
