Graphics.prototype.setFontDigitalNumbersRegular = function(scale) {
  // Actual height 32 (31 - 0)
  this.setFontCustom(atob("[encoded data, 8352 characters]"), 32, atob("GhoaGhoaGhoaGhoaAxoDGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGhoaGho="), 32+(scale<<8)+(1<<16));
  return this;
};
Graphics.prototype.setFontDigitalNumbersSmall = function(scale) {
  // Actual height 16 (15 - 0)
  this.setFontCustom(atob("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf+b/5v/2AAAAAAAAAAAAAAAAAAAAAAAAAADAAMAAwAAAAAAAAAAAAAAAAmAGYAb8//9/4AZgBmAG+H//f/AOYAZABEAAAAAAf4BfQGBA//9gQP//YF5AfwA/AAAAAAAAAAAAAAweDHwB8AfAHwB8YABgAAAAAAAAAAB/f77/wMHAw+Dn8HZwPAAcADwAfAAEAAAAAAAAAAAAAAAA8ADwAPAAAAAAAAAAAAAAAAAAAAAAAAAA/3++P8ABwAGAAQAAAAAAAAAAAAAAAAAAAACAAcABwAG+f39/AAAAAAAAAAAAAAAAAAABAAfAB8APwAfAB8ABAAAAAAAAAAAAAAAAAAEAAwAHwA/AB8ADAAEAAAAAAAAAAD8APAAAAAAAAAEAAwADAAMAAwADAAEAAAAAAAAAAAYABgAAAAAAAAAeAHwB8AfAHwB8AAAAAAAAAAAAAAAAAP9/vn/AAcABwAHAAf49/n9/fwAAAAAAAAAAAAAAAAAAAAAAAAAAfHj8/P7+AAAAAAAAAACAf4D/wYHBgcGBwYH/gf6BfwAAAAAAAAAAAAAAgIHBgcGBwYHBgf29/v9/fwAAAAAAAAAA/gB9AAMAAwADAAMAf3j9/P7+AAAAAAAAAAD/AL6BwYHBgcGBwYHBvYD/AH8AAAAAAAAAAP9/vv/BgcGBwYHBgcG9gP8AfwAAAAAAAAAAAACAAMAAwADAAMAA/AC+fn9/AAAAAAAAAAD/f77/wYHBgcGBwYH/vf7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JEJEJEJsO4AAAAH4OYIEIEIEIEIEIEIEIEIEAAP8IEIEIEIEIEIEIEIEMMH4AAAAH4FYJEJEJEJEJEJEJEJEJEAAAAH8NAJAJAJAJAJAJAJAJAIAAAH4OYIEIEIEIEJEJEJEBMB4AAAAP8BABABABABABABABAP8P8AAP8AAAAAwAYAEAEAEAEAEAEAMP4PwAAP8BABABABABABABABgO8OcAAAAP4AYAEAEAEAEAEAEAEAEAEAAP8IAMAHABwAcAEAcBwHAMAIAP8AAAAP8IAMAGADABgAwAYAMAEP8AAAAH4OcIEIEIEIEIEIEIEIEIEMMH4BgAAP8IgIgIgIgIgIgIgIgIgPgHAAAH4OcIEIEIEIEIEIEIEIEIEMMH8BkAAP8IgIgIgIgIgIgIgIwI4PsHEAACAPEJEJEJEJEJEJEJEJEJ8A4AAIAIAIAIAIAP8IAIAIAIAIAAAP4AcAEAEAEAEAEAEAMP4PwAAIAMAGADgAwAYAMAcAwBgDAOAIAIAAAP4AcAEAEAEAEAEAMP4P4AMAEAEAEAEAEAMP4AAAAOcGwBABABABABABADgO8McAAPADgAgAgAgA8AgAgBgPAOAAAIcIUI0IkIkJkJEJELEKEOEAAAAf+QCQCAAQAYAGABgAcAGACAAQCf+f+AA"), 32, atob("BQUECQwODgMGBgYIAwgDBw4FDg4NDg4NDg4DBAgIBwwNDQ0MDQ0MDQwDDA0MDw0PDQ8ODAwNDhQMDA0FBwU="), 12+(scale<<8)+(1<<16));
  return this;
};

