g.clear();

// Draw the Backgound, that will not change  
g.setColor(0.7, 0.7, 0.7).fillPoly([0, 0, 70, 0, 20, 120, 70, 240, 0, 240]);
g.setColor(0.7, 0.7, 0.7).fillPoly([240, 0, 170, 0, 220, 120, 170, 240, 240, 240]);
g.setColor(0, 1, 0).fillPoly([90, 0, 38, 120, 90, 240, 150, 240, 202, 120, 150, 0]);

var avatar = {
    width: 109, height: 140, bpp: 1,
    transparent: 0,
};

// Shadows for each Alien
const avatar_shadow = [
    require("heatshrink").decompress(atob("AH4A/ADcwBhcB4gNLh0QFBnABpf8BhcH8BDL/+ABpUPFBk/+QoM+BDLGxYaBGxcP/56Lv/+IZn4FDE/FBcD/6jLn//DRhsLj4oLLwIoLGoJsKV4JsLv4NBXxIaCKJQaCc5IaCGxRQB/6jJNYRfKZQIoLBgQoJDQZRJLwQoJZQIoLn/fFBUB/wbCUZEP85sLv70BepMD/BSCIZPDFBUD/goCNhE/8AMB355JbQP9FBEf+DaB94oI/+BFAM/PJH+X4Px/gaI8BsBx56HIIOD//8PRE//BsB84oHgJ2BIYQoHIYIqB/IoIv/4NgPPPQ6HB4P//1/PQ8fNgXzFBBsDz4oHIIIqBNgIoHNgn+FBJsBYQIoINgeANg/8bISVJ8YoBv6VIGYP//aVKDQPfNg4oB86VBNhAoBIYP3NhCTBIQJwBXxBCC54aHJ4KvB904FBKvBuJDHFAXn/eOFBHxKQPcWA6+Bx/5/1nWA8HGgPP+BDInnn/l/gZDHgPnn/j/EeDQ8P3P+j/BQ48Anvv8H+DREA9//g/4DRED3/4n5CHAAMc//BZRAABkf8h4nIKQXgv4MJgF+gf4BhMH+EfIZLaBgP8BhImBj5DKNQP/IZX8G4JDK8E/co5eDIZnwh5eKj+AnxDLgJDKgfgh5DKj8AvgoK/EBIZUH4EODRUegFwBpXggZDKg8AjgaKnAcBBhJPBDgIAJJ4IcBABNwNhZPBNhZPBvBsLRIJsLn5sM/xsM/gNJh4qBBpX4dIICBDRChBv6kJ/44B/7NIh4zBgf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/3+MAwVn/f4Ogdw/g1DCgMPw6NDwPgPQlg8EgAoUEgODPQkGaQsMAwoA/AH4A/AH4A/AH4A/ABg=")),
    require("heatshrink").decompress(atob("AH4A/AH4A/AH4A/AH4AIwAMLg/ABpcfBpn/FJcD/4aLh/8Bpc/+AMKgP/IhcH/woM/BfMFDRRNUZhDLh5DLBgJDLBgIoIgg0B//zZhEQg////YExFDBgLLJcgIAB8BPKKBc/v4aKLoIaLGwSvKE4P/NZA1BFBaFBBgM4BhR4BKI4mCGgMDZYwZCAAJ9BQhAAC+E8BgkBBgiHBNoonEDQI2FDQ8HDRX5wEMVgwAB869Hh4HC/5VBUQ1+AQMH+EfUQ0D/CJCwE/Qw0PMoMBUIKIBaowVBh/AgYPBfww+BBQJJDPQg+Bj4dBn4aGIgI1CJIYAEg/gEoRRHGIOADQRDHgE8g4XBj6HGAANwvgaBa4oAD44aBv4MIgT9Bn5rGMAfAh5dHAAUcKgIMJgAlBFQIAIgxUCBpIKBgKGHIYSZCDRM4AQMeBhAlDvxeKbwZeJNoJ7Ij4DCbJDzCAAIoIeYTMKZAb5BPI4/DvBDISRIACIYaiJeJIAChwMLAH4A/AH4A/AH4A/AH4AoA"))
];

var blinkFlag = 0;
function blink() {
    if (blinkFlag) {
        g.clear();
        blinkFlag ^= 1;
    }
    else {
        g.setColor(0, 1, 0).fillPoly([0, 0, 0, 240, 240, 240, 240, 0]);
        blinkFlag ^= 1;
    }
}

function morf() {
    a = setInterval(blink, 10);
    setTimeout(() => { clearTimeout(a); }, 500);
    setTimeout(() => { load(); }, 1500);
}

var i = 0;

function draw(i) {
    // fill the are bw > and < with green
    g.setColor(0, 1, 0).fillPoly([90, 0, 38, 120, 90, 240, 150, 240, 202, 120, 150, 0]);
    // select the alien's shadow
    avatar.buffer = avatar_shadow[i];
    // Draw it
    g.setColor(0, 0, 0).drawImage(avatar, 70, 50);

}

function next() {
    i = (i + 1) % 10;
    draw(i);
}

function prev() {
    i = (i + 9) % 10;
    draw((i + 10) % 10);
}

function init() {
    draw(0);

    Bangle.on('touch', morf);
    Bangle.on('swipe', dir => {
        if (dir == 1) prev();
        else next();
    });

    setWatch(next, BTN1, { repeat: true });
    setWatch(morf, BTN2);
    setWatch(prev, BTN3, { repeat: true });

}

init();
