// Clock with date day and moon based on the "Anton Clock"


Graphics.prototype.setFontmoonphases = function() {
  // Actual height 23 (23 - 1)
  return this.setFontCustom(
    atob('[encoded data, 4220 characters]'),
    32,
    atob("GAgKDAwUEwQICBgXBggGBxgMDAwMDAwMDAwHBw4ADgsYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgIBwg="),
    24|65536
  );
}

Graphics.prototype.setFontInknutAntiqua24 = function() {
  // Actual height 24 (23 - 0)
  return this.setFontCustom(
    atob('AAAAAB4AAD4AAD4AAD4AABwAAAAAAAAAAAAAAAAPAAD/AA/4AP+AD/gAf4AAeAAAAAAAAAAAAYAAB/AAD/gAH/wAP/4APH4AcB4AcA4AcA4AcA4AeA4AfBwAf/wAP/gAH/AAD+AAA4AAAAAAAAAAIAYAcAYAcA4Af/4Af/4Af/4Af/4AcAYAYAYAAAQAAAAACAIAGAYAOA4AeB4AcD4AcH4AcP4Aef4Af94Af54APx4APh4ADH4AAH8AAAAAAAAACAAAGAAYOAAYeAA4cCAwcHBwefBwf/ngP//gPz/AHj+ACB8AAAwAAAAAAAwAABwAAHwAAPwAAfwAA8wABwwADgwAPBwAP//wf//wf//wf//gAAwAAAwAAAwAAAAAAHAGD/AOH/AMHvgMHngcHnwYHn44Hj/wHj/wfh/gfw/AAAeAAAAAAAAAAAAAAB8AAP/AAf/gA//wB//4D8D4HwB4HsA4POA4ePA4cPh44H/w4H/gAD/AAB+AAAYAAAAAAAAA/gAAfgAAeAAYeAB4eAHweAfAeB8AeHwAePAAe8AAf4AAfgAAeAAAYAAAAAAAAAAAAAeADg/APx/gP7/gf//w/+Dw4+Bw4fBw4fBw8fhw//zwf3/gfj/gPD/AAB+AAAYAAAAAAAAAA+AAD/AAH/gYH/wYP/w4OD5wcA5wcAzgeAPgfAfAP/+AP/8AH/4AD/wAB/AAAAAAAAAAAAAAHB4APj4APj4APj4APBwAAAAAAAAA'),
    46,
    atob("CAkTDBANEQ8SEBISCA=="),
    24|65536
  );
};

Graphics.prototype.setFontInknutAntiqua66 = function() {
  // Actual height 66 (65 - 0)
  return this.setFontCustom(
    atob('[encoded data, 5656 characters]'),
    46,
    atob("FhczISslMCkyKzMyFg=="),
    66|65536
  );
};

Graphics.prototype.setFontInknutAntiqua88 = function() {
  // Actual height 88 (87 - 0)
  return this.setFontCustom(
    atob('[encoded data, 10076 characters]'),
    46,
    atob("HR9FLTkxQDdDOkNDHQ=="),
    88|65536
  );
};

