/**
 *
 * Lato Clock
 *
 * The entire clock code is contained within the block below this
 * supports 'fast load'
 *
 * To add support for clock_info_supprt we add the code marked at [1] and [2]
 *
 */

Graphics.prototype.setFontLato = function(scale) {
  // Actual height 50 (54 - 5)
  this.setFontCustom(
    E.toString(require('heatshrink').decompress(atob('ADkD8AHFg/wA4sP/AHVD44HHgPALD0OA40+F43+H4wHGn/8A4v/L4sH/5PFj//CxkD/6eFCw9/GooWHh//wAWLgP/TgoWHn5rFCw41BMYqCHaRDKGgzLYKAJgFv//LIhQBAAI7DWgIABU4adBAAJTDn4HCVAaOCQQhvDAYQuBDYaxBgJEDh4HBgYzDPgUDIYYECA5DUDgIHBg4HEEgIHfF44/EA45HDL4xvHP46PHT5CvHX47PDGYcDb4zvHf5AA/AA9wA4yoDZYq/DXAgHDXYQHEXYQHEj4HHXYQHDn6UCA4d/e4sAXYYHCd4gHCXwbADA86DFA/4HGAGA3Db40HA4UDe40Hc4YHCh7nDA4UfA4X/A4U/A4b/Cv7vGX4UB/A+CZ4YaCgf9A4sH+IHCHwfjA4JWDj/DA4s/wYHFv4kCA4f+A4pKBA4sD/AHCG4R9BA4YCBj/gA4s/4AECN4R5BA4f/gf/Mgn///+A4wZBA4d//6JBA4c/VATHEVASUEEwIHEAAbnDAGbyCAAg+DgKwDA4S4DLQSlCSYQHCn4HDFAV/bAX/4ADFCYgbCh4zHZ4SlBR4iSEA46XCe4QHCDgJWCngHOnwHGvwHRG4iFBI4ppBA4f4OIRnCN4MD9+AO4f///v8CHCDoP/54CBS4f/44CBU4f/wYHBX4f/EQLHDh6gB/6jDZQaTDAEUcA40/A4xODYoYHGgYHGh4HGNIIuG74uGz4uGj4uF/gHFh/4A4sf+AHFn/AA4q0BA4kBVgIHEFwIHFFwIHFj7jBA4guBA4rjCA4YuCA4guCA4r0BAATgBA75SEa4wHvAEEBA40DUYIAEg4HDgZ0Bh67BXAQHCZYJMBA4UHA4KPCA4SXEAgQHL4IEBgIHC/AMCgP4CQUDFgIHoIQY3DA4wCEDggHFO4YHB/iHDCQX+gE/S4IHCOIP/U4IHCv6CBA4k/A4K1CEQKpBEIIHDh//HILSDTQK+CAAd/f64Amn4GFgLxCAAZfBSIIADN4heDP4YeDR4Z5CEwN/U4IABg4NBj6ADEwLHDIoQtBVgQuCHoIHDFwIHBe4QLB/14A4kH/i1BeQQuB/AHFn/wA4pLBA4guBwAHELoMAA4o9BA4Q3BgYFBJ4gCCA4pqBvxvDf4T2Bh4HCIIc/R4MCKISfBS4aQCU4gHDX4ioBY4paBNwQAD/6uDAAUOf4wAjO4QHNdQYHYmAHGW4gUEA4kPA4z7BA4v/A4qYBY4QHCh63CA4c/V4QHDV4Y6DV4YHCDwYHDDwYHDv7ODA4MBZwgHBcwL1DA4MfdogHBDwgHB+LtFgf3DwhMCDwgHCDwhcFA4geEA4IeFA4IeFd5AArj77EsCgB/gGCg5QBOQkf/6oB/77D//DA4JrCv//44HB4DkC//n/E/MgIcCRIMPA4X8RIUHegQCBFoL8DA4cBA4QaBv4HGvwHBTgMHHQM+HgIHhF44HFJ5RfGN45/Bz6NBP4SPHT4XnT4ivHX47PHgCQCb4bvIAHxdBMgRfD/58CKgf/WgIADP4JlFR4J1ET4QHCiACBQwQEBuC/CDIIHBX4QtBn+Aa4sfZ4bvCh+Ah4HGUAUHA4d/AgIHEa4QHDwJyCA4eDKIQHDx5pCA4bPDG4c/RIRPCjwuCA4aJBUwZnCRAcBP4SgE/+D/7+ET4ImDA4jIEX4KvFh7HGgbXGgF+f6oAggZeBSgShEb4RYCagQHGh5iDA5QXEE443HADoA=='))),
    46,
    atob("DhglJSUlJSUlJSUlEA=="),
    64+(scale<<8)+(1<<16)
  );
  return this;
}


