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AP///8AH///4AD///wAA///AAAAAAAAAAAAAAAAAAAAAAAAAAAB8DwAAB8HwAAB8HwAAB8DwAAAAAAAAAAAAA"), 46, atob("CBIkESMjJCMjIyMjCA=="), 36+(1<<8)+(1<<16));
};

Graphics.prototype.setFontMichroma16 = function(scale) {
g.setFontCustom(atob("AAAAGAAYAAAAGAB4A/APwD4AeADgAAAAAAA/8H/4YBjAGMAcwBzAHMAcwBzAHMAYYBh/+D/wAAAAABgAOABwAGAA//h/+AAAAAA4+Hn4YZjhmMOYw5jDmMMYwxjDGOMYYxh/GD4YAAAAADBwcHhgGOAYwBzHHMccxxzHHMcc5xhnGH/4PfAAAAAAAOAB4APgB2AGYAxgHGA4YDBgYGD/+P/4AOAAYAAAAAD+cP547BjsGOwc7BzsHOwc7BzsHOwY7zjv+APgAAAAAD/wf/hmGOYYxhzGHMYcxhzGHOYYZhh3uDP4AeAAAEAA4ADgAOAI4DjgeODw4eDjgOcA7gD8APgA8AAAAAAAAAA58H/4bxjmGMYcxhzGHMYcxhzGHOYYbxh/+DnwAAAAADxgfnBnOOMYwxjDHMMcwxzDHMMY4xhjOH/4P/AAAAAABnAGcAAA"), 46, atob("BAgQCBAQEBAQEBAQBA=="), 16+(scale<<8)+(1<<16));
};

function timeToString(duration) {
    var hrs = ~~(duration / 3600);
    var mins = ~~((duration % 3600) / 60);
    var secs = ~~duration % 60;
    var ret = "";
    if (hrs > 0) {
        ret += "" + hrs + ":" + (mins < 10 ? "0" : "");
    }
    ret += "" + mins + ":" + (secs < 10 ? "0" : "");
    ret += "" + secs;
    return ret;
}

var newtimer_left_from = 60;
var newtimer_left_to = 2*60;

var newtimer_right_from = 5*60;
var newtimer_right_to = 7*60;

var current_from = 5*60;
var current_mid = 6*60;
var current_to = 7*60;
var current_value = 0;

var timerinterval;
var istimeron = false;

var islocked = false;

function countDown() {
  current_value++;
  draw();

  if (current_value == current_from) {
    Bangle.buzz(500);
  } else if (current_value == current_mid) {
    Bangle.buzz(400).then(()=>{
      return new Promise(resolve=>setTimeout(resolve, 800));
    }).then(()=>{
      return Bangle.buzz(500);
    });
  } else if (current_value == current_to) {
    Bangle.buzz(300).then(()=>{
      return new Promise(resolve=>setTimeout(resolve, 600));
    }).then(()=>{
      Bangle.buzz(300).then(()=>{
        return new Promise(resolve=>setTimeout(resolve, 600));
      }).then(()=>{
        return Bangle.buzz(500);
      });
    });
  }

}

Bangle.on('touch',(touchside, touchdata)=>{
  if (!islocked && istimeron && touchdata.y > (100+10)) {
    Bangle.buzz(40);
    istimeron = false;
    clearInterval(timerinterval);
  } else if (touchdata.y > 24 && touchdata.y < (100-10)) {
    Bangle.buzz(40);
    islocked = !islocked;
  } else if (!islocked && touchdata.y > (100+10) && touchdata.x > 88 + 10) {
    Bangle.buzz(40);
    current_from = newtimer_right_from;
    current_to = newtimer_right_to;
    current_mid = (current_from + current_to) / 2;
    current_value = 0;
    if (timerinterval) clearInterval(timerinterval);
    timerinterval = setInterval(countDown, 1000);
    istimeron = true;
  } else if (!islocked && touchdata.y > (100+10) && touchdata.x < 88 - 10) {
    Bangle.buzz(40);
    current_from = newtimer_left_from;
    current_to = newtimer_left_to;
    current_mid = (current_from + current_to) / 2;
    current_value = 0;
    if (timerinterval) clearInterval(timerinterval);
    timerinterval = setInterval(countDown, 1000);
    istimeron = true;
  }
  showInstructions = false;
  draw();
});

Bangle.on('swipe',(swiperight, swipedown)=>{
  console.log(swiperight);
  console.log(swipedown);

  if (swiperight == -1) {
    if (newtimer_left_from >= 60) {
      newtimer_left_from += 60;
      newtimer_left_to += 60;
    } else { // special case for 0:30 to 1:00
      newtimer_left_from = 60;
      newtimer_left_to = 120;
    }
    newtimer_right_from += 60;
    newtimer_right_to += 60;
    draw();
  } else if (swiperight == 1) {
    if (newtimer_left_from > 60) {
      newtimer_left_from -= 60;
      newtimer_left_to -= 60;
    } else { // special case for 0:30 to 1:00
      newtimer_left_from = 30;
      newtimer_left_to = 60;
    }

    if (newtimer_right_from > 120) {
      newtimer_right_from -= 60;
      newtimer_right_to -= 60;
    }
    draw();
  }
});

var showInstructions = true;

function draw() {
  g.reset();
  if (current_value >= current_to) { g.setBgColor("#F00"); }
  else if (current_value >= current_mid) { g.setBgColor("#FF0"); }
  else if (current_value >= current_from) { g.setBgColor("#8F8"); }
  g.clearRect(0,24,176,176);

  g.reset().setFontAlign(0, 0).setColor(istimeron ? "#000" : "#444");
  g.setFont("Michroma36").drawString(timeToString(current_value), 88, 62);

  g.reset().setFontAlign(0, 0);

  g.setFont("HaxorNarrow7x17");
  g.drawString(timeToString(current_from), 44, 62+26);
  g.drawString(timeToString(current_mid), 88, 62+26);
  g.drawString(timeToString(current_to), 132, 62+26);

  if (current_value >= current_from) { g.drawRect(44-1,62+26+9,44+1,62+26+9+1); }
  if (current_value >= current_mid) { g.drawRect(88-1,62+26+9,88+1,62+26+9+1); }
  if (current_value >= current_to) { g.drawRect(132-1,62+26+9,132+1,62+26+9+1); }

  if (showInstructions) {
    g.setFont("6x8").drawString("Tapping timer locks buttons", 88, 100+5);
    g.setFont("6x8").drawString("<= Swipe to change time =>", 88, 168);
  }

  g.setColor(islocked ? "#444" : "#000");
  g.setFont("Michroma16");
  g.drawString(timeToString(newtimer_left_from), 44, 138-9);
  g.drawString(timeToString(newtimer_left_to), 44, 138+9);
  g.drawString(timeToString(newtimer_right_from), 132, 138-9);
  g.drawString(timeToString(newtimer_right_to), 132, 138+9);

  g.drawRect(0+8,138-24, 88-9+1, 138+22+1);
  g.drawRect(0+8,138-24, 88-9, 138+22);
  g.drawRect(88+8,138-24, 176-10+1, 138+22+1);
  g.drawRect(88+8,138-24, 176-10, 138+22);
}

g.setTheme({bg:"#fff",fg:"#000",dark:false}).clear();
require("FontHaxorNarrow7x17").add(Graphics);
g.clear();
Bangle.loadWidgets();
Bangle.drawWidgets();
draw();
