let opLvl = 0;
let w1aw;
let log = [];
var fullLog = false;

function modelChanged(reason, unit, details) {
  switch (reason) {
    case 'online':
      window.shareRender();
      break;
    case 'offline':
      break;
    case 'beforeDelete':
      break;
    case 'afterDelete':
      break;
    case 'changed':
      window.shareRender();
      break;
    case 'force':
      window.shareRender();
      break;
    case 'boot':
      window.shareRender();
      break;
    case 'afterLoad':
      break;
    case 'changedType':
      break;
    case 'showCfg':
      break;
    case 'stopSendingUnit':
      if (typeof details != 'string') details = '';
      putLog('stop sending to PPK: ' + details);
      break;
  }
}

// done-s fixed
function saveValueForElement(elt) {
  return;
}

// done-s fixed
function refreshBoth() {
  return;
}

// done-s fixed
function putLog(str) {
  let ssss = '';
  ssss = ssss + new Date().toLocaleTimeString('RU') + ' - ' + str;
  if (log.length > 1500) log.shift();
  log.push(ssss);
}

function sendConfigToPPK(w1a, only1) {
  if (sendingUnit) {
    stopSendingUnit('You stopped uploading');
  } else {
    // setW2("t");
    justSendConfigToPPK(w1a.curUnit, w1a.curSut, only1);
    // document.getElementById("dict2ppk").style.animation =
    //     "blinker 0.3s cubic-bezier(.5, 0, 1, 1) infinite alternate";
    // document.getElementById("dict2ppk").style.color = "red";
  }
}

function finalFullLog() {
  fullLog = false;
}

function systemLog(critical, text) {
  if (critical) {
    alert(text);
  } else {
    console.log(text);

    let ssss = '';
    ssss = ssss + new Date().toLocaleTimeString('RU') + ' - ' + text;
    if (log.length > 1500) log.shift();
    log.push(ssss);
  }
}
// window.shareRender()

// todo-s some difference can be later updated
function createCfg() {
  let cfg = {
    lang: lang,
    oper: opLvl,
    version: schema.version,
    state: sys.state,
  };

  function copySub(s, c) {
    for (let n of ['_typeName', '_sut', '_sun', 'config', '_deleted']) c[n] = s[n];
    c._subUnits = {};
    for (let stn in s._subUnits)
      if (stn) {
        c._subUnits[stn] = {};
        try {
          if (!s._desc.__.subUnits[stn].virtual)
            /* 
                          done-p: 
                          this fix for save config in local storage. grey view does not set ._configured for children units
                          old value :  if (s._subUnits[stn][sun] && sun[0] !== "_" && (s._subUnits[stn][sun]._configured || (s._subUnits[stn][sun]._deleted && s._subUnits[stn][sun]._sut !== "_Box") || !(s._desc.__.subUnits[stn].hardware || s._desc.__.subUnits[stn].software))) {
                      */
            for (let sun in s._subUnits[stn])
              if (
                s._subUnits[stn][sun] &&
                sun[0] !== '_' &&
                (s._subUnits[stn][sun]._configured ||
                  s._subUnits[stn][sun]._sut !== '_Box' ||
                  !(s._desc.__.subUnits[stn].hardware || s._desc.__.subUnits[stn].software))
              ) {
                c._subUnits[stn][sun] = {};
                copySub(s._subUnits[stn][sun], c._subUnits[stn][sun]);
              }
        } catch {}
      } else console.log('undef');
  }

  // done-s for adding _configured to subUnits when click save
  function markConfigured(unit) {
    unit._configured = !unit._deleted;
    if (unit._subUnits)
      for (let subUnit in unit._subUnits)
        if (subUnit[1] != '_')
          for (let sn in unit._subUnits[subUnit]) if (sn[0] != '_') markConfigured(unit._subUnits[subUnit][sn]);
  }

  markConfigured(sys);

  copySub(sys, cfg);
  return JSON.stringify(cfg);
}
