#include <U8g2lib.h>

U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, 18, 23, 5, U8X8_PIN_NONE);

//splashscreen
const unsigned char logo [] U8X8_PROGMEM = {
  0x00,0x00,0xff,0x07,0x00,0x00,0x00,0xe0,0x20,0x18,0x00,0x00,0x00,0x18,0x40,0x60,0x00,0x00,0x00,0x06,0x00,0x80,
  0x01,0x00,0x00,0x07,0x00,0x00,0x06,0x00,0x80,0x05,0x00,0x00,0x0c,0x00,0xc0,0x00,0x40,0x00,0x08,0x00,0x40,0x00,
  0x02,0x00,0x10,0x00,0x20,0x00,0x02,0x08,0x10,0x00,0x20,0x08,0x00,0x08,0x20,0x00,0x30,0x10,0x20,0x04,0x20,0x00,
  0x30,0x10,0x20,0x00,0x21,0x00,0x51,0x20,0x22,0x80,0x21,0x02,0x91,0x20,0x24,0x82,0x20,0x02,0x11,0x40,0x24,0x42,
  0x20,0x02,0x11,0x42,0x24,0x42,0x20,0x02,0x11,0x86,0x24,0x21,0x20,0x02,0x31,0x84,0x24,0x21,0x22,0x02,0x71,0x08,
  0x25,0x11,0x23,0x02,0xd1,0x10,0x29,0x99,0x21,0x02,0x91,0x33,0xaa,0xc9,0x30,0x02,0x11,0x46,0xfe,0x65,0x2c,0x02,
  0x31,0xdc,0x03,0x27,0x22,0x02,0xf1,0xf1,0x00,0x9c,0x21,0x02,0x11,0x67,0x00,0xd8,0x20,0x02,0x31,0x38,0x00,0x70,
  0x20,0x02,0xf1,0x31,0x00,0x20,0x3e,0x02,0x11,0x13,0x00,0x40,0x21,0x02,0x31,0x0c,0x07,0xc0,0x23,0x02,0x71,0xe8,
  0x18,0x60,0x2c,0x02,0xd1,0x39,0xe0,0x1c,0x30,0x02,0x11,0x07,0x00,0x03,0x20,0x02,0x11,0x00,0x00,0x00,0x20,0x02,
  0x31,0xc0,0x1f,0xe0,0x27,0x02,0xf1,0x60,0x60,0x18,0x38,0x02,0x11,0x1f,0x80,0x07,0x20,0x02,0x11,0x00,0x00,0x00,
  0x20,0x02,0x31,0x80,0x0f,0xc0,0x27,0x02,0x71,0x60,0x30,0x30,0x38,0x02,0x91,0x3f,0xc0,0x0f,0x20,0x02,0x11,0x00,
  0x00,0x00,0x20,0x02,0x11,0x00,0x00,0x00,0x20,0x02,0x11,0x00,0x07,0x80,0x23,0x02,0x71,0xc0,0x18,0x60,0x3c,0x02,
  0xe3,0x3f,0xe0,0x1c,0x30,0x02,0x23,0x00,0x00,0x03,0x10,0x02,0x22,0x00,0x00,0x00,0x10,0x03,0x42,0xc0,0x1f,0xe0,
  0x0f,0x01,0x84,0x39,0x60,0x18,0x0c,0x01,0x04,0x07,0x80,0x07,0x86,0x01,0x08,0x06,0x00,0x00,0xc3,0x00,0x18,0x38,
  0x00,0xe0,0x60,0x00,0x30,0xc0,0xff,0x1f,0x30,0x00,0xc0,0x00,0x00,0x00,0x0c,0x00,0x00,0x03,0x00,0x00,0x03,0x00,
  0x00,0xfc,0xff,0xff,0x00,0x00};


void SplashScreen(){
  u8g2.clearBuffer();
  u8g2.setDrawColor(1); //set to 1
  u8g2.setFont(u8g2_font_helvB08_tr);
  u8g2.drawStr(51, 32, "HYDROZONER");
  u8g2.drawXBMP(6, 4, 42, 56, logo);
  u8g2.setFont(u8g2_font_profont11_tr);
  u8g2.drawStr(55, 41, "PKM-KC 2024");
  u8g2.sendBuffer();
  //delay(2000);
}

void InitDisplay(){
  //u8g2.setBusClock(1000000);
  u8g2.setBusClock(8000000);
  u8g2.begin();
  //u8g2.clearBuffer();
  u8g2.setFontMode(1);
  u8g2.setBitmapMode(1);
  u8g2.clear();
  delay(2);
  u8g2.initDisplay();
  delay(2);
  //u8g2.sendBuffer();
}

void ClearDisplayBuffer(){
  u8g2.clearBuffer();
}

void SendDisplayBuffer(){
  u8g2.sendBuffer();
}

void DrawDashboardBackground(){
  //Not implemented
  //u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_profont11_tr);
  u8g2.setDrawColor(1);
  u8g2.drawStr(0, 10, "Dashboard not coded!"); 

  //u8g2.drawXBM(0, 0, 32, 32, tangki_kosong); 
  
  //u8g2.sendBuffer();
  //...................................
}


