<commit_before>"use strict";

var ls = localStorage;
var iteration = 0;
var intervalId = null;

var newsLimit = 8; // The most news you can cram into Infoscreen, if other features are disabled

var mainLoop = function(force) {
  console.lolg("\n#" + iteration);

  if (ls.showCantina === 'true')
    if (force || iteration % UPDATE_CANTINAS_INTERVAL === 0)
      updateCantinas();
  if (ls.showAffiliation1 === 'true')
    if (force || iteration % UPDATE_NEWS_INTERVAL === 0)
      updateAffiliationNews('1');
  if (ls.showAffiliation2 === 'true')
    if (force || iteration % UPDATE_NEWS_INTERVAL === 0)
      updateAffiliationNews('2');
  // Only if hardware
  if (Affiliation.org[ls.affiliationKey1].hw)
    if (ls.showOffice === 'true')
      if (force || iteration % UPDATE_OFFICE_INTERVAL === 0)
        updateOffice();
    if (ls.showOffice === 'true')
      if (force || iteration % UPDATE_SERVANT_INTERVAL === 0)
        updateServant();
    if (ls.showOffice === 'true')
      if (force || iteration % UPDATE_MEETINGS_INTERVAL === 0)
        updateMeetings();
    if (ls.showOffice === 'true')
      if (force || iteration % UPDATE_COFFEE_INTERVAL === 0)
        updateCoffee();
  // Always update, tell when offline
  if (ls.showBus === 'true')
    if (force || iteration % UPDATE_BUS_INTERVAL === 0)
      updateBus();

  // No reason to count to infinity
  if (10000 < iteration)
    iteration = 0;
  else
    iteration++;
}

var updateOffice = function(debugStatus) {
  console.lolg('updateOffice');
  Office.get(function(status, message) {
    if (DEBUG && debugStatus) {
      status = debugStatus;
      message = 'debugging';
    }
    if (ls.infoscreenOfficeStatus !== status || ls.infoscreenOfficeStatusMessage !== message) {
      if (Object.keys(Office.foods).indexOf(status) > -1) {
        if (typeof Office.foods[status].image !== 'undefined') {
          // Food status with image
          $('#office #status img').attr('src', Office.foods[status].image);
          $('#office #status #text').hide();
          $('#office #status img').show();
        }
        else {
          // Food status with just title
          $('#office #status #text').text(Office.foods[status].title);
          $('#office #status #text').css('color', Office.foods[status].color);
          $('#office #status img').hide();
          $('#office #status #text').show();
        }
      }
      else {
        // Regular status
        $('#office #status #text').html(Office.statuses[status].title);
        $('#office #status #text').css('color', Office.statuses[status].color);
        $('#office #status img').hide();
        $('#office #status #text').show();
      }
      // Save them
      ls.infoscreenOfficeStatus = status;
      ls.infoscreenOfficeStatusMessage = message;
      // Check for Affiliation specific status message
      var msgs = Affiliation.org[ls.affiliationKey1].hw.statusMessages;
      if (msgs)
        if (msgs[status])
          message = msgs[status];
      $('#office #subtext').html(message);
    }
  });
}

var updateServant = function() {
  console.lolg('updateServant');
  Servant.get(function(servant) {
    $('#todays #schedule #servant').html('- '+servant);
  });
}

var updateMeetings = function() {
  console.lolg('updateMeetings');
  Meetings.get(function(meetings) {
    meetings = meetings.replace(/\n/g, '<br />');
    if (ls.affiliationKey1.match(/online|abakus/g) === null) {
      $('#todays #schedule #meetings').html(meetings);
    }
    else {
      Hackerspace.get(function(hackerspace) {
        $('#todays #schedule #meetings').html(meetings + '<br />' + hackerspace);
      }); 
    }
  });
}

var updateCoffee = function() {
  console.lolg('updateCoffee');
  Coffee.get(true, function(pots, age) {
    $('#todays #coffee #pots').html('- '+pots);
    $('#todays #coffee #age').html(age);
  });
}

var updateCantinas = function(first) {
  // This function just fetches from localstorage (updates in background)
  console.lolg('updateCantinas');
  var update = function(shortname, data, selector) {
    // Set name
    var name = Cantina.names[shortname];
    $('#cantinas '+selector+' .title').html(name);
    // Set hours
    var hours = data.hours;
    $('#cantinas '+selector+' .hours').html(hours);
    // Set dinners
    var menu = data.menu;
    $('#cantinas '+selector+' #dinnerbox').html(listDinners(menu));
  };
  var cantina1Data = JSON.parse(ls.cantina1Data);
  var cantina2Data = JSON.parse(ls.cantina2Data);
  update(ls.cantina1, cantina1Data, '.first');
  update(ls.cantina2, cantina2Data, '.second');
}

