// FONTS

/*
    Share Tech Mono: https://fonts.google.com/specimen/Share+Tech+Mono
    Converted with: https://www.espruino.com/Font+Converter
*/

Graphics.prototype.setFontShareTechMonoBig = function(scale) {
  // Actual height 56 (55 - 0)
  this.setFontCustom(
    atob('[encoded data, 4160 characters]'),
    46,
    32,
    60+(scale<<8)+(1<<16)
  );
  return this;
};

Graphics.prototype.setFontShareTechMono = function(scale) {
  // Actual height 38 (37 - 0)
  this.setFontCustom(
    atob('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHgAAAAAeAAAAAB4AAAAAHgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABwAAAAAfAAAAAP8AAAAD/gAAAB/4AAAAf+AAAAP/AAAAH/gAAAB/4AAAA/8AAAAP/AAAAH/gAAAD/wAAAA/8AAAAP+AAAAA/gAAAADwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB//4AAA///8AAP///8AA////wAHwAfvgAeAH4eABwA/A4AHAHwDgAcB+AOAB4PgB4AHj8AHgAf///+AA////wAB///+AAD///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHAAAOAA8AAA4ADwAADgAeAAAOAB4AAA4AH////gAf///+AB////4AH////gAAAAAOAAAAAA4AAAAADgAAAAAOAAAAAA4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB4AHAAAfgAcAAD+ABwAA/4AHAAH/gAcAA/OABwAP44AHgB+DgAfAfwOAA//8A4AD//gDgAH/4AOAAH+AA4AAAAADgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAcAAAOABwBwA4AHAHADgAcAcAOABwBwA4AHAHADgAeA+AOAB8H4B4AD////gAP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    22,
    40+(scale<<8)+(1<<16)
  );
  return this;
};

Graphics.prototype.setFontShareTechMonoSmall = function(scale) {
  // Actual height 23 (22 - 0)
  this.setFontCustom(
    atob('[encoded data, 4968 characters]'),
    32,
    atob("DQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0H"),
    24+(scale<<8)+(1<<16)
  );
  return this;
};

{

    // VARS

    let FONT_NAME = "ShareTechMono";
    let BIG_FONT_HEIGHT = 60;
    //let NORMAL_FONT_HEIGHT = 40;
    let SMALL_FONT_HEIGHT = 24;

    let timeDrawTimeout;
    let infoDrawTimeout;
    let lockState = Bangle.isLocked();
    let pressure;



    // LISTENERS

    Bangle.on('lock', function(isLocked) {
        lockState = isLocked;
        timeDraw();
        infoDraw();
    });



    // DRAW FUNCTIONS

    let timeDraw = function() {
        g.reset();
        g.clearRect(Bangle.appRect.x, Bangle.appRect.y, Bangle.appRect.x2, Bangle.appRect.y + BIG_FONT_HEIGHT);

        var date = new Date();
        var timeArray = [date.getHours().toString().padStart(2, "0"),
                         date.getMinutes().toString().padStart(2, "0")];
        if (!lockState) timeArray.push(date.getSeconds().toString().padStart(2, "0"));
        var timeString = timeArray.join(":");
        g.setFontAlign(0, 0).setColor(g.theme.fg).setFont(FONT_NAME + (lockState ? "Big" : ""));
        g.drawString(timeString, Bangle.appRect.x2/2, Bangle.appRect.y + BIG_FONT_HEIGHT/2);

        if (timeDrawTimeout) clearTimeout(timeDrawTimeout);
        timeDrawTimeout = setTimeout(function() {
            timeDrawTimeout = undefined;
            timeDraw();
        }, (lockState ? 10000 - (Date.now() % 10000) : 1000 - (Date.now() % 1000))); // if locked, every clock's 10s, otherwise every 1s
    };

    let infoDraw = function() {
        g.reset();

        var date = new Date();
        var dateString = [date.getFullYear().toString().padStart(4,"0"),
                          (date.getMonth()+1).toString().padStart(2,"0"),
                          date.getDate().toString().padStart(2,"0")].join("-");

        var tzOffset = -(date.getTimezoneOffset())/60;
        var tzOffsetString = (tzOffset >= 0 ? "+" + tzOffset : tzOffset);

        var batteryString = (Bangle.isCharging() ? "+" : "") + E.getBattery() + "%";

        var pressureString = (pressure ? pressure + "hPa" : "(hPa)");

        var powerString = (E.getPowerUsage().total / 1000) + "mA";

        var stepsString = Bangle.getHealthStatus("day").steps + "ST";

        var bluetoothStatus = NRF.getSecurityStatus();
        var bluetoothString = (bluetoothStatus.connected ? bluetoothStatus.connected_addr.split(" ")[0].substr(-5) : "N/C");

        var infoMatrix = [
                          [dateString + tzOffsetString  ],
                          [batteryString, pressureString],
                          [powerString                  ],
                          [stepsString, bluetoothString ]
                         ];

        g.clearRect(Bangle.appRect.x, Bangle.appRect.y + BIG_FONT_HEIGHT, Bangle.appRect.x2, Bangle.appRect.y2);
        g.setFontAlign(0, -1).setColor(g.theme.fg2).setFont(FONT_NAME+"Small");

        infoMatrix.forEach((lineArray, lineNumber) => {
            g.drawString(lineArray.join(" "), Bangle.appRect.x2/2, Bangle.appRect.y + BIG_FONT_HEIGHT + SMALL_FONT_HEIGHT*lineNumber);
        });

        Bangle.getPressure().then(baroValue => { pressure=Math.round(baroValue.pressure); });

        if (infoDrawTimeout) clearTimeout(infoDrawTimeout);
        infoDrawTimeout = setTimeout(function() {
            infoDrawTimeout = undefined;
            infoDraw();
        }, (lockState ? 60000 : 10000)); // if locked, a minute from now, otherwise in 10s
    };



    // DRAW CALLS

    g.clear();

    Bangle.setUI({
      mode: "clock",
      remove: function() {
        if (timeDrawTimeout) clearTimeout(timeDrawTimeout);
        timeDrawTimeout = undefined;
        if (infoDrawTimeout) clearTimeout(infoDrawTimeout);
        infoDrawTimeout = undefined;

        delete Graphics.prototype.setFontShareTechMono;
        delete Graphics.prototype.setFontShareTechMonoBig;
        delete Graphics.prototype.setFontShareTechMonoSmall;
    }});

    Bangle.loadWidgets();
    Bangle.drawWidgets();

    timeDraw();
    infoDraw();
}