{ // 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 drawStatusTimeout;
  
let drawStatus = function(){ // battery and 24h line clock, remove if ou want  to display wigets instead
  
g.clearRect(0,22,175,22);
g.setColor(g.theme.fg); // draw battery 
g.drawLine(0,2,E.getBattery()*1.75,2);
  // draw line for 24 h clock
  var x = g.getWidth() / 2;
  //var y = g.getHeight() / 2;
  var ix;
  var shade;
  var date = new Date();
  for ( ix = 0; ix < 176; ix=ix+4){
    shade = 2*Math.PI * ((date.getHours()-6)/24);
    shade = Math.sin(2*Math.PI* ix/176 + shade );
    shade = shade * 0.5 + 0.5;
    shade = 1- shade ;
g.setColor(shade,shade,shade);
g.drawLine(ix,18, ix+3,18);
  }
g.setColor(g.theme.fg); // draw dots at noon / midnight
ix = x-(((date.getHours())%12)/24)*176;
g.drawLine(ix,16,ix+2,18);
g.drawLine(ix+2,18,ix, 20);
g.drawLine(ix,20,ix-2,18);
g.drawLine(ix-2,18,ix,16);
  
ix = ix + 176/2;
g.drawLine(ix,16,ix+2,18);
g.drawLine(ix+2,18,ix, 20);
g.drawLine(ix,20,ix-2,18);
g.drawLine(ix-2,18,ix,16);

// queue next drawStatus
  if (drawStatusTimeout) clearTimeout(drawStatusTimeout);
  drawStatusTimeout = setTimeout(function() {
    drawStatusTimeout = undefined;
    drawStatus();
    //draw();
    //drawWeek();    
  }, 30*60000 - (Date.now() % 30*60000));
  
};//------------------<< status
  
  
  
let drawWeekTimeout;
  
let drawWeek = function() {
  //  date and day of week, moonphase, next quater moon
  var date = new Date();
  var weekday = Math.abs(date.getDay() -1 % 7);
  var dateStr = date.getDate() ;
  
  g.reset().clearRect(0,175-36,175,175-36);
  g.setColor(g.theme.fg);
  g.fillRect(0, 175-15, 175, 175);
  var pos;
  var d;
 for ( d=0; d< 7; d++){ 
    pos = d * 25;
     if (d < 5 ){
       g.setColor(g.theme.bg);
       g.fillRect(pos, 175-15, pos+22, 175);
     }else{        
       g.setColor(g.blendColor(g.theme.bg,g.theme.fg,0.25));
       g.fillRect(pos, 175-15, pos+22, 175);
     }
    
 }
g.setColor(g.theme.fg);
g.setFontAlign(0, 0).setFont("InknutAntiqua24", 1).drawString(dateStr,  weekday*(176/7)+11, g.getHeight() - 15);

var mphaseq=-1; // moonphase 0-3
var qday = -1; // day in week quater moon
var moonssince; // full moons since May 5th 2023
var lundaymo; // lunation day monday
var tms = date.getTime() + 86400000;//
var weekdayms = weekday * 86400000 ;//seconds since monday
var startd = tms - weekdayms; // monday
var perc;// percent of cycle
tms = startd - 1683308000000 ;//may5th 1734h 2023;
moonssince = tms /2551442823.5;//div ms in a lunar month
perc = moonssince - Math.floor(moonssince); 
lundaymo = perc * 29.530588235;
mphaseq =Math.round((lundaymo)/ 7.38264705875);
   
qday = Math.round( 7.38264705875 - (lundaymo- (mphaseq* 7.38264705875))) % 7; // next quater phase day
mphaseq = (mphaseq+1) % 4; // set to next quater phase
  

g.setColor(0,0,0); // black moon bg
var yoffset = (qday == weekday )*24;
g.setFontAlign(0, 0).setFont("moonphases", 1).drawString(String.fromCharCode(48), qday*(176/7)+11, g.getHeight() - 22- yoffset);
g.setColor(1,1,1); // moonshape
   //g.setFontAlign(0, 0).setFont("moonphases", 1).drawString(String.fromCharCode(65+mphaseq*26/4), qday*(176/7)+11, g.getHeight() - 26- yoffset); // next moon or :
  // exception for full moon:
  var char;
  if (Math.round(65+perc*26)==77) { 
       char = 48;
      }else{
   char = Math.round(65+perc*26);
      }
g.setFontAlign(0, 0).setFont("moonphases", 1).drawString(String.fromCharCode(char), qday*(176/7)+11, g.getHeight() - 22- yoffset);

// queue next drawWeek
  if (drawWeekTimeout) clearTimeout(drawWeekTimeout);
  drawWeekTimeout = setTimeout(function() {
    drawWeekTimeout = undefined;
    draw();
    drawWeek();
  }, 180*60000 - (Date.now() % 180*60000)); 

};//-------------------<< week
  
let drawTimeout;

//  draw  clock
let draw = function() {
  var x = g.getWidth() / 2;
  var y = g.getHeight() / 2;
  var xoffset =0;
  var minute;
  var hour;
  var hour_old;
  var date;
  var digit1;
  var digit2;
  
  g.reset().clearRect(0,25,175,175-36); // clear area

  date = new Date();
  hour_old = hour;
  hour = date.getHours(); // hour 
  if (hour_old != hour){ // only draw if hour has changed
  if (hour > 12) {
    hour = hour % 12;
  }
  digit2 = hour % 10;
  digit1 = hour - digit2;
  if (digit1 > 0 ){ // draw hours, one or two digits
  xoffset = -10;
  g.setColor(g.theme.fg);
   g.setFontAlign(1, 0).setFont("InknutAntiqua66",1).drawString(hour, x+xoffset, y);
  }
  else {
    xoffset = -15;
  g.setColor(g.theme.fg);
  g.setFontAlign(1, 0).setFont("InknutAntiqua88",1).drawString(hour, x+xoffset, y);  
  }
  }
   minute = date.getMinutes(); //   minutes
  digit2 = minute % 10;
  digit1 = Math.floor(minute / 10);
  minute = digit1.toString() + digit2.toString();
  
    g.setColor(g.blendColor(g.theme.bg,g.theme.fg,0.5));
  g.setFontAlign(-1, 0).setFont("InknutAntiqua66",1).drawString(minute, x+xoffset , y);
  
// queue next draw
  if (drawTimeout) clearTimeout(drawTimeout);
  drawTimeout = setTimeout(function() {
    drawTimeout = undefined;
    draw();
  }, 60000 - (Date.now() % 60000));
 
};//-----------------------<< clock  
  

// Show launcher when middle button pressed
Bangle.setUI({
  mode : "clock",
  remove : function() {
    // Called to unload all of the clock app
    if (drawStatusTimeout) clearTimeout(drawStatusTimeout);
    drawStatusTimeout = undefined;
    if (drawTimeout) clearTimeout(drawTimeout);
    drawTimeout = undefined;
    if (drawWeekTimeout) clearTimeout(drawWeekTimeout);
    drawWeekTimeout = undefined;
    delete Graphics.prototype.setFontmoonphases;
    delete Graphics.prototype.setFontInknutAntiqua24;
    delete Graphics.prototype.setFontInknutAntiqua66d;
    delete Graphics.prototype.setFontInknutAntiqua88;
    
    
  }});
//Load widgets
//Bangle.loadWidgets();
g.reset().clearRect(0,0,g.getWidth(),g.getHeight());
draw();
drawWeek();
drawStatus(); // remove if ou want wigets to draw instead
//
//setTimeout(Bangle.drawWidgets,0);
}
