const locale = require("locale");

let hour;
let minute;
let date;

let timer;

function getBackground() {
  return {
    width: 120,
    height: 96,
    bpp: 8,
    buffer: require("heatshrink").decompress(atob("AGkrlcqq0q0nP6vP54HClRN/AEtVqvV1XOAAIDBAJWkqpV/cztV1Wq6vOAIZ4EA4KADAA+jPn50ZMIx2EAAr1MAIN/lUrM34AOqpuDAKYAP0hp/ABbLCMQgDLfJDzO52jlRu/AAsrwHV1TrVeJCWCAZZ5/AAmkLQLtGABYPPeZ1/Ov9V552CbJLzaABujvJ17leAKgzNNeJADPEpejO3TPI6gFFOQxUBCI4BOC4QtFQQkqO21VYonP1XP0uB0gFBOQwAKdpoBD52r1YrB0ZyCAAOpAoVVO23V1fQ1nX1gAGwOrPgLJCAJLrP54tC1otH1YrBPQVPO2WA550GwIBIAAOkdIp0BcI4BGc4IrDE4wABFYmleIVOO2A5BdIxCDAZWr0r1HdJJ1BOhKmMwPPqp1tlZIBJQhOTwLzFeInPAIYlFABr1GFYJ4swHXAAJ2FAB4TEOgbvEAAJ2B1QqDTybzF1d5O1OBJIY4HBIxaL1mq1PO1J4B5ztD1agEcpTxOPAN/O1rhJNxADI1nPeIp2IAIpwMGpJ4mqztJAAlWAIIABAoRMKwOr1bxCPQPV1nXE4wrHAAYrDfBesqp2jlbtIwOCGgMrlYWHR4LpEeI+reIXO652FAIYrQeJZ3jOwTVHqxIHAA5OKAAOj1YpGdoh0IPY7qEeI2BO0jREFwQdSJoLtFEYagKFaikLYB4AQOghKEqwgVlYhHOhTsBFatWd4zwhlfXYogDCOyx4FPRh2YPAYpHJzBSG65KfeKArceIQhDAoglZOwTMEdrTxQwR2bAAKeHF4R4ZDwgBDOzjFGZI4reE5B3adwwkBJTooCeJQqeUYKkHFLJ3BTorCeJhkrFb7qEeIgiYdwggbFJZ4GFUErKYoCBd7DFHO8QqBOw1WFcLwGAoMrEC7FEAAJLiYgZ4EFUJ3CUgwgXJQgDCS64rQAIQqiUYbxEK6+sd4RODJcR3COwh3mFIYDBO652C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YBILwIAJNAiYCTgILDOw54Dd4p2Cq0zwLMJAAV5d4KkBvB3LwHVqocDEQYACklPU4KCBquqJwYBBNhYLDbYYBTQwZ0GdolUOApTGbQIABlV4C4WiO5bJBEQgBEAAdVAQL7CeQbxOQZT5KfowBCA4LtF0d/qteOxBTFAAaQDqp2JqvODgyVDAYcrmcImczwNceYOAv7fC1SWBAobpYdopyFOIWApy2Bq0zrxABI4R0LAAV4OwIfBO5Oq57rKAAoLBG4OCrter2BwOIAIWrwOswGkwAyBKoJ8GAAJoBAAOq5oFBNQt5vNVwNWAIVdrtWAIJyBOwOBAYJHGA4ZdHp7vC5p3J6uACwQABdYYBFlQDCg4QBOwNePQQABxJ6Cr2CAoIACRANdAI2AA4tdq4TBTwQmDF4IBCVwICDrrtDgLDGJYJdCBQrvC0V5Ow/OO4QABVoIbEOwYABPAcAIoJOCOQIFCAJpmGABczAIZwBAIQABdYIPDJgJHDlcsdopbCeIkq0ej52iOw0rO4NVCQJ2KAI4ABHwIAEdIOCdoWseQL9BAIbhBc4RYBwJgBAYTlFA4NWq5sDcwIJBGgTxCH4JZCdpDuGgEqd4XOlR3FvPV6ulR4gAGFw4HCXIRSBbozxNC4ZwCPwMydYoBHq54CfIg/EOAkAOxAABp/HPAV5O4vVd4OAORABIAAY3Br0rOAZeCAAbtFNAJqDNxgAFA44JDdwZ4EYw5bHzmiAAOjLgmjOwPO0iPHd44sGIAR2BOgTzHQAwAFLwIDHAJjrCPAhDFd4gFDB4150TxB514O4eqd6srhAOBrpDCdZjvFNBzxFAY4yBAAhLFKYgGEKo8r0buBeAkqOwTvCSIggFlQBBqtOVIRSFOwjtCA4YDDcKSANd4p4CAAMBljnIOw53B5wBB0VPO4POOwXOwB2CSIwABvNOAITtCX4bhEdote1j3HeJ4PBC4boGAAw+BIILCDNw7vHgGjAAeigA"))
  };
}

function draw() {
  const d = new Date();

  const newHour = ('0' + d.getHours()).substr(-2);
  const newMinute = ('0' + d.getMinutes()).substr(-2);
  const newDate = locale.date(d).trim();

  g.setFontAlign(0, 0, 0);

  if (newHour !== hour) {
    g.setFont('6x8', 5);
    g.setColor(0x0000);
    g.drawString(hour, 76, 100);
    g.setColor(0xFFFF);
    g.drawString(newHour, 76, 100);
    hour = newHour;
  }

  if (newMinute !== minute) {
    g.setFont('6x8', 5);
    g.setColor(0x0000);
    g.drawString(minute, 164, 100);
    g.setColor(0xFFFF);
    g.drawString(newMinute, 164, 100);
    minute = newMinute;
  }

  if (newDate !== date) {
    g.setFont('6x8', 2);
    g.setColor(0x0000);
    g.drawString(date, 120, 228);
    g.setColor(0xFFFF);
    g.drawString(newDate, 120, 228);
    date = newDate;
  }
}

function startDrawing() {
  hour = '';
  minute = '';
  date = '';
  g.setColor(0x0000);
  g.fillRect(0, 216, 240, 240);
  g.drawImage(getBackground(), 0, 24, { scale: 2 });
  Bangle.drawWidgets();
  draw();
  timer = setInterval(draw, 1000);
}

function stopDrawing() {
  if (timer) {
    clearInterval(timer);
    timer = undefined;
  }
}

Bangle.on('lcdPower', (on) => {
  stopDrawing();
  if (on) {
    startDrawing();
  }
});

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

Bangle.loadWidgets();
startDrawing();


