const SETTINGS_FILE = "90sclk.setting.json";
const locale = require('locale');
const storage = require('Storage');
const widget_utils = require('widget_utils');


/*
 * Load settings
 */
let settings = {
  fullscreen: false,
};

let saved_settings = storage.readJSON(SETTINGS_FILE, 1) || settings;
for (const key in saved_settings) {
  settings[key] = saved_settings[key]
}


function getImg() {
  return require("heatshrink").decompress(atob("[encoded data, 4540 characters]"));
}

Graphics.prototype.setFontTime = function(scale) {
  // Actual height 54 (56 - 3)
  this.setFontCustom(atob("[encoded data, 4544 characters]"), 46, atob("FionHyIiJiIyJScyFA=="), 58+(scale<<8)+(1<<16));
  return this;
};


Graphics.prototype.setFontDate = function(scale) {
  // Actual height 28 (27 - 0)
  this.setFontCustom(atob("[encoded data, 5232 characters]"), 32, atob("HA4NFhEaFwoNDQsRCRALFBMPEBESEBgSExgKCRARERIXEhQVExEPGBMOERYRFhQaExwUExARFRYTFhMPFA4="), 28+(scale<<8)+(1<<16));
  return this;
}


// timeout used to update every minute
var drawTimeout;

// schedule a draw for the next minute
function queueDraw() {
  if (drawTimeout) clearTimeout(drawTimeout);
  drawTimeout = setTimeout(function() {
    drawTimeout = undefined;
    draw();
  }, 60000 - (Date.now() % 60000));
}


function drawBorderString(str, x, y, b, fc){
  g.setColor("#000");
  g.drawString(str, x, y+b);
  g.drawString(str, x, y-b);
  g.drawString(str, x+b, y);
  g.drawString(str, x-b, y);

  g.setColor(fc);
  g.drawString(str,x+1,y);
}


function getSteps() {
  try{
      if (WIDGETS.wpedom !== undefined) {
          return WIDGETS.wpedom.getSteps();
      } else if (WIDGETS.activepedom !== undefined) {
          return WIDGETS.activepedom.getSteps();
      }
  } catch(ex) {
      // In case we failed, we can only show 0 steps.
  }

  return 0;
}


function draw() {
  // queue draw in one minute
  queueDraw();

  var x = g.getWidth()/2;
  var y_offset = settings.fullscreen ? 0 : 10;
  var y = g.getHeight()/2-20 + y_offset;

  g.reset().clearRect(0,24,g.getWidth(),g.getHeight());
  g.drawImage(getImg(),0,0);

  // Draw time
  var date = new Date();
  var timeStr = locale.time(date,1);
  g.setFontAlign(0,0);
  g.setFontTime();
  drawBorderString(timeStr, x, y, 5, "#fff");

  // Draw date
  y += 50;
  x = x - g.stringWidth(timeStr) / 2 + 5;
  g.setFontDate();
  g.setFontAlign(-1,0);
  var dateStr = locale.dow(date, true).toUpperCase() + date.getDate();
  var fc = Bangle.isLocked() ? "#0ff" :"#fff";
  fc = E.getBattery() < 50 ? "#f00" : fc;
  drawBorderString(dateStr, x, y, 3, fc);

  // Draw steps
  g.setFontAlign(1,1);
  var steps = parseInt(getSteps() / 1000);
  drawBorderString(steps, g.getWidth()-10, g.getHeight()-10, 3, "#f0f");

  // Draw widgets if not fullscreen
  if(settings.fullscreen){
    widget_utils.hide();
  } else {
    Bangle.drawWidgets();
  }
}

// Show launcher when middle button pressed
Bangle.setUI("clock");

Bangle.loadWidgets();

// Clear the screen once, at startup
g.setTheme({bg:"#000",fg:"#fff",dark:false}).clear();
// draw immediately at first, queue update
draw();
// Stop updates when LCD is off, restart when on
Bangle.on('lcdPower',on=>{
  if (on) {
    draw(); // draw immediately, queue redraw
  } else { // stop draw timer
    if (drawTimeout) clearTimeout(drawTimeout);
    drawTimeout = undefined;
  }
});


Bangle.on('lock', function(isLocked) {
  print("LOCK");
  if (drawTimeout) clearTimeout(drawTimeout);
  drawTimeout = undefined;
  draw();
});


