#pragma once
#include "ise_display_definitions.hpp"

#include <ESPMegaDisplay.hpp>
#include <TimeStructure.hpp>
#include <DigitalInputCard.hpp>
#include <DigitalOutputCard.hpp>
#include <ClimateCard.hpp>
#include <RemoteVariable.hpp>

/**
 * @brief A class for controlling the ESPMegaDisplay.
 * 
 * Made for ISE building 2 room 303/2.
 */



struct lightPosition {
    uint8_t row;
    uint8_t column;
};

class ISEDisplay : public ESPMegaDisplay { 
    public:
        ISEDisplay(HardwareSerial* adapter, const uint8_t *light_array, uint8_t row, uint8_t column);
        void begin(DigitalInputCard* inputCard, DigitalOutputCard* outputCard, ClimateCard* climateCard, RemoteVariable* pm_switch, RemoteVariable* pm_fan_speed);
        void updateLightGroupStatePageDashboard();
        void updateLightGroupStatePageStandby();
        void updateAirPurifierState();
        void updateAirPurifierStateStandby();
        void updateDateTimeText(rtctime_t time);
        void updateWeather(char *weather_string);
        void updateTempOutside(float temp_outside);
        void updatePMoutside(u_int16_t pm25_outside);
        void updatePMinside(u_int16_t pm25_inside);
        void loop();

        

    private:
        
        void handleTouch(uint8_t page, uint8_t component, uint8_t touch_type);
        void handlePWMChange(uint8_t pin, bool state, uint16_t value);
        void handleACChange(uint8_t mode, uint8_t fan_speed, uint8_t temperature);
        void setPMstate(bool is_pm_on, uint8_t pm_fan_speed);
        void setACstate(uint8_t ac_fan_speed, uint8_t ac_mode, uint8_t ac_temperature);
        void setLightLevel(uint8_t row, uint8_t level);
        u_int8_t getLightLevel(uint8_t row);
        lightPosition getRowCol(uint8_t pin);
        
        DigitalInputCard* inputCard;
        DigitalOutputCard *outputCard;
        ClimateCard *climateCard;

        const uint8_t *light_array;
        uint8_t row;
        uint8_t column;

        RemoteVariable *pm_switch;
        RemoteVariable *remote_pm_fan_speed;
        uint8_t outputCallbackHandle;
        uint8_t climateCallbackHandle;
        uint8_t user_mode;
        uint8_t ac_mode;
        uint8_t ac_fan_speed;
        uint8_t ac_temperature;
        uint8_t pm_fan_speed;
        uint8_t time_since_last_screen_update;
        u_int8_t lightLevelRow1;
        u_int8_t lightLevelRow2;
        u_int8_t lightLevelRow3;
        u_int8_t lightLevelRow4;
        
        void updateACState();
        void updateuserACmode();
        void updateACfanSpeed();
        bool calculateLightGroupState();
        void toggleLightGroupState();
        void toggleLightGroupStateStandby();
        void toggleLightIndividual(uint8_t row);
        void toggleSliderLight(uint8_t row,uint8_t lightLevel);
        void togglePM();
        void togglePMStandby();
        void toggleAC();
        void toggleACStandby();
        void changeUserACmode();
        
};