#include "display.h"


uint8_t *BlackImage = 0;


static int8_t view_count = -1;
static int8_t view_curr = 0;
static DisplayWrapper** views = 0;

void Display::setup(){
    DEV_Module_Init();
    EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
    EPD_2IN13_V2_Clear();
    delay(100);

    uint16_t Imagesize = ((EPD_2IN13_V2_WIDTH % 8 == 0) ? (EPD_2IN13_V2_WIDTH / 8 ) : (EPD_2IN13_V2_WIDTH / 8 + 1)) * EPD_2IN13_V2_HEIGHT;
    if ((BlackImage = (uint8_t *)malloc(Imagesize)) == NULL) exit(1);
    Paint_NewImage(BlackImage, EPD_2IN13_V2_WIDTH, EPD_2IN13_V2_HEIGHT, 270, WHITE);
    Paint_SelectImage(BlackImage);
    Paint_SetMirroring(MIRROR_HORIZONTAL);
    Paint_Clear(WHITE);

    Paint_DrawIcon(89,36, ICON_LOGO, &FontIcon, BLACK, WHITE);
    Paint_DrawString_EN(29, 0, "Helcel", &Font45, WHITE, BLACK);
    Paint_SelectImage(BlackImage);
    EPD_2IN13_V2_Display(BlackImage);
    delay(100);
}

void Display::drawTime(strDateTime* dt){
    Paint_ClearWindows(170, EPD_2IN13_V2_WIDTH - Font24.Height, 170 + Font24.Width * 4.5, EPD_2IN13_V2_WIDTH, WHITE);
    if (!dt->valid) return;
    Paint_DrawTime(170, 98, dt, &Font24, WHITE, BLACK);
    EPD_2IN13_V2_DisplayPart(BlackImage);
};

void Display::refresh(){
    Paint_Clear(WHITE);
    EPD_2IN13_V2_Init(EPD_2IN13_V2_FULL);
    EPD_2IN13_V2_DisplayPartBaseImage(BlackImage);
    EPD_2IN13_V2_Init(EPD_2IN13_V2_PART);
    Paint_SelectImage(BlackImage);
};

void Display::update(){
    Paint_ClearWindows(0, 0, EPD_2IN13_V2_HEIGHT, EPD_2IN13_V2_WIDTH - Font24.Height, WHITE);
    Paint_ClearWindows(0, EPD_2IN13_V2_WIDTH - Font24.Height, 170, EPD_2IN13_V2_WIDTH, WHITE);
    if(views && view_curr >=0)
      views[view_curr]->drawDisplay();
    EPD_2IN13_V2_DisplayPart(BlackImage);
}

void Display::setViews(DisplayWrapper** view_tab, int8_t size){
    view_count = size;
    views = view_tab;
};

void Display::nextView(){ view_curr++; view_curr = view_curr%view_count;}
void Display::prevView(){ view_curr--; view_curr = (view_curr+view_count) % view_count;}

 

ICON_tpe getWeatherIcon(int id){
  if(id>=200 && id<300){ //Thunder
    return ICON_CLOUD_THUNDER;
  }else if(id>=300 && id<400){//Drizzle
    return ICON_CLOUD_RAIN;
  }else if(id>=500 && id<600){//Rain
    return ICON_CLOUD_RAIN;
  }else if(id>=600 && id<700){//Snow
    return ICON_CLOUD_SNOW;
  }else if(id>=700 && id<800){//Warning
    return ICON_WARN;
  }else if(id>=800 && id<900){//Clear & Cloudy
    if(id==800)      return ICON_SUN;
    else if(id==801) return ICON_CLOUD_SUN;
    else             return ICON_CLOUD;
  }else{
    return ICON_WARN;
  }
}