void DrawSectionPlaceholder(){
  u8g2.setFont(u8g2_font_profont12_tr); //8 pixel height font
  u8g2.setDrawColor(1);
  int x = 0;
  int y = 0;
  int h = 12;
  int w = 128;
  for (int i=0;i<5;i++){
    y = (i*12); 
    if ((i%2)==0){
      u8g2.drawBox(x,y,w,h);
      u8g2.setDrawColor(2); //set to xor
      //u8g2.setFontMode(1);
    }else{
      //u8g2.setFontMode(0);
      u8g2.setDrawColor(1); //set to 1
    }
    y = 10 + (i*12);
    u8g2.drawStr(x, y, "123456789ABCDEFGHIJKL");
  }
}

int DrawTextRowCount(){
  return 5;
}

void DrawTextMenuAtRow(const char *v, byte row, bool highlight){
  int x = 0;
  int y = 0;
  int h = 12;
  int w = 128;
  u8g2.setFont(u8g2_font_profont12_tr); //8 pixel height font

  u8g2.setDrawColor(1);

  //highlight
  if (highlight){
    x = 0;
    y = row*12;
    u8g2.drawBox(x,y,w,h);
    u8g2.setDrawColor(2); //set to xor
  }

  //text
  x = 2;
  y = 10 + (row*12);
  u8g2.drawStr(x, y, v);
}

void DrawAbout(){
  u8g2.clearBuffer();
  u8g2.setDrawColor(1); //set to 1
  u8g2.setFont(u8g2_font_helvB08_tr);
  u8g2.drawStr(51, 22, "HYDROZONER");
  u8g2.drawXBMP(6, 4, 42, 56, logo);
  u8g2.setFont(u8g2_font_profont11_tr);
  u8g2.drawStr(55, 35, "PKM-KC 2024");
  u8g2.drawStr(55, 45, "V1.0");
  u8g2.sendBuffer();
}

void DrawUnderConstruction(){
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_profont12_tr); //8 pixel height font
  u8g2.setDrawColor(1);
  u8g2.drawStr(5, 35, "<Under Construction>");
  u8g2.sendBuffer();
}

void DrawCenteredText(const char *v, byte row, bool highlight=false, bool arrowLeft = false, bool arrowRight = false, bool horizontalLine=false){
  int x = 0;
  int y = 0;
  int h = 12;
  int w = 128;
  u8g2.setFont(u8g2_font_profont12_tr); //8 pixel height font

  u8g2.setDrawColor(1);

  //highlight
  if (highlight){
    x = 0;
    y = row*12;
    u8g2.drawBox(x,y,w,h);
    u8g2.setDrawColor(2); //set to xor
  }

  //text
  x = (128-u8g2.getStrWidth(v)) / 2;
  y = 10 + (row*12);
  u8g2.drawStr(x, y, v);
  
  if (arrowLeft){
    u8g2.setFont(u8g2_font_profont12_tr); //8 pixel height font
    x = 10;
    u8g2.drawStr(x, y, "<");
  }

  if (arrowRight){
    u8g2.setFont(u8g2_font_profont12_tr); //8 pixel height font
    x = 108;
    u8g2.drawStr(x+u8g2.getStrWidth(">"), y, ">");
  }

  u8g2.setDrawColor(1);
  if (horizontalLine){
    u8g2.drawHLine(0, y+2, 128);
  }
}

void DrawTank(int x, int y, int w, int h){
  //draw rounded
  u8g2.setDrawColor(1);
  //u8g2.drawRBox(x, y, w, h, 4);
  u8g2.drawRFrame(x, y, w, h, 4);
}

void DrawTankFilled(int x, int y, int w, int h){
  //draw rounded
  //u8g2.drawRBox(x, y, w, h, 4);
  u8g2.setDrawColor(2); //set to xor
  u8g2.drawRBox(x, y, w, h, 4);
}

void DrawTextAt(int x, int y, const char *v){
  u8g2.setFont(u8g2_font_profont10_tr); //6 pixel height font
  u8g2.setDrawColor(1);
  u8g2.drawStr(x, y, v);
}

int DrawTextAtWitdh(const char *v){
  u8g2.setFont(u8g2_font_profont10_tr); //6 pixel height font
  return u8g2.getStrWidth(v);
}

void DrawFrame(int x, int y, int w, int h){
  u8g2.setDrawColor(1); 
  u8g2.drawFrame(x, y, w, h);
}

// Function to draw a propeller
void drawPropeller(int x, int y, int diameter, float angle, bool xorMode=false) {
  int radius = diameter / 2;
  float radAngle = angle * (PI / 180.0);  // Convert angle to radians

  // Calculate positions of the blades
  int x1 = x + radius * cos(radAngle);
  int y1 = y + radius * sin(radAngle);
  int x2 = x - radius * cos(radAngle);
  int y2 = y - radius * sin(radAngle);

  int x3 = x + radius * cos(radAngle + PI / 2);
  int y3 = y + radius * sin(radAngle + PI / 2);
  int x4 = x - radius * cos(radAngle + PI / 2);
  int y4 = y - radius * sin(radAngle + PI / 2);

  // Draw the blades
  if (xorMode){
    u8g2.setDrawColor(2);
  }else{
    u8g2.setDrawColor(1);
  }
  u8g2.drawLine(x, y, x1, y1);
  u8g2.drawLine(x, y, x2, y2);
  u8g2.drawLine(x, y, x3, y3);
  u8g2.drawLine(x, y, x4, y4);
}

void DrawCircle(int x, int y, int r){
  u8g2.setDrawColor(1);
  u8g2.drawCircle(x,y,r);
}

