Graphics.prototype.setFontLECO1976Regular42 = function(scale) {
  // Actual height 42 (41 - 0)
  return g.setFontCustom(atob("[encoded data, 4300 characters]"), 46, atob("ERkmHyYmJiYmJCYmEQ=="), 60+(scale<<8)+(1<<16));
};

Graphics.prototype.setFontLECO1976Regular22 = function(scale) {
  // Actual height 22 (21 - 0)
  return g.setFontCustom(atob("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAP/nA/+cD/5wP/nAAAAAAAAPwAA/gAD+AAPwAAAAAD+AAP4AA/gAAAAAAAAAAAAAcOAP//A//8D//wP//AHDgAcOAP//A//8D//wP//AHDgAAAAAAAAH/jgf+OB/44H/jj8OP/w4//Dj/8OPxw/4HD/gcP+Bw/4AAAAAAAP+AA/8AD/wQOHHA4c8D//wP/8A//gAD4AAfAAH/8A//wP//A84cDjhwIP/AA/8AB/wAAAAAAAD//wP//A//8D//wOHHA4ccDhxwOHHA4f8Dh/wOH/A4f8ABwAAAAAAAAD8AAP4AA/gAD8AAAAAAAAAAAEAAD+AB//A///v/D//gB/wABwAAAAAADgAA/wAf/4P8///wf/4AP8AAOAAAAAAAAAyAAHcAAPwAD/gAP/AA/8AA/AAH8AAMwAAAAAAAAAAAAADgAAOAAA4AAf8AD/wAP/AA/8AAOAAA4AADgAAAAAAAAAAD8AAfwAB/AAD8AAAAAAAADgAAOAAA4AADgAAOAAA4AADgAAAAAAAAAADgAAOAAA4AADgAAAAAAAAABwAB/AA/8A//gP/gA/wADwAAIAAAAAAD//wP//A//8D//wOAHA4AcDgBwOAHA//8D//wP//A//8AAAAAAAA4AcDgBwOAHA//8D//wP//A//8AABwAAHAAAcAAAAAAAA+f8D5/wPn/A+f8DhxwOHHA4ccDhxwP/HA/8cD/xwP/HAAAAAAAAOAHA4AcDhxwOHHA4ccDhxwOHHA4ccD//wP//A//8D//wAAAAAAAD/wAP/AA/8AD/wAAHAAAcAABwAAHAA//8D//wP//A//8AAAAAAAA/98D/3wP/fA/98DhxwOHHA4ccDhxwOH/A4f8Dh/wOH/AAAAAAAAP//A//8D//wP//A4ccDhxwOHHA4ccDh/wOH/A4f8Dh/wAAAAAAAD4AAPgAA+AADgAAOAAA4AADgAAP//A//8D//wP//AAAAAAAAP//A//8D//wP//A4ccDhxwOHHA4ccD//wP//A//8D//wAAAAAAAD/xwP/HA/8cD/xwOHHA4ccDhxwOHHA//8D//wP//A//8AAAAAAAAOA4A4DgDgOAOA4AAAAAAAAOA/A4H8DgfwOA/AAAAAAAAB4AAPwAA/AAD8AAf4ABzgAPPAA8cAHh4AAAAAAAAAAAAHHAAccABxwAHHAAccABxwAHHAAccABxwAHHAAAAAAAAAOHAA4cADzwAPPAAf4AB/gAD8AAPwAAeAAB4AAAAAAAAA+AAD4AAPgAA+ecDh9wOH3A4fcDhwAP/AA/8AD/wAP/AAAAAAAAAP//4///j//+P//44ADjn/OOf845/zjnHOP8c4//zj//OP/84AAAAAAAP//A//8D//wP//A4cADhwAOHAA4cAD//wP//A//8D//wAAAAAAAD//wP//A//8D//wOHHA4ccDhxwOHHA//8D//wP9/A/j8AAAAAAAA//8D//wP//A//8DgBwOAHA4AcDgBwOAHA4AcDgBwOAHAAAAAAAAP//A//8D//wP//A4AcDgBwOAHA8A8D//wH/+AP/wAf+AAAAAAAAD//wP//A//8D//wOHHA4ccDhxwOHHA4ccDhxwOAHA4AcAAAAAAAA//8D//wP//A//8DhwAOHAA4cADhwAOHAA4cADgAAOAAAAAAD//wP//A//8D//wOAHA4ccDhxwOHHA4f8Dh/wOH/A4f8AAAAAAAA//8D//wP//A//8ABwAAHAAAcAABwAP//A//8D//wP//AAAAAAAAP//A//8D//wP//AAAAAAAAOAHA4AcDgBwOAHA4AcDgBwOAHA//8D//wP//A//8AAAAAAAA//8D//wP//A//8AHwAA/AAP8AB/wAPn/A8f8DB/wIH/AAAAAAAAP//A//8D//wP//AAAcAABwAAHAAAcAABwAAHAAAAAAAAP//A//8D//wP//Af8AAP+AAH/AAD8AAHwAD/AB/wAf8AP+AA//8D//wP//AAAAAAAAP//A//8D//wP//AfwAAfwAAfwAAfwAAfwP//A//8D//wAAAAAAAAAAAP//A//8D//wP//A4AcDgBwOAHA4AcD//wP//A//8D//wAAAAAAAD//wP//A//8D//wOHAA4cADhwAOHAA/8AD/wAP/AA/8AAAAAP//A//8D//wP//A4AcDgBwOAHA4AcD//+P//4///j//+AAA4AADgAAAP//A//8D//wP//A4eADh+AOH8A4f4D/3wP/HA/8MD/wQAAAAAAAD/xwP/HA/8cD/xwOHHA4ccDhxwOHHA4f8Dh/wOH/A4f8AAAAAAAA4AADgAAOAAA//8D//wP//A//8DgAAOAAA4AADgAAAAAA//8D//wP//A//8AABwAAHAAAcAABwP//A//8D//wP//AAAADAAAPgAA/wAD/4AB/8AA/8AAfwAB/AA/8Af+AP/AA/wAD4AAMAAA4AAD+AAP/gA//8AH/wAB/AAf8Af/wP/4A/4AD/gAP/4AH/8AB/wAB/AB/8D//wP/gA/gADgAAIABA4AcDwDwPw/Afn4Af+AA/wAD/AA//AH5+A/D8DwDwOAHAgAEAAAAP/AA/8AD/wAP/AAAf8AB/wAH/AAf8D/wAP/AA/8AD/wAAAAAAAADh/wOH/A4f8Dh/wOHHA4ccDhxwOHHA/8cD/xwP/HA/8cAAAAAAAAf//9///3///f//9wAA3AADcAAMAAAOAAA/gAD/wAH/8AB/8AA/wAAPAAAEAAAAHAADcAANwAB3///f//9///wAA"), 32, atob("BwYLDg4UDwYJCQwMBgkGCQ4MDg4ODg4NDg4GBgwMDA4PDg4ODg4NDg4GDQ4MEg8ODQ8ODgwODhQODg4ICQg="), 22+(scale<<8)+(1<<16));
};

