var MYIMG = {
  width : 176, height : 176, bpp : 4,
  buffer : require("heatshrink").decompress(atob("[encoded data, 4624 characters]"))
};

//var IMAGEWIDTH = 176;
//var IMAGEHEIGHT = 109;
//var IMAGEWIDTH = 176;
var IMAGEHEIGHT = 176;

Graphics.prototype.setFontcustom = function() {
  // Actual height 58 (57 - 0)
  return this.setFontCustom(
    E.toString(require('heatshrink').decompress(atob('[encoded data, 11664 characters]'))),
    32,
    atob("EA4RPC4uLQoZGhwaDxkOKCodHx4pLCMdHCMODx4mHyE0LSgrKiAfLCsZJDImPy4rJC8yHiYoLkEyLiQUFBM="),
    58|65536
  );
};

// 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() {
  //clock pos
  var x = g.getWidth()/2;
  var y = 105;
  g.setColor("#000000"); //color of clock text
  //draw clock
  g.drawImage(MYIMG,0,g.getHeight()-IMAGEHEIGHT, 1);
  //get time/date
  var date = new Date();
  var timeStr = require("locale").time(date,1);
  var dateStr = require("locale").date(date).toUpperCase();
  var dowStr = require("locale").dow(date).toUpperCase();
  // draw time
  g.setFontAlign(0,0).setFont("custom");
  g.drawString(timeStr,x,y);
  // draw date
  g.setColor(1,1,1);
  var dateFormatted = dowStr + ", " + dateStr;
  g.setFont("6x8");
  g.drawString(dateFormatted,g.getWidth()/2,g.getHeight() - 8);
  Bangle.drawWidgets();
  // queue draw in one minute
  queueDraw();
}


g.setTheme({bg:"#000",fg:"#fff",dark:true}).clear();
draw();
Bangle.on('lcdPower',on=>{
  if (on) {
    draw();
  } else {
    if (drawTimeout) clearTimeout(drawTimeout);
    drawTimeout = undefined;
  }
});
Bangle.setUI("clock");
Bangle.loadWidgets();
Bangle.drawWidgets();