let globalUserState = {
  level: 0,
  title: '',
  loggedInUser: '',
  titleChanged: false,
};

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

function modelChanged(reason, unit, details) {
  switch (reason) {
    case 'startReadAllCfg':
      {
        let buttonId = '';
        if (unit) buttonId = unit._sut;
        readingAllCfg = true;
      }
      break;
    case 'doneReadAllCfg':
      {
        let buttonId = '';
        if (unit) buttonId = unit._sut;
        readingAllCfg = false;
      }
      break;
    case 'finalFullLog':
      fullLog = false;
      break;
    case 'online':
      window.shareRender();
      break;
    case 'offline':
      break;
    case 'beforeDelete':
      break;
    case 'afterDelete':
      break;
    case 'changed':
      window.shareRender();
      break;
    case 'force':
      break;
    case 'boot':
      break;
    case 'afterLoad':
      break;
    case 'changedType':
      break;
    case 'showCfg':
      break;
    case 'stopSendingUnit':
      if (typeof details != 'string') details = '';
      putLog('stop sending to PPK: ' + details);
      break;
    case 'userSet':
      {
        let la = 'EN';
        if (lang) la = lang;
        let n = schema.dictionary.notLoggedIn[la];
        let lvl = 0;
        if (loggedInUser) {
          n = loggedInUser.config.name;
          if (!loggedInUser.config.lang) loggedInUser.config.lang = 0;
          if (!loggedInUser.config.level) loggedInUser.config.level = 0;
          la = schema.languages[loggedInUser.config.lang];
          lvl = loggedInUser.config.level;
        }
        // if (loggedInUser) changeLang(loggedInUser.config.lang);

        if (n == '') n = 'user#' + loggedInUser._sun;
        let tit = 'v' + schema.version + ' ' + n;
        let newLvl = opLvl;
        if (opLvl == 0 && lvl) newLvl = 1;
        if (opLvl > lvl) newLvl = lvl;
        if (opLvl != newLvl) {
          opLvl = newLvl;
          globalUserState.level = opLvl;
          // setUnitViewSub(w1aw, w1aw.curUnit, w1aw.curSut);
        }
        globalUserState.title = tit;
        globalUserState.loggedInUser = n;
      }
      break;
  }
}

// done-s fixed
function saveValueForElement(elt) {
  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);
}

// really used in grey/Sergey - both to start and stop
function sendConfigToPPK(w1a, only1) {
  if (sendingUnit) {
    stopSendingUnit('You stopped uploading');
  } else {
    justSendConfigToPPK(w1a.curUnit, w1a.curSut, only1);
  }
}

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()