Graphics.prototype.setFontLECO1976Regular14 = function() {
  // Actual height 14 (13 - 0)
  return this.setFontCustom(
    atob('AAAAAAAAAAAD+w/sAAAAA8APAAAA8APAAAAMwP/D/wMwDMD/w/8DMAAAAAD8w/M8z/M/zPM/DPwAAAAPwD8QzcP/D/AHgD/D/wzMI/APwAAAAD/w/8MzDMwzMM/DPwDAAADwA8AAAAAAD8H/74f4BwA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ICQkECAkHDAkJCAkJCQgJCQ0JCQkFBgU="),
    14|65536
  );
}

{
const SETTINGS_FILE = "pebblepp.json";
let settings = Object.assign({'theme':'System', 'showdate':true, 'clkinfoborder': false}, require("Storage").readJSON(SETTINGS_FILE,1)||{});
let background = require("clockbg");
let theme;
let drawTimeout;

const h = g.getHeight();
const w = g.getWidth();
//const ha = 2*h/5 - 4;
const h2 = Math.round(3*h/5) - 10;
const h3 = Math.round(7*h/8);

let draw = function() {
  let locale = require("locale");
  let date = new Date();
  let time = locale.time(date, 1);

  g.reset();
  g.setBgColor(theme.bg).clearRect(0, h2, w, h3); // clear area where clock is
  if (settings.showdate) {
    g.setColor(theme.fg).fillRect(w / 2 - 30, h3 + 5, w / 2 + 30, h); // refresh date background
    g.setFontLECO1976Regular22().setFontAlign(0, -1);
    g.setColor(theme.bg).drawString(date.getDate() + "." + (date.getMonth() + 1), w / 2, h3 + 5);
  }
  g.setFontLECO1976Regular42().setFontAlign(0, -1);
  g.setColor(theme.fg);
  g.drawString(time, w/2, h2 + 8);

  // queue next draw
  if (drawTimeout) clearTimeout(drawTimeout);
  drawTimeout = setTimeout(function() {
    drawTimeout = undefined;
    draw();
  }, 60000 - (Date.now() % 60000));
};

let loadThemeColors = function() {
  theme = {fg: g.theme.fg, bg: g.theme.bg };
  if (settings.theme === "Dark") {
    theme.fg = g.toColor(1,1,1);
    theme.bg = g.toColor(0,0,0);
  } else if (settings.theme === "Light") {
    theme.fg = g.toColor(0,0,0);
    theme.bg = g.toColor(1,1,1);
  }
};
loadThemeColors();

// Load the clock infos
let clockInfoW = 0|(w/2);
let clockInfoH = 0|(h/2);
let clockInfoG = Graphics.createArrayBuffer(26, 26, 2, {msb:true});
clockInfoG.transparent = 3;
clockInfoG.palette = new Uint16Array([g.theme.bg, g.theme.fg, g.toColor("#888"), g.toColor("#888")]);
let clockInfoItems = require("clock_info").load();
let clockInfoDraw = (itm, info, options) => {
  // itm: the item containing name/hasRange/etc
  // info: data returned from itm.get() containing text/img/etc
  // options: options passed into addInteractive
  // Clear the background - if focussed, add a border
  g.reset().setBgColor(theme.bg).setColor(theme.fg);
  var y,b = 0; // border
  if (options.focus) { // white border
    b = 4;
    g.clearRect(options.x, options.y, options.x+options.w-1, options.y+options.h-1);
  }
  background.fillRect(options.x+b, options.y+b, options.x+options.w-1-b, options.y+options.h-1-b);
  // we're drawing center-aligned here
  var midx = options.x+options.w/2;
  if (info.img) { // draw the image
    // TODO: we could replace certain images with our own ones here...
    y = options.y+8;
    if (settings.clkinfoborder)
      require("clock_info").drawBorderedImage(info.img,midx-24,y,{scale:2});
    else
      require("clock_info").drawFilledImage(info.img,midx-24,y,{scale:2});
  }
  g.setFontLECO1976Regular22().setFontAlign(0, 0);
  var txt = info.text.toString().toUpperCase();
  if (g.stringWidth(txt) > options.w) // if too big, smaller font
    g.setFontLECO1976Regular14();
  if (g.stringWidth(txt) > options.w) {// if still too big, split to 2 lines
    var l = g.wrapString(txt, options.w);
    txt = l.slice(0,2).join("\n") + (l.length>2)?"...":"";
  }
  y = options.y+options.h-12;
  if (settings.clkinfoborder) {
    g.setColor(theme.bg)
    g.drawString(txt, midx-2, y).drawString(txt, midx+2, y).drawString(txt, midx, y-2).drawString(txt, midx, y+2);
    g.setColor(theme.fg);
  }
  g.drawString(txt, midx, y); // draw the text
};

let clockInfoMenuA = require("clock_info").addInteractive(clockInfoItems, {
  app:"pebblepp",
  x : 0, y: 0, w: clockInfoW, h:clockInfoH,
  draw : clockInfoDraw
});
let clockInfoMenuB = require("clock_info").addInteractive(clockInfoItems, {
  app:"pebblepp",
  x : w/2, y: 0, w: clockInfoW, h:clockInfoH,
  draw : clockInfoDraw
});

// Show launcher when middle button pressed
Bangle.setUI({
  mode : "clock",
  redraw : draw,
  remove : function() {
    // Called to unload all of the clock app
    if (drawTimeout) clearTimeout(drawTimeout);
    drawTimeout = undefined;
    clockInfoMenuA.remove();
    clockInfoMenuB.remove();
    delete Graphics.prototype.setFontLECO1976Regular22;
    delete Graphics.prototype.setFontLECO1976Regular42;
    delete Graphics.prototype.setFontLECO1976Regular14;
    require("widget_utils").show(); // re-show widgets
  }});

Bangle.loadWidgets();
require("widget_utils").swipeOn(); // hide widgets, make them visible with a swipe
background.fillRect(Bangle.appRect); // start off with completely clear background
// background contrast bar
g.setColor(theme.fg).fillRect(0, h2 - 6, w, h3 + 6);

draw();
}