/**
 * @copyright 2021 Double Bastion LLC <www.doublebastion.com>
 *
 * @author Double Bastion LLC
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

$(document).ready(function() {

  $("#save_fax_settings").on("click", function(event) {

    event.preventDefault();
    OC.msg.startAction("#paxfax_save_msg", t("pax_fax", "Saved"));

    var baseUrl = OC.generateUrl("/apps/pax_fax/user/updatesettings");
    var userid = "<?php p($userId); ?>";
    var getnotify = 0;

    if ($("#get_notification").is(':checked')) {
        getnotify = 1;
    }

    var emailaddressinit = $("#notification_email").val();

    function validate_email($email) {
        var email_reg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        return email_reg.test($email);
    }

    if (validate_email(emailaddressinit)) {
         var notifyemail = emailaddressinit;
    } else {
         var notifyemail = '';
    }

    var datatosave = {
                      userId: userid,
                      apiKey: $("#api_key").val(),
                      apiSecret: $("#api_secret").val(),
                      webhookToken: $("#webhook_token").val(),
                      receiveUrl: $("#receive_url").val(),
                      getNotification: getnotify,
                      notificationEmail: notifyemail
    };

    $.ajax({
       method: 'PUT',
       url: baseUrl + '/' + userid,
       contentType: 'application/json',
       data: JSON.stringify(datatosave),
       error: function(resp){
                  alert('Error ! Please check your settings !');
       }
    });

  });

  var baseUrlget = OC.generateUrl("/apps/pax_fax/user/getsettings");
  var userid = "<?php p($userId); ?>";

  $.ajax({
       method: 'GET',
       url: baseUrlget + '/' + userid,
       contentType: 'application/json',
       success: function(settingsfromdb) {
          $("#api_key").val(settingsfromdb.api_key);
          $("#api_secret").val(settingsfromdb.api_secret);
          $("#webhook_token").val(settingsfromdb.webhook_token);
          $("#receive_url").val(settingsfromdb.receive_url);
          $("#notification_email").val(settingsfromdb.notification_email);

          if (settingsfromdb.get_notification == 1) {
              $("#get_notification").attr('checked', true);
          } else {
              $("#get_notification").attr('checked', false);
          }
       }
  });

});