#include "Raindrop.h"

const float pi = 3.14159265359;
//float displayPropellerAngle = 0;
int displayPropellerAngle = 0;
int diplayAniTick = 0;
int diplayAniDropTick = 0;

int displayStateTick3 = 0;
int displayDemoTick = 0;

uint32_t footerScrollSpeed = 3;
String footerText = "";
uint32_t footerDelayTick = 0;
int footerX = 4;
int footerY = 61;
int footerDisplayLength = 0;
uint32_t footerScrollTick = 0;

void DisplayFooterText(String v){
  if (footerText!=v){
    //reset and delay
    footerX = 4;
    footerText = v;
    footerDisplayLength = DrawTextAtWitdh(v.c_str());
    footerDelayTick = millis();
    footerScrollTick = millis();
    DrawTextAt(footerX, footerY, footerText.c_str());
  }
  
  //animation of text
  if (footerDisplayLength<=120){
    DrawTextAt(footerX, footerY, footerText.c_str());
  }else{
    //scrolling animation
    if ((millis()-footerDelayTick) > 2000){
      if (millis()-footerScrollTick >= 20){
        footerScrollTick = millis();
        footerX-=footerScrollSpeed;
        if (footerX+footerDisplayLength+8<0){
          footerScrollTick = millis();
          footerX = 128;
        }
        DrawTextAt(footerX, footerY, footerText.c_str());
      }
    }else{
      DrawTextAt(footerX, footerY, footerText.c_str());
    }
  }
}

String formatTime(int minutes, int seconds) {
  char buffer[6];
  sprintf(buffer, "%02d:%02d", minutes, seconds);
  return String(buffer);
}

String MillisToMMSS(uint32_t v){
  uint32_t sec = v / 1000;
  uint32_t m = sec / 60;
  sec = sec - (m*60);
  String timeString = formatTime(m, sec);
  return timeString;
}