var nasa = require("heatshrink").decompress(atob("jEUwcCpMkyQBBqQFCAQOJAgQIEAgclAYUipdkBoWlkVJiVZkofDiVIyVZkmypMmAwIIDy1JyVEBAmTsmQiQKB/1bvnPlP8yQIB3/Z/0P5t8BAWz9s/yfbnoIC6VLtua5ctBANAIgMsiRcCGoVJkYCBsmUBAVIOAYIBMQNBBAekOgUJBA0gQw6YJVQwA="));

var runner = require("heatshrink").decompress(atob("kEgwINKhwDCj4DKB4UH+ADBh/4mAOB/wDBjn/BgM5//gAYPxAYMz+HAgEDAwIfBAYYKBEYIDGjoPCnlwGQU4GQPwAYMP8ADBAAIDXAAo"));

var calendar = require("heatshrink").decompress(atob("j0ZwMAv////7BQU4AYVgAQMBwADBgwKCjgDCDAIABBw0Yg0xx1gjHGnOAocIowODwkwwlnBwWcscY8wODAYQOB4EBBwIsBmOMBwlwAQMDLIX4A=="));

var heartImageData = require("heatshrink").decompress(atob("//z/4CD4EPAQgOBAQXggEfAQXwgEDAQX4EIN4AQN8AQMMAQMPGAkDwAGEsAFEjAFEgwFEgIYkjgFEh4SEgZmBDwf8RAoSEv5zBAAU//wFDj6MCFQX/QYUAg6iBGAYFBGQUBAoIyDAoIyDv4FBGQU/AoIyCGALICGAQABGQIwCGQQwCGQQwCGQYFDGQIwCGQQwCGQQwDAAPPAogiD"));

var colorPaletteActive = new Uint16Array([g.toColor(1,0,0), g.toColor(1,1,1)]);
var colorPaletteInactiveLightBg = new Uint16Array([g.toColor(0,0,0), g.toColor(1,0,1)]);
var colorPaletteInactiveDarkBg = new Uint16Array([g.toColor(1,1,1), g.toColor(1,0,1)]);

var light = require("heatshrink").decompress(atob("iEQwIEB8EBAIPAgYBBwABBg/gAIILBgOAAIUAgQBCAAY"));

var planet = require("heatshrink").decompress(atob("5F9wZC/AH4A/AH4AugMkyVJkmAIv4AEI4JKCAQJG/AAUCJQ0kyBJ/JROSpAgVhIcEJUcSJRACBwAdQgIcJJVp7QWRKYjJRlJDZsEDJQCCkBKePBjjNbpQcRJUOSoAaJDJzjgJR7jJI54CCpBLdGCDGGghKSpMgJUkAgLjMhJHREogAbGYxWLVigCFyBKhpAMFPZECJS2SJTbXFaggACIQp7BiRKXOYwAVJRhZFyEEI6r9HJUoABBwWAI64CCkBKaQIhKKbYT0FASyWbEIdIB5UJkEJJTeAJTSGCJRgABI7ICCyBKagQhDCCACZcLZKuoBKahIhCkDxOJW0BEIYPKI7YCCpDhbEIeABpEEJT1JJTY8DyBYMATkgJbYhDJX4AGiRKKgJKgkhKbgQhCoALGhJ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GagxKFUhAATJSxAEBgtIE4jvFBYoAWEQoCQRJRXEXoxKcgBKUkAcGDCBKcFyJ8KRgxKnFyACBDhLjHVx4AWhJKPwAcKghKsgDEcWhxKegESFxdADhzjNJT7jLbpYAGJVguJDihKtADpKIkBJ/AAMCJX6YQpBF/AH4A/AH4Au"));

