Graphics.prototype.setFontLECO1976Regular42 = function (scale) {
    // Actual height 42 (41 - 0)
    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)
    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));
};


require("Font7x11Numeric7Seg").add(Graphics);


// the following 2 sections are used from waveclk to schedule minutely updates
// timeout used to update every minute
var drawTimeout;

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

function drawBackground() {
    g.setBgColor(0, 0, 0);
    g.setColor(1, 1, 1);
    g.clear();
}

function digit(num) {
    return String.fromCharCode(num + 48);
}

function timeString(h, m) {
    return digit(h / 10) + digit(h % 10) + ":" + digit(m / 10) + digit(m % 10);
}

function dayString(w) {
    return digit(w / 10) + digit(w % 10);
}

function getSteps() {
    if (WIDGETS.wpedom !== undefined) {
        return WIDGETS.wpedom.getSteps();
    }
    return '????';
}

/**
 * draws calender week view (-1,0,1) for given date 
 */
function drawCal() {
    d = /*this.date ? this.date : */ new Date();

    const DAY_NAME_FONT_SIZE = 10;
    const CAL_Y = g.getHeight() - 44; // Bangle.appRect.y+this.DATE_FONT_SIZE()+10+this.TIME_FONT_SIZE()+3;
    const CAL_AREA_H = 44; // g.getHeight()-CAL_Y+24; //+24: top widgtes only
    const CELL_W = g.getWidth() / 7; //cell width
    const CELL_H = (CAL_AREA_H - DAY_NAME_FONT_SIZE) / 3; //cell heigth
    const DAY_NUM_FONT_SIZE = Math.min(CELL_H + 3, 15); //size down, max 15

    const wdStrt = 1;

    const ABR_DAY = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    const IS_SUNDAY = [1, 0, 0, 0, 0, 1, 1]; // what days are sunday?
    const nrgb = ["#000", "#FFF", "#F00", "#0F0", "#00F", "#FF0"]; //fg, r ,g , b
    const suClr = 5; // sunday color fg
    const tdyMrkClr = 3; // today bk
    const tdyNumClr = 0; // today fg

    g.setFont("Vector", DAY_NAME_FONT_SIZE + 3);
    g.setColor(nrgb[1]);
    g.setFontAlign(-1, -1);
    // g.clearRect(Bangle.appRect.x, CAL_Y, Bangle.appRect.x2, CAL_Y+CAL_AREA_H);

    //draw grid & Headline
    const dNames = ABR_DAY.map((a) => a.length <= 2 ? a : a.substr(0, 2)); //force shrt 2
    for (var dNo = 0; dNo < dNames.length; dNo++) {
        const dIdx = wdStrt >= 0 ? ((wdStrt + dNo) % 7) : ((dNo + d.getDay() + 4) % 7);
        const dName = dNames[dIdx];
        // if(dNo>0) { g.drawLine(dNo*CELL_W, CAL_Y, dNo*CELL_W, CAL_Y+CAL_AREA_H-1);}


        var colTx = 0;
        var colBk = 1;
        if (IS_SUNDAY[dIdx]) {
            colBk = suClr;
        }

        g.setColor(nrgb[colBk]);
        g.fillRect(dNo * CELL_W, CAL_Y, dNo * CELL_W + CELL_W, CAL_Y + DAY_NAME_FONT_SIZE - 1);
        g.setColor(nrgb[colTx]);
        g.drawString(dName, dNo * CELL_W + (CELL_W - g.stringWidth(dName)) / 2 + 2, CAL_Y - 1);
        // g.setColor(nrgb[clTxt]);
    }
    g.setColor(nrgb[1]);
    var nextY = CAL_Y + DAY_NAME_FONT_SIZE;

    // horizontal lines
    // for(i=0; i<3; i++){ const y=nextY+i*CELL_H; g.drawLine(Bangle.appRect.x, y, Bangle.appRect.x2, y); }

    g.setFont("Vector", DAY_NUM_FONT_SIZE);

    g.setFont("7x11Numeric7Seg", 1);

    //write days
    const tdyDate = d.getDate();
    const days = wdStrt >= 0 ? 7 + ((7 + d.getDay() - wdStrt) % 7) : 10; //start day (week before=7 days + days in this week realtive to week start) or fixed 7+3 days
    var rD = new Date(d.getTime());
    rD.setDate(rD.getDate() - days);
    var rDate = rD.getDate();
    for (var y = 0; y < 3; y++) {
        for (var x = 0; x < dNames.length; x++) {
            if (rDate === tdyDate) { //today
                g.setColor(nrgb[tdyMrkClr]); //today marker color or fg color

                // rectangle
                g.fillRect(x * CELL_W, nextY + CELL_H - 1, x * CELL_W + CELL_W, nextY + CELL_H + CELL_H - 1);
                g.setColor(nrgb[tdyNumClr]); //today color or fg color

                // simulate "bold"
                g.drawString(rDate, 1 + x * CELL_W + ((CELL_W - g.stringWidth(rDate)) / 2) + 2, nextY + ((CELL_H - DAY_NUM_FONT_SIZE + 2) / 2) + (CELL_H * y));

            } else if (IS_SUNDAY[rD.getDay()]) { //sundays
                g.setColor(nrgb[suClr]);
            } else { //default
                g.setColor(nrgb[1]);
            }
            g.drawString(rDate, x * CELL_W + ((CELL_W - g.stringWidth(rDate)) / 2) + 2, nextY + ((CELL_H - DAY_NUM_FONT_SIZE + 2) / 2) + (CELL_H * y));
            rD.setDate(rDate + 1);
            rDate = rD.getDate();
        }
    }
}


function draw() {
    g.reset();
    drawBackground();
    var date = new Date();
    var h = date.getHours(),
      m = date.getMinutes();
    var d = date.getDate(),
      w = date.getDay(); // d=1..31; w=0..6

    g.setBgColor(0, 0, 0);
    g.setColor(1, 1, 1);


    // g.setFont('Vector', 30);
    // g.setFont("7x11Numeric7Seg", 5);
    g.setFontLECO1976Regular42();
    g.setFontAlign(0, -1);
    g.drawString(timeString(h, m), g.getWidth() / 2, 28);
    g.drawString(dayString(d), g.getWidth() * 3 / 4, 88);
    g.setColor(0, 1, 0);
    g.fillRect(0, 76, g.getWidth(), 80);
    g.reset();

    // Steps
    g.setFontLECO1976Regular22();
    g.setFontAlign(-1, -1);
    g.drawString(getSteps(), 8, 88);

    drawCal();


    // widget redraw
    Bangle.drawWidgets();
    queueDraw();
}


////////////////////////////////////////////////////
// Bangle.setBarometerPower(true);

Bangle.setUI("clock");

Bangle.loadWidgets();
draw();


// Bangle.on('pressure', function(e){
//   temperature = e.temperature;
//  draw();
// });

//the following section is also from waveclk
Bangle.on('lcdPower', on => {
    if (on) {
        draw(); // draw immediately, queue redraw
    } else { // stop draw timer
        if (drawTimeout) clearTimeout(drawTimeout);
        drawTimeout = undefined;
    }
});


Bangle.drawWidgets();