void DashboardRoutine(){
  ClearDisplayBuffer();
  //gambar bingkai
  DrawFrame(0,0,128,64);
  
  DrawFrame(0,0,128,12); //kotak kiri atas
  DrawFrame(0,0,43,12); //kotak tengah atas
  DrawFrame(42,0,43,12); //kotak kanan atas
  
  DrawFrame(0,52,128,12); //kotak bawah

  DrawTextAt(4, 9, "pH ---"); //isi dengan PH
  DrawTextAt(46, 9, "NTU ---"); //isi dengan NTU
  DrawTextAt(88, 9, "Auto"); //isi dengan apalah: sinyal wifi kek, sisa batre kek ya apalah gitu. Asal jangan isi dengan perasaanmu padanya.

  //draw tank ==================================
  int x = 0;
  int y = 15;
  int w = 25;
  int h = 34;
  int gap = 11;
  //int startX = (128 - ((2*(w+gap)) + w)) / 2; //center, ignore pump draw
  int startX = (128 - ((3*(w+gap)) + 0)) / 2; //center includes pump draw
  String v = "";
  for (int i = 0; i<3; i++){

    //tong
    x = startX + (i*(w+gap));
    //DrawTank(x,y,w,h);

    //kotak pompa samping
    DrawFrame(x+w-1, y+4, gap+2, gap+2);

    //animasi ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    bool drawPump = false;
    if (i==0){
      //pompa in 1 (ke tong penampung setelah sterilisasi)
      drawPump = GetSwitchState(STERILIZED_CHAMBER_FILL_PUMP_SWITCH);
    }else if (i==1){
      //pompa in 2 (tong sterilisasi)
      drawPump = GetSwitchState(STERILIZATION_CHAMBER_FILL_PUMP_SWITCH);
    }else{
      //pompa in 3 (tong penampung air kotor)
      drawPump = GetSwitchState(WATER_BANK_FILL_PUMP_SWITCH);
    }
    
    if (drawPump){
      //propeller
      drawPropeller(x+w+5,y+4+6,10,displayPropellerAngle);
      //bulatan propeller
      DrawCircle(x+w+5,y+4+6,5);
    }
    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    if (i==1){
      //large propeller on tank 2
      bool drawSterilizationFX = false;
      drawSterilizationFX = GetSwitchState(STERILIZATION_OZONE_AND_UV_SWITCH);
      if (drawSterilizationFX){
        //draw big propeller
        drawPropeller(x+(w/2),y+4+17,11,displayPropellerAngle);
        //drawPropeller(x+(w/2),y+4+15,11,displayPropellerAngle+45);
        //draw waves
        DrawCircle(x+(w/2),y+4+17,0+diplayAniTick<10?7+diplayAniTick:10);
        DrawCircle(x+(w/2),y+4+17,5+diplayAniTick<10?5+diplayAniTick:10);
        DrawCircle(x+(w/2),y+4+17,7+diplayAniTick<10?7+diplayAniTick:10);
      }
    }

    //wave animation on tank 1
    if (i==0){
      bool drawUvFX = false;
      drawUvFX = GetSwitchState(STERILIZED_UV_SWITCH);
      if (drawUvFX){
        DrawCircle(x+(w/2),y+4+17,0+diplayAniTick<10?diplayAniTick:10);
        DrawCircle(x+(w/2),y+4+17,5+diplayAniTick<10?5+diplayAniTick:10);
        DrawCircle(x+(w/2),y+4+17,7+diplayAniTick<10?7+diplayAniTick:10);
      }
    }

    //draw between empty-lower-upper with xor mode
    int waterState = 0;
    if (i==0){
      //(tong penampung setelah sterilisasi)
      if (GetInSwitchState(STERILIZED_CHAMBER_FLOAT_SWITCH_LOWER)) waterState = 1;
      if (GetInSwitchState(STERILIZED_CHAMBER_FLOAT_SWITCH_UPPER)) waterState = 2;
    }else if (i==1){
      //(tong sterilisasi)
      if (GetInSwitchState(STERILIZATION_CHAMBER_FLOAT_SWITCH_LOWER)) waterState = 1;
      if (GetInSwitchState(STERILIZATION_CHAMBER_FLOAT_SWITCH_UPPER)) waterState = 2;
    }else{
      //(tong penampung air kotor)
      if (GetInSwitchState(WATER_BANK_FLOAT_SWITCH_LOWER)) waterState = 1;
      if (GetInSwitchState(WATER_BANK_FLOAT_SWITCH_UPPER)) waterState = 2;
    }
    //drawing
    if (waterState==1){
      //lower
      DrawTankFilled(x+2,y+(h-10)-2,w-4,10); //h harus minimal radius*2 hedeh
    }else if (waterState==2){
      //upper
      DrawTankFilled(x+2,y+(h-24)-1,w-4,23); //h harus minimal radius*2 hedeh
    }

    DrawTank(x,y,w,h);

    if (drawPump){
      //watering animation on all tank
      initRaindrops(6, h-11);
      animateRain(diplayAniDropTick, (x+w)-9, y+7, 6, h-11);
    }
    
    //angka pada tong
    v = (String) (i+1);
    DrawTextAt(x+10, y+8, v.c_str());
  }
  //===========================================

  diplayAniTick++;
  if (diplayAniTick>5) diplayAniTick = 0;
  diplayAniDropTick++;
  if (diplayAniDropTick>8) diplayAniDropTick = 8;

  if (millis()-displayDemoTick>2000){
    displayDemoTick = millis();
    displayStateTick3++;
    if (displayStateTick3>=3) displayStateTick3 = 0;
  }
  
  //footer info
  //ni nih paling susah karena harus sinkronasi sama error dan perubahan text error
  String footerToDisplay = "";
  if (ChamberIsError()){
    footerToDisplay = ChamberErrorString();
  }else if (IsWaterBankError()){
    footerToDisplay = WaterBankErrorString();
  }else{
    if (ChamberIsIdle()){
      if (ChamberIsSuccess()){
        footerToDisplay = "Selesai.";
      }else{
        if (IsWaterBankSystemRunning()){
          footerToDisplay = "Mengisi Tangki 3...";
        }else{
          footerToDisplay = "Sistem siap."; //ready
        }
      }
    }else if (IsChamberSystemRunning()){
      //display state
      if (IsChamberFilling()){
        footerToDisplay = "Mengisi Tangki 2...";
      }else if (IsChamberUnload()){
        footerToDisplay = "Memindahkan ke Tangki 1...";
      }else{
        uint32_t t = GetSterilizationRemainingMillis();
        if (t>0){
          bool onPostUV = OnPostUV();
          bool onOzone = OnOzoneStrelization();
          if (onOzone){
            footerToDisplay = "Sedang Sterilisasi " + MillisToMMSS(t);
          }else if (onPostUV){
            footerToDisplay = "Post UV " + MillisToMMSS(t);
          }else{
            footerToDisplay = "Sedang Sterilisasi..."; //aneh sih kalau disini, dahlah
          }
        }else{
          footerToDisplay = "Sedang Sterilisasi...";
        }
      }
    }
  }

  DisplayFooterText(footerToDisplay);
  //DrawTextAt(4, 61, "Ready."); 
  
  
  SendDisplayBuffer();

  //displayPropellerAngle+=pi/10;
  //if (displayPropellerAngle>=pi) displayPropellerAngle-=pi;
  displayPropellerAngle-=15;
  if (displayPropellerAngle<=-360) displayPropellerAngle=-(abs(displayPropellerAngle)%360);
}