void WeatherDisplay::drawDisplay() {
    if(cowm == NULL || !cowm->valid_weather) return;
    JsonObject weather_0 = cowm->weather["weather"][0];
    JsonObject weather_main = cowm->weather["main"];
    JsonObject weather_wind = cowm->weather["wind"];
    JsonObject weather_sys = cowm->weather["sys"];
    if(!weather_0 || !weather_main || !weather_sys || !weather_wind) return;
    Paint_DrawIcon(0,8, getWeatherIcon(weather_0["id"]), &FontIcon, BLACK, WHITE);
    Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,weather_0["description"], &Font16, WHITE, BLACK);
    Paint_DrawFltUnit(76,0,weather_main["temp"], "C", &Font24, BLACK, WHITE);
    Paint_DrawFltUnit(186,6,weather_main["feels_like"], "C", &Font12, BLACK, WHITE);
    Paint_DrawIntUnit(76,24,weather_main["pressure"], "hPa", &Font24, BLACK, WHITE);
    Paint_DrawIntUnit(76,48,weather_main["humidity"], "%", &Font24, BLACK, WHITE);
    Paint_DrawFltUnit(76,72,weather_wind["speed"], "m/s", &Font24, BLACK, WHITE);
    strDateTime sunrisedt = NTPtime::ConvertUnixTimestamp(NTPtime::adjustTimeZone(weather_sys["sunrise"], 1.0, 1));
    strDateTime sunsetdt  = NTPtime::ConvertUnixTimestamp(NTPtime::adjustTimeZone(weather_sys["sunset"], 1.0, 1));
    Paint_DrawTime(20, 6, &sunrisedt, &Font12, WHITE, BLACK);
    Paint_DrawTime(20, 76, &sunsetdt, &Font12, WHITE, BLACK);
}

void TempDisplay::drawDisplay() { 
    Paint_DrawIcon(0,8, ICON_TEMPERATURE,&FontIcon, BLACK, WHITE);
    Paint_DrawFltUnit(76,0,sensor->temp, "C", &Font24, BLACK, WHITE);
    Paint_DrawFltUnit(186,6,sensor->heatidx, "C", &Font12, BLACK, WHITE);
    Paint_DrawFltUnit(76,24,sensor->pres, "hPa", &Font24, BLACK, WHITE);
    Paint_DrawFltUnit(76,48,sensor->hum, "%", &Font24, BLACK, WHITE);

    Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"nominal", &Font16, WHITE, BLACK);
}

void AirDisplay::drawDisplay() { 
    Paint_DrawIcon(0,8, ICON_POLUTION,&FontIcon, BLACK, WHITE);

    Paint_DrawIntUnit(76,0,sensor->voci, "voci", &Font24, BLACK, WHITE);

    Paint_DrawIntUnit(76,24+12*0,sensor->pmd.pm10_standard, "ug/m3", &Font12, BLACK, WHITE);
    Paint_DrawIntUnit(76,24+12*1,sensor->pmd.pm25_standard, "ug/m3", &Font12, BLACK, WHITE);
    Paint_DrawIntUnit(76,24+12*2,sensor->pmd.pm100_standard, "ug/m3", &Font12, BLACK, WHITE);

    Paint_DrawIntUnit(186,24+12*0,sensor->pmd.pm10_env, "ug/m3", &Font12, BLACK, WHITE);
    Paint_DrawIntUnit(186,24+12*1,sensor->pmd.pm25_env, "ug/m3", &Font12, BLACK, WHITE);
    Paint_DrawIntUnit(186,24+12*2,sensor->pmd.pm100_env, "ug/m3", &Font12, BLACK, WHITE);

//       particles_03um,      ///< 0.3um Particle Count
//       particles_05um,      ///< 0.5um Particle Count
//       particles_10um,      ///< 1.0um Particle Count
//       particles_25um,      ///< 2.5um Particle Count
//       particles_50um,      ///< 5.0um Particle Count
//       particles_100um;     ///< 10.0um Particle Count

  Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"nominal", &Font16, WHITE, BLACK);
    
}


void LightDisplay::drawDisplay() { 
    Paint_DrawIcon(0,8, ICON_BULB,&FontIcon, BLACK, WHITE); //TODO BULB

    Paint_DrawFltUnit(76,0,sensor->light_lux, "lux", &Font24, BLACK, WHITE);
    Paint_DrawIntUnit(76,24*1,sensor->uvi, "uvi", &Font24, BLACK, WHITE);
    Paint_DrawFltUnit(76,24*2,sensor->light, "l", &Font24, BLACK, WHITE);
    Paint_DrawIntUnit(76,24*3,sensor->light_vis, "vl", &Font24, BLACK, WHITE);

    Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"nominal", &Font16, WHITE, BLACK);
}


void MainDisplay::drawDisplay() { 
    Paint_DrawIcon(89,36, ICON_LOGO, &FontIcon, BLACK, WHITE);
    Paint_DrawString_EN(29, 0, "Helcel", &Font45, WHITE, BLACK);
    Paint_DrawString_EN(2,EPD_2IN13_V2_WIDTH - 4 - Font16.Height,"WESP-v0.1", &Font16, WHITE, BLACK);
}

void CustomDisplay::drawDisplay() { 
    Paint_DrawString_EN(60,0,"CUSTOM", &Font24, WHITE, BLACK);
}