require("Font7x11Numeric7Seg").add(Graphics);
require("Font5x9Numeric7Seg").add(Graphics);
require("Font8x12").add(Graphics);
require("FontDylex7x13").add(Graphics);
const X = 98, Y = 46;
var wizible = 0;

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

function draw() {
  var d = new Date();
  var h = d.getHours() % 12 || 12, m = d.getMinutes(), yyyy = d.getFullYear(), mm = d.getMonth(), dd = d.getDate();
  var time = (""+h).substr(-2) + ":" + ("0"+m).substr(-2);
  g.reset();  // Reset the state of the graphics library
  g.clear();
  g.drawImage(getImg()); //load bg image
  //TIME
  g.setFont("7x11Numeric7Seg",2);
  g.setFontAlign(1,1);
  g.setColor(0,0,1);
  g.drawString(time, 97, 53, false /*clear background*/);
  g.setColor(0,0,0);
  g.drawString(time, 96, 52, false /*clear background*/);
  //SECONDS
  g.setFont("7x11Numeric7Seg",1);
  //g.setFont("5x9Numeric7Seg");
  g.setFontAlign(-1,1); // align right bottom
  g.setColor(0,0,1);
  g.drawString(("0"+d.getSeconds()).substr(-2), 100, 42, 0);
  g.setColor(0,0,0);
  g.drawString(("0"+d.getSeconds()).substr(-2), 99, 41, 0);
  //DATE
  g.setFont("5x9Numeric7Seg",1);
  g.setFontAlign(1,1);
  g.setColor(0,0,1);
  g.drawString(yyyy+" "+("0"+mm)+" "+dd, 100, 65, 0);
  g.setColor(0,0,0);
  g.drawString(yyyy+" "+("0"+mm)+" "+dd, 99, 64, 0);
  //BATTERY
  g.setColor(0,0,1);
  g.drawString(E.getBattery(), 137, 53, 0);
  g.setColor(0,0,0);
  g.drawString(E.getBattery(), 136, 52, 0);
  //STEPS
  g.setColor(0,0,1);
  g.drawString(Bangle.getHealthStatus("day").steps, 137, 65, 0);
  g.setColor(0,0,0);
  g.drawString(Bangle.getHealthStatus("day").steps, 136, 64, 0);
  //WEEK DAY
  g.setFont("8x12");
  g.setColor(0,0,1);
  if (d.getDay()==0) {
   g.drawString("SU", 137, 43, 0);
    g.setColor(0,0,0);
    g.drawString("SU", 136, 42, 0); 
  } else if (d.getDay()==1) {
    g.drawString("MO", 137, 43, 0);
    g.setColor(0,0,0);
    g.drawString("MO", 136, 42, 0);
  } else if (d.getDay()==2) {
    g.drawString("TU", 137, 43, 0);
    g.setColor(0,0,0);
    g.drawString("TU", 136, 42, 0);
  } else if (d.getDay()==3) {
    g.drawString("WE", 137, 43, 0);
    g.setColor(0,0,0);
    g.drawString("WE", 136, 42, 0);
  } else if (d.getDay()==4) {
    g.setFont("Dylex7x13");
    g.drawString("TH", 137, 43, 0);
    g.setColor(0,0,0);
    g.drawString("TH", 136, 42, 0);
  } else if (d.getDay()==5) {
    g.drawString("FR", 137, 43, 0);
    g.setColor(0,0,0);
    g.drawString("FR", 136, 42, 0);
  } else {
    g.drawString("SA", 137, 43, 0);
    g.setColor(0,0,0);
    g.drawString("SA", 136, 42, 0);
  }
  if(wizible==1){
     Bangle.drawWidgets();
     }
}

// Clear the screen once, at startup
g.clear();
// draw immediately at first
draw();
var secondInterval = setInterval(draw, 1000);
// Stop updates when LCD is off, restart when on
Bangle.on('lcdPower',on=>{
  if (secondInterval) clearInterval(secondInterval);
  secondInterval = undefined;
  if (on) {
    secondInterval = setInterval(draw, 1000);
    draw(); // draw immediately
  }
});
// Show launcher when middle button pressed
Bangle.setUI("clock");

//Toggle Widgets
Bangle.loadWidgets();
Bangle.on('touch', function(button) {
  if(wizible==0){
    wizible=1;
  }
  else if(wizible==1){
    wizible=0;
  }
});