void DashboardDemoRoutine(){
  ClearDisplayBuffer();
  //gambar bingkai
  DrawFrame(0,0,128,64);
  
  DrawFrame(0,0,128,12); //kotak kiri atas
  DrawFrame(0,0,43,12); //kotak tengah atas
  DrawFrame(42,0,43,12); //kotak kanan atas
  
  DrawFrame(0,52,128,12); //kotak bawah
  //draw lines for
  
  //DrawDashboardBackground(); //not implemented yet
  DrawTextAt(4, 9, "pH 14.0"); //isi dengan PH
  DrawTextAt(46, 9, "NTU 999"); //isi dengan NTU
  DrawTextAt(88, 9, "Auto"); //isi dengan apalah: sinyal wifi kek, sisa batre kek
  
  //draw tank ==================================
  int x = 0;
  int y = 15;
  int w = 25;
  int h = 34;
  int gap = 11;
  //int startX = (128 - ((2*(w+gap)) + w)) / 2; //center, ignore pump draw
  int startX = (128 - ((3*(w+gap)) + 0)) / 2; //center includes pump draw
  String v = "";
  for (int i = 0; i<3; i++){

    //tong
    x = startX + (i*(w+gap));
    //DrawTank(x,y,w,h);

    //pompa samping
    DrawFrame(x+w-1, y+4, gap+2, gap+2);
    //propeller
    drawPropeller(x+w+5,y+4+6,10,displayPropellerAngle);
    //bulatan propeller
    DrawCircle(x+w+5,y+4+6,5);

    if (i==1){
      //large propeller on tank 2
      drawPropeller(x+(w/2),y+4+17,11,displayPropellerAngle);
      //drawPropeller(x+(w/2),y+4+15,11,displayPropellerAngle+45);
      DrawCircle(x+(w/2),y+4+17,0+diplayAniTick<10?0+diplayAniTick:10);
      DrawCircle(x+(w/2),y+4+17,5+diplayAniTick<10?5+diplayAniTick:10);
      DrawCircle(x+(w/2),y+4+17,7+diplayAniTick<10?7+diplayAniTick:10);
    }

    //wave animation on tank 1
    if (i==0){
      DrawCircle(x+(w/2),y+4+17,0+diplayAniTick<10?diplayAniTick:10);
      DrawCircle(x+(w/2),y+4+17,5+diplayAniTick<10?5+diplayAniTick:10);
      DrawCircle(x+(w/2),y+4+17,7+diplayAniTick<10?7+diplayAniTick:10);
    }

    //draw between empty-lower-upper with xor mode
    if (displayStateTick3==1){
      //lower
      DrawTankFilled(x+2,y+(h-10)-2,w-4,10); //h harus minimal radius*2 hedeh
    }else if (displayStateTick3==2){
      //upper
      DrawTankFilled(x+2,y+(h-24)-1,w-4,23); //h harus minimal radius*2 hedeh
    }

    DrawTank(x,y,w,h);

    //watering animation on all tank
    initRaindrops(6, h-11);
    animateRain(diplayAniDropTick, (x+w)-9, y+7, 6, h-11);
    
    //angka pada tong
    v = (String) (i+1);
    DrawTextAt(x+10, y+8, v.c_str());
  }
  //===========================================

  diplayAniTick++;
  if (diplayAniTick>5) diplayAniTick = 0;
  diplayAniDropTick++;
  if (diplayAniDropTick>8) diplayAniDropTick = 8;

  if (millis()-displayDemoTick>2000){
    displayDemoTick = millis();
    displayStateTick3++;
    if (displayStateTick3>=3) displayStateTick3 = 0;
  }
  
  //footer info
  
  DrawTextAt(4, 61, "Ready."); 
  
  SendDisplayBuffer();

  //displayPropellerAngle+=pi/10;
  //if (displayPropellerAngle>=pi) displayPropellerAngle-=pi;
  displayPropellerAngle-=15;
  if (displayPropellerAngle<=-360) displayPropellerAngle=-(abs(displayPropellerAngle)%360);
}