const APP_NAME = "spaceclock";
const HEADER_LINE_Y = 25;
const HEADER_TEXT_X = 5;
const HEADER_TEXT_Y = 7;
const MAIN_IMAGE_X = 13;
const MAIN_IMAGE_Y = 70;
const BATTERY_IMAGE_X = 112;
const BATTERY_IMAGE_Y = 4;
const BATTERY_CIRCLE_X = 120;
const BATTERY_CIRCLE_Y = 11;
const BATTERY_CIRCLE_RAD = 10;
const BATTERY_TEXT_X = 134;
const BATTERY_TEXT_Y = 5;
const TIME_X = 0;
const TIME_Y = 33;
const DATE_X = 12;
const DATE_Y = 125;
const WEEKDAY_X = 130;
const WEEKDAY_Y = 30;
const TEMPERATURE_X = 130;
const TEMPERATURE_Y = 50;
const HEART_IMAGE_X = 137;
const HEART_IMAGE_Y = 71;
const HEART_RATE_X = 127;
const HEART_RATE_Y = 99;
const FOOTER_LINE_Y = 150;
const FOOTER_TEXT_LEFT_X = 5;
const FOOTER_TEXT_RIGHT_X = 105;
const FOOTER_TEXT_Y = 160;
const FOOTER_IMAGE_X = 75;
const FOOTER_IMAGE_Y = 154;
const RUNNER_IMAGE_X = 90;
const RUNNER_IMAGE_Y = 117;
const STEPS_TEXT_X = 130;
const STEPS_TEXT_Y = 128;


const isDarkBg = g.getBgColor() ===  0;
const weekdays = ["SUN","MON","TUE","WED","THU","FRI","SAT"];

const drawHeader = () => {
  g.setFontArkitechLight(1);
  g.drawString("ASTEROID", HEADER_TEXT_X, HEADER_TEXT_Y);
  g.drawLine(0,HEADER_LINE_Y, g.getWidth(),HEADER_LINE_Y);
};

const drawMainImage = ()=>{
  g.drawImage(planet,MAIN_IMAGE_X, MAIN_IMAGE_Y,{scale:0.4});
};

const drawBattery = ()=>{
  const battery = Math.round(E.getBattery());
  g.drawImage(light,BATTERY_IMAGE_X, BATTERY_IMAGE_Y);
  g.drawCircle(BATTERY_CIRCLE_X,BATTERY_CIRCLE_Y,BATTERY_CIRCLE_RAD);
  g.setFontDigitalNumbersSmall(1);
  g.drawString(battery,BATTERY_TEXT_X,BATTERY_TEXT_Y);
};

const drawTime = ()=>{
  const date = new Date();
  const month = date.getMonth()+1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const weekdayInt = date.getDay();

  // Time
  g.setFontDigitalNumbersRegular(1);
  g.drawString( ("0" + hour).substr(-2), TIME_X, TIME_Y);
  g.drawString(":",TIME_X + 43,TIME_Y);
  g.drawString(("0" + minute).substr(-2),TIME_X + 60,TIME_Y);

  // Date
  g.setFontDigitalNumbersSmall(1);
  g.drawString(("0" + day).substr(-2) + "-" + ("0" + month).substr(-2), DATE_X, DATE_Y);

  // Weekday
  g.setFontDigitalNumbersSmall(1);
  g.drawString(weekdays[weekdayInt], WEEKDAY_X, WEEKDAY_Y);
};

const drawHeart = (isHRMOn) =>{
 var palette;

  if (isHRMOn){
    palette = colorPaletteActive;
  } else {
    palette = isDarkBg ? colorPaletteInactiveDarkBg : colorPaletteInactiveLightBg;
  }

  var heart = {
  width : 50, height : 43, bpp : 1,
  buffer : heartImageData,
  palette: palette,
  transparent:  1
  };

  g.drawImage(heart, HEART_IMAGE_X, HEART_IMAGE_Y,{scale:0.5});

};

