//<script src="https://apis.google.com/js/api:client.js"></script>
var googleUser = {};
var client;
var accessToken;

window.loadGoogleLogin = function() {
  client = google.accounts.oauth2.initTokenClient({
    client_id: $('#btngoogle').data('google-id'),
    scope: 'openid profile email',
    callback: logarGoogle
  });
  attachSignin(document.getElementById('btngoogle'));
};

function attachSignin(element) {
  if (client) {
    element.addEventListener('click', (e) => {
      e.preventDefault();
      client.requestAccessToken();
    });
  }
}

function logarGoogle(obj) {
  var tipoValidacaoSocial = $('#btngoogle').data('tipo-validacao-social');

  if (obj) {
    accessToken = obj.access_token ?? '';

    $.ajax({
      url: 'index.php?route=account/login/socialLogin',
      type: 'post',
      data: {
        origemLogin: 'google',
        tokenloginsocial: accessToken,
        tipo_validacao_social: tipoValidacaoSocial,
      },
      dataType: 'json',
      success: function(json) {
        if (!(tipoValidacaoSocial == 'vincular')) {
          if (json['login']) {
            if (window.location.href.indexOf('route=checkout/checkout') != -1) {
              location.reload();
            } else {
              document.formlogin.submit();
            }
          } else {
            notificationMsg(json['warning'], 'warning');
            $("input[name='email']").val(json['emailRedeSocial']);
            $("input[name='firstname']").val(json['primeiroNomeRedeSocial']);
            $("input[name='lastname']").val(json['sobrenomeRedeSocial']);
          }
        } else {
          if (!json['warning_login_vinculado']) {
            if (json['login_vinculado']) {
              $('.divgoogleconnect').addClass('hidden');
              $('.divgoogledisconnect').removeClass('hidden');
              notificationMsg($('#btngoogle').data('text-google-vinculado'), 'success');
            } else {
              $('.divgoogleconnect').removeClass('hidden');
              $('.divgoogledisconnect').addClass('hidden');
              notificationMsg($('#btngoogle').data('text-google-desvinculado'), 'success');
            }
          } else {
            notificationMsg(json['warning_login_vinculado'], 'warning');
          }
        }
      },
    });
  }
}

window.googleUnlink = function() {
    $.ajax({
      url: 'index.php?route=account/account/unlink_social_media_access',
      type: 'post',
      dataType: 'json',
      data: {
        ajax: true,
        redesocial: 'google',
      },
    }).done(function(json) {
      if(json['result']){
        $('.divgoogleconnect').removeClass('hidden');
        $('.divgoogledisconnect').addClass('hidden');
        notificationMsg($('#btngoogledisconnect').data('text-google-desvinculado'), 'success');
      } else {
        console.log('Ocorreu um problema ao desvincular a conta!');
        notificationMsg('Ocorreu um erro ao fazer o desvinculo dos dados do Google!', 'warning');
      }
    }).fail(function(json) {
      console.log('error');
      console.log(json);
    });
}
