#include "FT18e_STW_DISPLAY.h"
#include "Arduino.h"
#include "EDIPTFT.h"
#include "FT18e_STW_INIT.h"
#include "FT_2018e_STW_CAN.h"

EDIPTFT tft(true, false);
String bezeichnungen[] = {"Batterieleistung", "Moment", "Batterietemp"};
//"T_mot","T_oil","P_oil","% fa","U_batt","P_wat","T_air",
//"P_b_front","P_b_rear","Error Type","Speed_fl","Speed_fr","Speed"};
//"Drehzahl","P_fuel","Index"
int vergleichsindex;
int sizeaalt;
int sizeaneu;
int sizebalt;
int sizebneu;
int sizecalt;
int sizecneu;
int sizedalt;
int sizedneu;
int sizeealt;
int sizeeneu;
uint8_t clearcounter = 56;
uint8_t modealt = Stw_data.mode;
uint8_t trccounter;  // = Stw_data.trc;
uint8_t modecounter; // = Stw_data.mode;
bool trctimer;
bool modetimer;
int led_s[] = {led1, led2,  led3,  led4,  led5,  led6,  led7,  led8,
               led9, led10, led11, led12, led13, led14, led15, led16};
unsigned long poiltimer;
unsigned long tmottimer;
unsigned long toiltimer;
bool poilbool = true;
bool tmotbool = true;
bool toilbool = true;

void init_display() {
  pinMode(writeprotect, OUTPUT);
  digitalWrite(writeprotect, HIGH);
  pinMode(reset, OUTPUT);
  pinMode(disp_cs, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, OUTPUT);
  // pinMode(CLK, INPUT);
  digitalWrite(disp_cs, HIGH);
  digitalWrite(MOSI, HIGH);
  digitalWrite(MISO, HIGH);
  digitalWrite(reset, LOW);
  // edip.smallProtoSelect(7);
  // edip.setNewColor(EA_GREY, 0xe3, 0xe3,0xe3);	 // redefine r-g-b-values
  // of EA_GREY edip.drawImage(0,50,FASTTUBE_LOGO_PNG);
  digitalWrite(reset, HIGH);
  tft.begin(115200); // start display communication
  /*int h = 20;
  char charh[2];
  String strh = String(h);
  strh.toCharArray(charh,2);
  tft.DisplayLight(charh);*/
  tft.cursorOn(false);
  tft.terminalOn(false);
  tft.setDisplayColor(EA_WHITE, EA_BLACK);
  tft.setTextColor(EA_WHITE, EA_BLACK);
  // tft.setTextFont('4');
  tft.setTextSize(5, 8);
  tft.clear();
  // tft.displayLight('30');
  tft.drawText(0, 14, 'C', "FaSTTUBe"); // draw some text
                                        // tft.loadImage(0,0,1);
                                        // delay(2000);
}

double get_value(int a) { return 0; }

void update_display() {
  if (!tft.disconnected) {
    tft.cursorOn(false);
    if (modealt != Stw_data.mode || modetimer == true) {
      display_mode();
    } else {
      if (clearcounter >= 56) {
        tft.clear();
        clearcounter = 0;
      }
      clearcounter += 1;
    }
  }
}

void display_mode() {
  if (modealt != Stw_data.mode) {
    tft.clear();
    tft.setTextSize(6, 8);
    tft.setDisplayColor(EA_WHITE, EA_RED);
    tft.setTextColor(EA_WHITE, EA_RED);
    char modeanzeige[7];
    String str = String("MODE:");
    str += String(Stw_data.mode);
    str.toCharArray(modeanzeige, 7);
    tft.drawText(0, 0, 'L', "            ");
    tft.drawText(0, 60, 'L', "            ");
    tft.drawText(0, 120, 'L', "            ");
    tft.drawText(0, 180, 'L', "            ");
    tft.drawText(15, 68, 'L', modeanzeige);
    modecounter = 0;
    modealt = Stw_data.mode;
    modetimer = true;
  } else if (modecounter >= 255) {
    tft.setDisplayColor(EA_WHITE, EA_BLACK);
    tft.setTextColor(EA_WHITE, EA_BLACK);
    tft.clear();
    modetimer = false;
  } else {
    modecounter += 1;
    delay(5);
  }
}

void alarm(String textstr) {
  uint8_t x = 1;
  ;
  char text[7];
  textstr.toCharArray(text, 7);
  tft.setTextSize(8, 8);
  while (x == 1) {
    if (!tft.disconnected) {
      tft.setTextColor(EA_BLACK, EA_RED);
      tft.fillDisplayColor(EA_RED);
      tft.drawText(5, 68, 'L', text);
    }
    for (int j = 0; j < 16; j++) {
      digitalWrite(led_s[j], HIGH);
    }
    delay(100);
    if (!tft.disconnected) {
      tft.setTextColor(EA_BLACK, EA_WHITE);
      tft.fillDisplayColor(EA_WHITE);
      tft.drawText(5, 68, 'L', text);
    }
    for (int j = 0; j < 16; j++) {
      digitalWrite(led_s[j], LOW);
    }
    delay(100);
    if (Stw_data.button_ll & Stw_data.button_rr) {
      x = 0;
      tft.setTextColor(EA_WHITE, EA_BLACK);
    }
  }
}