var listDinners = function(menu) {
  var dinnerlist = '';
  // If menu is just a message, not a menu: (yes, a bit hackish, but reduces complexity)
  if (typeof menu === 'string') {
    dinnerlist += '<li>' + menu + '</li>';<commit_msg>infoscreen: temp new functions for updating office, servant, meetings, coffee<commit_after>"use strict";

var ls = localStorage;
var iteration = 0;
var intervalId = null;

var newsLimit = 8; // The most news you can cram into Infoscreen, if other features are disabled

var mainLoop = function(force) {
  console.lolg("\n#" + iteration);

  if (ls.showCantina === 'true')
    if (force || iteration % UPDATE_CANTINAS_INTERVAL === 0)
      updateCantinas();
  if (ls.showAffiliation1 === 'true')
    if (force || iteration % UPDATE_NEWS_INTERVAL === 0)
      updateAffiliationNews('1');
  if (ls.showAffiliation2 === 'true')
    if (force || iteration % UPDATE_NEWS_INTERVAL === 0)
      updateAffiliationNews('2');
  // Only if hardware
  if (Affiliation.org[ls.affiliationKey1].hw) {
    if (ls.showOffice === 'true') {
      if (force || iteration % UPDATE_OFFICE_INTERVAL === 0) {
        Browser.getBackgroundProcess().updateAffiliation(function() {
          updateOffice();
          updateServant();
          updateMeetings();
          updateCoffee();
        });
      }
    }
  }
  // if (Affiliation.org[ls.affiliationKey1].hw)
  //   if (ls.showOffice === 'true')
  //     if (force || iteration % UPDATE_OFFICE_INTERVAL === 0)
  //       updateOffice();
  //   if (ls.showOffice === 'true')
  //     if (force || iteration % UPDATE_SERVANT_INTERVAL === 0)
  //       updateServant();
  //   if (ls.showOffice === 'true')
  //     if (force || iteration % UPDATE_MEETINGS_INTERVAL === 0)
  //       updateMeetings();
  //   if (ls.showOffice === 'true')
  //     if (force || iteration % UPDATE_COFFEE_INTERVAL === 0)
  //       updateCoffee();
  // Always update, tell when offline
  if (ls.showBus === 'true')
    if (force || iteration % UPDATE_BUS_INTERVAL === 0)
      updateBus();

  // No reason to count to infinity
  if (10000 < iteration)
    iteration = 0;
  else
    iteration++;
}

var updateOffice = function(debugStatus) {
  console.lolg('updateOffice');
    
  // Get
  var affiliation1Data = JSON.parse(ls.affiliation1Data);
  
  // Presume the worst
  var status = 'error';
  var title = Office.statuses['error'].title;
  var message = Office.statuses['error'].title;
  var meetings = 'En feil oppstod.';

  try {
    // Extract relevant objects
    var meetingData = affiliation1Data.meeting;
    var statusData = affiliation1Data.status;

    // Extract meeting data
    if (meetingData.error) {
      meetings = meeting.error;
    }
    else {
      status = meetingData.free ? 'open' : 'meeting';
      message = meetingData.message;
      if (meetingData.meetings) {
        meetings = '';
        for (var i in meetingData.meetings) {
          meetings += meetingData.meetings[i].message + (i !== "0" ? '\n' : '');
        }
      }
    }

    // Extract status data
    if (statusData.error) {
      message = statusData.error;
    }
    else {
      if (status === 'error' || status === 'open') {
        status = (statusData.status ? 'open' : 'closed');
      } // else leave status unchanged, it's a meeting
      if (meetingData.error) {
        // Set a message manually
        message = Office.statuses[status].message;
      }
    }
  }
  catch (e) {
    console.error(e);
  }

  // console.lolg('well, we got:\nstatus:',status,'\nmessage',message,'\nmeetings',meetings);

  //
  // Run the old script, expects [status, message, meetings]
  //

  if (DEBUG && debugStatus) {
    status = debugStatus;
    message = 'debugging';
  }
  if (ls.infoscreenOfficeStatus !== status || ls.infoscreenOfficeStatusMessage !== message) {
    if (Object.keys(Office.foods).indexOf(status) > -1) {
      if (typeof Office.foods[status].image !== 'undefined') {
        // Food status with image
        $('#office #status img').attr('src', Office.foods[status].image);
        $('#office #status #text').hide();
        $('#office #status img').show();
      }
      else {
        // Food status with just title
        $('#office #status #text').text(Office.foods[status].title);
        $('#office #status #text').css('color', Office.foods[status].color);
        $('#office #status img').hide();
        $('#office #status #text').show();
      }
    }
    else {
      // Regular status
      $('#office #status #text').html(Office.statuses[status].title);
      $('#office #status #text').css('color', Office.statuses[status].color);
      $('#office #status img').hide();
      $('#office #status #text').show();
    }
    // Save them
    ls.infoscreenOfficeStatus = status;
    ls.infoscreenOfficeStatusMessage = message;
    // Check for Affiliation specific status message
    var msgs = Affiliation.org[ls.affiliationKey1].hw.statusMessages;
    if (msgs)
      if (msgs[status])
        message = msgs[status];
    $('#office #subtext').html(message);
  }
}

var updateServant = function() {
  console.lolg('updateServant');
  // Get
  // Hope for the best
  try {
    var data = JSON.parse(ls.affiliation1Data);
    var servant = data.servant.message;
    $('#todays #schedule #servant').html('- '+servant);
  }
  catch (e) {
    console.error(e);
    $('#todays #schedule #servant').html('- '+Servant.msgError);
  }
  // // OLD
  // Servant.get(function(servant) {
  //   $('#todays #schedule #servant').html('- '+servant);
  // });
}
// var updateServant = function() {
//   console.lolg('updateServant');
//   Servant.get(function(servant) {
//     $('#todays #schedule #servant').html('- '+servant);
//   });
// }

var updateMeetings = function() {
  console.lolg('updateMeetings');
  // Get
  var affiliation1Data = JSON.parse(ls.affiliation1Data);
  // Extract relevant information
  try {
    var meetings = '';
    for (var i in affiliation1Data.meeting.meetings) {
      meetings += (i!=="0"?"\n":"") + affiliation1Data.meeting.meetings[i].message;
    }

    // OLD CODE
    meetings = meetings.replace(/\n/g, '<br />');
    // Online and Abakus gets the Hackerspace info as well as meetings
    if (ls.affiliationKey1.match(/online|abakus/g)) {
      Hackerspace.get(function(hackerspace) {
        $('#todays #schedule #meetings').html(meetings + '<div id="hackerspace">' + hackerspace + '</div>');
        $('#todays #schedule #meetings #hackerspace span').click(function(elem) {
          Browser.openTab(Hackerspace.web);
          window.close();
        });
      });
    }
    else {
      $('#todays #schedule #meetings').html(meetings);
    }
  }
  catch (e) {
    console.error(e);
    $('#todays #schedule #meetings').html(Meetings.msgError);
  }
}
// var updateMeetings = function() {
//   console.lolg('updateMeetings');
//   Meetings.get(function(meetings) {
//     meetings = meetings.replace(/\n/g, '<br />');
//     if (ls.affiliationKey1.match(/online|abakus/g) === null) {
//       $('#todays #schedule #meetings').html(meetings);
//     }
//     else {
//       Hackerspace.get(function(hackerspace) {
//         $('#todays #schedule #meetings').html(meetings + '<br />' + hackerspace);
//       }); 
//     }
//   });
// }

var updateCoffee = function() {
  console.lolg('updateCoffee');
  // New
  // Get
  var affiliation1Data = JSON.parse(ls.affiliation1Data);
  // Hope for the best
  try {
    // console.warn('Coffee data is', affiliation1Data.coffee);
    var date = affiliation1Data.coffee.date;
    var pots = affiliation1Data.coffee.pots;
    // Parse that date
    date = new Date(date);
    var age = Coffee.minuteDiff(date);

    // We have pots and age, now get pretty versions
    var prettyPots = Coffee.prettyPotsString(pots);
    var hours = date.getHours(); hours = (hours < 10 ? '0' + hours : hours);
    var minutes = date.getMinutes(); minutes = (minutes < 10 ? '0' + minutes : minutes);
    var prettyAge = Coffee.prettyAgeString(age, [hours, minutes])

    $('#todays #coffee #pots').html('- ' + prettyPots);
    $('#todays #coffee #age').html(prettyAge);
  }
  catch (e) {
    console.error(e);
    $('#todays #coffee #pots').html('- ' + Coffee.msgConnectionError);
    $('#todays #coffee #age').html(Coffee.msgComforting);
  }

  // // Old
  // Coffee.get(true, function(pots, age) {
  //   $('#todays #coffee #pots').html('- ' + pots);
  //   $('#todays #coffee #age').html(age);
  // });
}
// var updateCoffee = function() {
//   console.lolg('updateCoffee');
//   Coffee.get(true, function(pots, age) {
//     $('#todays #coffee #pots').html('- '+pots);
//     $('#todays #coffee #age').html(age);
//   });
// }

var updateCantinas = function(first) {
  // This function just fetches from localstorage (updates in background)
  console.lolg('updateCantinas');
  var update = function(shortname, data, selector) {
    // Set name
    var name = Cantina.names[shortname];
    $('#cantinas '+selector+' .title').html(name);
    // Set hours
    var hours = data.hours;
    $('#cantinas '+selector+' .hours').html(hours);
    // Set dinners
    var menu = data.menu;
    $('#cantinas '+selector+' #dinnerbox').html(listDinners(menu));
  };
  var cantina1Data = JSON.parse(ls.cantina1Data);
  var cantina2Data = JSON.parse(ls.cantina2Data);
  update(ls.cantina1, cantina1Data, '.first');
  update(ls.cantina2, cantina2Data, '.second');
}

var listDinners = function(menu) {
  var dinnerlist = '';
  // If menu is just a message, not a menu: (yes, a bit hackish, but reduces complexity)
  if (typeof menu === 'string') {
    dinnerlist += '<li>' + menu + '</li>';
{'fraction_non_alphanumeric': 0.17327221438645982, 'fraction_numerical': 0.006276445698166431, 'mean_word_length': 2.9141595362958874, 'pattern_counts': {'":': 1, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7607625', 'n_tokens_mistral': 4780, 'n_tokens_neox': 4579, 'n_words': 1021}