/*****************************************************************************
 * Copyright (C) 2024
 *
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of mbsePi-apps
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * BrewCloud 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/


$(document).ready(function() {
 var global = {},
 url = 'dbglobal.php',
 globalSource = {
  datatype: 'json',
  cache: false,
  datafields: [
   { name: 'type', type: 'string' },
   { name: 'name', type: 'string' },
   { name: 'uuid', type: 'string' },
   { name: 'node', type: 'string' },
   { name: 'os' },
   { name: 'os_version' },
   { name: 'FW' },
   { name: 'server_port', type: 'int' },
   { name: 'websocket_port', type: 'int' },
   { name: 'nextunit', type: 'int' },
   { name: 'temp_uuid', map: 'THB>temperature>uuid' },
   { name: 'temp_state', map: 'THB>temperature>state' },
   { name: 'temp_value', map: 'THB>temperature>value', type: 'float' },
   { name: 'hum_uuid', map: 'THB>humidity>uuid' },
   { name: 'hum_state', map: 'THB>humidity>state' },
   { name: 'hum_value', map: 'THB>humidity>value', type: 'float' },
   { name: 'temp_hum_idx', map: 'THB>index', type: 'int' },
   { name: 'lcd_address', map: 'LCD>address' },
   { name: 'lcd_cols', map: 'LCD>cols', type: 'int' },
   { name: 'lcd_rows', map: 'LCD>rows', type: 'int' },
   { name: 'mqtt_host', map: 'MQTT>host' },
   { name: 'mqtt_port', map: 'MQTT>port', type: 'int' },
   { name: 'mqtt_username', map: 'MQTT>username' },
   { name: 'mqtt_password', map: 'MQTT>password' }
  ],
  id: 'name',
  url: url
 },
 globalData = new $.jqx.dataAdapter(globalSource, {
  loadComplete: function(records) {
   global = globalData.records[0];
   $('#name').val(global.name);
   $('#uuid').val(global.uuid);
   $('#node').val(global.node);
   $('#os').val(global.os + ' ' + global.os_version);
   $('#FW').val(global.FW);
   $('#server_port').val(global.server_port);
   $('#websocket_port').val(global.websocket_port);
   $('#nextunit').val(global.nextunit);
   $('#temp_uuid').val(global.temp_uuid);
   $('#temp_state').val(global.temp_state);
   $('#temp_value').val(global.temp_value);
   $('#hum_uuid').val(global.hum_uuid);
   $('#hum_state').val(global.hum_state);
   $('#hum_value').val(global.hum_value);
   $('#temp_hum_idx').val(global.temp_hum_idx);
   $('#lcd_address').val(global.lcd_address);
   $('#lcd_cols').val(global.lcd_cols);
   $('#lcd_rows').val(global.lcd_rows)
   $('#mqtt_host').val(global.mqtt_host);
   $('#mqtt_port').val(global.mqtt_port);
   $('#mqtt_username').val(global.mqtt_username);
   $('#mqtt_password').val(global.mqtt_password);
  }
 });

 // initialize the input fields.
 $('#name').jqxInput({ theme: theme, width: 240, height: 23 });
 $('#uuid').jqxInput({ theme: theme, width: 360, height: 23 });
 $('#node').jqxInput({ theme: theme, width: 240, height: 23 });
 $('#os').jqxInput({ theme: theme, width: 240, height: 23 });
 $('#FW').jqxInput({ theme: theme, width: 120, height: 23 });
 $('#server_port').jqxNumberInput(Show0dec);
 $('#websocket_port').jqxNumberInput(Show0dec);
 $('#nextunit').jqxNumberInput(Show0dec);
 $('#temp_uuid').jqxDropDownList({
  theme: theme,
  source: tempsensorlist,
  valueMember: 'uuid',
  displayMember: 'name',
  width: 240,
  dropDownWidth: 480,
  autoDropDownHeight: true
 });
 $('#temp_state').jqxInput({ theme: theme, width: 120, height: 23 });
 $('#temp_value').jqxNumberInput(Show1dec);
 $('#hum_uuid').jqxDropDownList({
  theme: theme,
  source: tempsensorlist,
  valueMember: 'uuid',
  displayMember: 'name',
  width: 240,
  dropDownWidth: 480,
  autoDropDownHeight: true
 });
 $('#hum_state').jqxInput({ theme: theme, width: 120, height: 23 });
 $('#hum_value').jqxNumberInput(Show1dec);
 $('#temp_hum_idx').jqxNumberInput(Spin0dec);
 $('#lcd_address').jqxNumberInput(Spin0dec);
 $('#lcd_cols').jqxNumberInput(GPIOInt);
 $('#lcd_rows').jqxNumberInput(GPIOInt);
 $('#mqtt_host').jqxInput({ theme: theme, width: 240, height: 23 });
 $('#mqtt_port').jqxNumberInput(Spin0dec);
 $('#mqtt_username').jqxInput({ theme: theme, width: 240, height: 23 });
 $('#mqtt_password').jqxPasswordInput({ placeHolder: "Enter Password", showPasswordIcon: true, theme: theme, width: 240, height: 23 });

 globalData.dataBind();


 $('#Save').jqxButton({ template: 'success', width: '90px', theme: theme });
 $('#Save').click(function() {
  var data,
  row = {
   name: $('#name').val(),
   port: parseInt($('#server_port').jqxNumberInput('decimal')),
   temp_uuid: $('#temp_uuid').val(),
   hum_uuid: $('#hum_uuid').val(),
   temp_hum_idx: $('#temp_hum_idx').val(),
   lcd_address: parseInt($('#lcd_address').jqxNumberInput('decimal')),
   lcd_cols: parseInt($('#lcd_cols').jqxNumberInput('decimal')),
   lcd_rows: parseInt($('#lcd_rows').jqxNumberInput('decimal')),
   mqtt_host: $('#mqtt_host').val(),
   mqtt_port: parseInt($('#mqtt_port').jqxNumberInput('decimal')),
   mqtt_user: $('#mqtt_username').val(),
   mqtt_pass: $('#mqtt_password').val(),
   websocket_port: parseInt($('#websocket_port').jqxNumberInput('decimal'))
  };
  data = 'update=true&' + $.param(row);
  $.ajax({
   dataType: 'json',
   url: url,
   cache: false,
   data: data,
   type: 'POST',
   success: function(data) {
    if (data.error) {
     console.log('update: ' + data.msg);
     alert('Error: ' + data.msg);
    } else {
     console.log('update: success');
    }
   },
   error: function(jqXHR, textStatus, errorThrown) {}
  });
 });

 websocket.onmessage = function(evt) {
  var msg = evt.data;
  var obj = JSON.parse(msg);

  if (obj.ping) {
   websocket.send('{"pong":' + obj.ping + '}');
  }
 }
});
