/**
 * @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_sipph_settings').on('click', function(event){

    event.preventDefault();
    OC.msg.startAction('#sip_trip_phone_save_msg', t('sip_trip_phone', 'Saved'));

    var baseUrl = OC.generateUrl('/apps/sip_trip_phone/user/updatesettings');
    var checkedornot = 0;
    if ($("#tracesipmsg").is(":checked")) { checkedornot = 1; }

    var datatobesaved = {
                          pdisplayname: $('#pdisplayname').val(),
                          sipusername: $('#sipusername').val(),
                          sipuserpassword: $('#sipuserpassword').val(),
                          stphwssurl: $('#stphwssurl').val(),
                          siprealm: $('#siprealm').val(),
                          stunserver: $('#stunserver').val(),
                          tracesipmsg: checkedornot,
                          voicenumbers: $('#voicenumbers').val(),
                          defaultvoicenumber: $('#defaultvoicenumber').val()
    };

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

    });

  });

  var baseUrl = OC.generateUrl('/apps/sip_trip_phone/user/getsettings');

  $.ajax({
       method: 'GET',
       url: baseUrl,
       contentType: 'application/json',
       success: function(settingsdb) {
          $("#pdisplayname").val(settingsdb.pdisplayname);
          $("#sipusername").val(settingsdb.sipusername);
          $("#sipuserpassword").val(settingsdb.sipuserpassword);
          $("#stphwssurl").val(settingsdb.stphwssurl);
          $("#siprealm").val(settingsdb.siprealm);
          $("#stunserver").val(settingsdb.stunserver);
          $('#voicenumbers').val(settingsdb.voicenumbers);
          $('#defaultvoicenumber').val(settingsdb.defaultvoicenumber);

	  if (settingsdb.tracesipmsg == 1) {
	      $('#tracesipmsg').attr('checked', true);
	  } else {
	      $('#tracesipmsg').attr('checked', false);
	  }
       }
  });

});
