#include "sensor_luz.h"

int sensor_luz ::get_level_tildado()
{
    return this->tilt;
}

int mucha_luz =0;

int sensor_luz ::get_level_altura()
{
    return this->nivel;
}

void sensor_luz ::check_level(int sensorActual, int sensorAnterior, int sensorSiguiente, enum Level nivel)
{
    if(mucha_luz >= UMBRAL_GRANDE){
      this->nivel = (enum Level)(0);
    }
    else if (sensorSiguiente < UMBRAL && nivel != high && sensorActual > UMBRAL)
    {
        this->nivel = nivel;
        Serial.println("Mantener");
    }
    else if ((sensorAnterior > UMBRAL) && (sensorActual <= UMBRAL))
    {
        this->nivel = (enum Level)(nivel - 1);
        Serial.println("bajar nivel");
    }
    else
    {
        if (nivel != high)
        {
            this->nivel = (enum Level)(nivel + 1);
            Serial.println("Subir nivel");
        }
        else
        {
            this->nivel = nivel;
        }
    }
}

void sensor_luz ::check_tilt(int sensor, enum Tilt tilt, enum Level nivel)
{
    if (sensor < UMBRAL2 && tilt == low_tilt)
    {
        this->tilt = medium_tilt;
    }
    else if (sensor < UMBRAL2 && tilt == high_tilt)
    {
        this->tilt = medium_tilt;
    }
    else if (tilt == medium_tilt && sensor > UMBRAL && nivel <= medium)
    {
        this->tilt = low_tilt;
    }
    else if (tilt == medium_tilt && sensor > UMBRAL && nivel > medium)
    {
        this->tilt = high_tilt;
    }
    else
    {
    }
}

void sensor_luz ::read_ldr()
{
    this->sensorValue0 = analogRead(this->sensor0); // read the value from the sensor
    this->sensorValue1 = analogRead(this->sensor1); // read the value from the sensor
    this->sensorValue2 = analogRead(this->sensor2); // read the value from the sensor
    this->sensorValue3 = analogRead(this->sensor3); // read the value from the sensor
    mucha_luz = this->sensorValue0;
    // Serial.println("Sensor 1");
    // Serial.println(this->sensorValue0); // prints the values coming from the sensor on the screen
    // Serial.println("Sensor 2");
    // Serial.println(this->sensorValue1); // prints the values coming from the sensor on the screen
    // Serial.println("Sensor 3");
    // Serial.println(this->sensorValue2); // prints the values coming from the sensor on the screen
    // Serial.println("Sensor 4");
    // Serial.println(this->sensorValue3); // prints the values coming from the sensor on the screen
    Serial.println("Nivel");
    Serial.println(this->nivel);
    // Serial.println("Tilt");
    // Serial.println(this->tilt);

    switch (nivel)
    {
    case low:
        check_level(0, 0, this->sensorValue0, this->nivel);
        break;
    case medium_low:
        check_level(this->sensorValue0, 500, this->sensorValue1, this->nivel);
        break;
    case medium:
        check_level(this->sensorValue1, this->sensorValue0, this->sensorValue2, this->nivel);
        break;
    case medium_high:
        check_level(this->sensorValue2, this->sensorValue1, this->sensorValue3, this->nivel);
        break;
    case high:
        check_level(this->sensorValue3, this->sensorValue2, 0, this->nivel);
        break;
    }
    check_tilt(this->sensorValue0, this->tilt, this->nivel);
}