Graphics.prototype.setFontLatoSmall = function(scale) {
  // Actual height 25 (24 - 0)
  this.setFontCustom(
    E.toString(require('heatshrink').decompress(atob('[encoded data, 7880 characters]'))),
    32,
    atob("BQkKDw8UEgYICAoPBQkFCQ8PDw8PDw8PDw8GBg8PDwoVERAREw8OEhMICxENFxMUDxQQDQ8SERkQEBAICQgPCggNDgwODQgNDgYGDQYVDg4ODgoLCQ4NEw0NDAgICA8AAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAADAAFCQ8PDw8IDQgUCQwPABQICg8ICAgOEQcICAoMEhISChERERERERcRDw8PDwgICAgUExQUFBQUDxQSEhISEA8PDQ0NDQ0NFAwNDQ0NBgYGBg4ODg4ODg4PDg4ODg4NDg0="),
    25+(scale<<8)+(1<<16)
  );
  return this;
}



{
  // must be inside our own scope here so that when we are unloaded everything disappears
  // we also define functions using 'let fn = function() {..}' for the same reason. function decls are global
  
  let draw = function() {
    var date = new Date();
    var timeStr = require("locale").time(date,1);
    var h = g.getHeight();
    var w = g.getWidth();
    
    g.reset();
    g.setColor(g.theme.bg);
    g.fillRect(Bangle.appRect);

    //g.setFont('Vector', w/3);
    g.setFontLato();
    g.setFontAlign(0, 0);
    g.setColor(g.theme.fg);
    g.drawString(timeStr, w/2, h/2);
    clockInfoMenu.redraw();   // clock_info_support

    // schedule a draw for the next minute
    if (drawTimeout) clearTimeout(drawTimeout);
    drawTimeout = setTimeout(function() {
      drawTimeout = undefined;
      draw();
    }, 60000 - (Date.now() % 60000));
  };

  /**
   * clock_info_support
   * this is the callback function that get invoked by clockInfoMenu.redraw();
   * 
   * We will display the image and text on the same line and centre the combined
   * length of the image+text
   *
   *
   */
  let clockInfoDraw = (itm, info, options) => {
    //g.reset().setFont('Vector',24).setBgColor(options.bg).setColor(options.fg);
    g.reset().setFontLatoSmall();
    g.setBgColor(options.bg).setColor(options.fg);
    
    //use info.text.toString(), steps does not have length defined
    var text_w = g.stringWidth(info.text.toString());
    // gap between image and text
    var gap = 10;
    // width of the image and text combined
    var w = gap + (info.img ? 24 :0) + text_w;
    // different fg color if we tapped on the menu
    if (options.focus) g.setColor(options.hl);

    // clear the whole info line, allow additional 2 pixels in case LatoFont overflows area
    g.clearRect(0, options.y -2, g.getWidth(), options.y+ 23 + 2);
    
    // draw the image if we have one
    if (info.img) {
      // image start
      var x = (g.getWidth() / 2) - (w/2);
      g.drawImage(info.img, x, options.y);
      // draw the text to the side of the image (left/centre alignment)
      g.setFontAlign(-1,0).drawString(info.text, x + 23 + gap, options.y+12);
    } else {
      // text only option, not tested yet
      g.setFontAlign(0,0).drawString(info.text, g.getWidth() / 2, options.y+12);
    }

  };

  // clock_info_support
  // retrieve all the clock_info modules that are installed
  let clockInfoItems = require("clock_info").load();

  // clock_info_support
  // setup the way we wish to interact with the menu
  // the hl property defines the color the of the info when the menu is selected after tapping on it
  let clockInfoMenu = require("clock_info").addInteractive(clockInfoItems, { x:64, y:132, w:50, h:40, draw : clockInfoDraw, bg : g.theme.bg, fg : g.theme.fg, hl : "#0ff"} );
  
  // timeout used to update every minute
  var drawTimeout;
  g.clear();

  // Show launcher when middle button pressed, add updown button handlers
  Bangle.setUI({
    mode : "clock",
    remove : function() {
      if (drawTimeout) clearTimeout(drawTimeout);
      drawTimeout = undefined;
      // remove info menu
      clockInfoMenu.remove();
      delete clockInfoMenu;
      // delete the custom fonts
      delete Graphics.prototype.setFontLato;
      delete Graphics.prototype.setFontLatoSmall;
    }
  });

  // Load widgets
  Bangle.loadWidgets();
  draw();
  setTimeout(Bangle.drawWidgets,0);
}  // end of clock