const drawSteps = () => {
  var steps = Bangle.getHealthStatus("day").steps;
  const stepsRaw =  steps/1000;
  const decimal = stepsRaw >=10 ? 0 : 1;
  steps =stepsRaw.toFixed(decimal) + "K";
  console.log(steps);
  g.setFontDigitalNumbersSmall(1);
  g.drawString(steps, STEPS_TEXT_X, STEPS_TEXT_Y);
  g.drawImage(runner, RUNNER_IMAGE_X, RUNNER_IMAGE_Y,{scale:0.9});

};

const drawFooter = ()=>{
  g.drawLine(0, FOOTER_LINE_Y, g.getWidth(), FOOTER_LINE_Y);
  g.setFontArkitechLight(1);
  g.drawString("SPACE", FOOTER_TEXT_LEFT_X, FOOTER_TEXT_Y);
  g.drawString("RESIST", FOOTER_TEXT_RIGHT_X,FOOTER_TEXT_Y);
  g.drawImage(nasa, FOOTER_IMAGE_X, FOOTER_IMAGE_Y);
};

const drawTemp = () => {
  Bangle.getPressure().then((measure) => {
    if (measure){
      g.clearRect(TEMPERATURE_X, TEMPERATURE_Y - 2,TEMPERATURE_X + 40, TEMPERATURE_Y+18);
      const temp = Math.round(measure.temperature);
      g.setFontDigitalNumbersSmall(1);
      g.drawString(temp + "C", TEMPERATURE_X, TEMPERATURE_Y);
    }}).catch((reason) => {
    console.log("Error in getPressure(): " + reason);
  });
};

const drawHeartRate =(hrm, isOn)=>{
  const x = ((hrm < 100) || !hrm) ? HEART_RATE_X + 8 : HEART_RATE_X;
  const measure = hrm ? hrm : "--";
  g.clearRect(HEART_RATE_X, HEART_RATE_Y - 5, HEART_RATE_X + 42, HEART_RATE_Y + 18);
  g.setFontDigitalNumbersSmall(1);
  g.drawString(measure, x, HEART_RATE_Y);
};

var drawTimeout;
const queueDraw = () => {
  if (drawTimeout) clearTimeout(drawTimeout);
  drawTimeout = setTimeout(function() {
    drawTimeout = undefined;
    draw();
  }, 60000 - (Date.now() % 60000));
};

const clearIntervals = () => {
  if (drawTimeout) clearTimeout(drawTimeout);
  drawTimeout = undefined;
};

const draw = ()=>{
  queueDraw();

  g.clear(1);
  const whiteOrBlack = isDarkBg? 1 : 0;
  g.setColor(whiteOrBlack, whiteOrBlack, whiteOrBlack);

  drawHeader();
  drawMainImage();
  drawFooter();
  drawHeart(Bangle.isHRMOn());
  drawHeartRate(null, Bangle.isHRMOn());
  drawTime();
  drawBattery();
  drawSteps();
  drawTemp();
};

Bangle.on('HRM',(hrm)=>{
    drawHeartRate(hrm.bpm, Bangle.isHRMOn());
});

Bangle.on('touch',(button, xy)=>{
  // Toggle Heartrate
  if (xy.x > 127 && xy.x < 167 && xy.y >71 && xy.y < 119) {
    Bangle.setHRMPower(!Bangle.isHRMOn(),APP_NAME );
    console.log("Setting HRM to: " + Bangle.isHRMOn());
    drawHeart(Bangle.isHRMOn());
  }
});

Bangle.on("lcdPower", (on) => {
  if (on) {
    draw();
  } else {
    clearIntervals();
  }
});

Bangle.on("lock", (locked) => {
  clearIntervals();
  draw();
});

Bangle.setUI("clock");
Bangle.setBarometerPower(true, APP_NAME);
Bangle.setHRMPower(true,APP_NAME);

draw();
