Graphics.prototype.setFontBuildingTypeface = function(scale) {
  // Actual height 100 (102 - 3)
  this.setFontCustom(
    atob('[encoded data, 9808 characters]'),
    46,
    atob("FCYpGigoKigoJykoFA=="),
    126+(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 draw() {
  var x = g.getWidth()/2;
  var y = g.getHeight()/2;
  g.reset();
  var date = new Date();
  var timeStr = require("locale").time(date,1);
  var dateStr = require("locale").date(date).toUpperCase();
  // draw time
  g.setFontAlign(0,0).setFont("BuildingTypeface");
  g.clearRect(0, 24, g.getWidth(), y+35); // clear the background
  g.drawString(timeStr,x,y);
  // draw date
  y += 60;
  g.setFontAlign(0,0).setFont("6x8",2);
  g.clearRect(0,y-8,g.getWidth(),y+8); // clear the background
  g.drawString(dateStr,x,y);
  // queue draw in one minute
  queueDraw();
}

// Clear the screen once, at startup
g.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;
  }
});
// Show launcher when middle button pressed
Bangle.setUI("clock");
// Load widgets
Bangle.loadWidgets();
Bangle.drawWidgets();
