/*
  FujitsuAC - ESP32 libary for controlling FujitsuAC through MQTT
  Copyright (c) 2025 Benas Ragauskas. All rights reserved.
  
  Project home: https://github.com/Benas09/FujitsuAC
*/

#pragma once

#include "Config.h"

namespace FujitsuAC {

    Config::Config(const char *version, int ledWPin, int ledRPin) : 
    	_preferences(),
    	_version(version),
    	_ledWPin(ledWPin),
    	_ledRPin(ledRPin)
	{
    }

	Config::~Config() {
	    _preferences.end();
	}

    void Config::generateUniqueId() {
        char buf[13];
        snprintf(buf, sizeof(buf), "%012llX", ESP.getEfuseMac());

        String uniqueId = buf;
        uniqueId.toLowerCase();

        _uniqueId = uniqueId;
    }

    void Config::load() {
        this->generateUniqueId();
        
        _preferences.begin("fujitsu_ac", false);

        _wifiSsid = _preferences.getString("wifi-ssid", "");
        _wifiPw = _preferences.getString("wifi-pw", "");
        _mqttIp = _preferences.getString("mqtt-ip", "");
        _mqttPort = _preferences.getString("mqtt-port", "");
        _mqttUser = _preferences.getString("mqtt-user", "");
        _mqttPw = _preferences.getString("mqtt-pw", "");
        _deviceName = _preferences.getString("device-name", "");
        _otaPw = _preferences.getString("ota-pw", "");
        _protocol = _preferences.getString("protocol", "");
        _ledsOn = _preferences.getBool("leds-on", true);
        _wifiSleepEnabled = _preferences.getBool("wifi-sleep", true);
        _lowCpuSpeedEnabled = _preferences.getBool("low-cpu-speed", true);

        this->setLowCpuSpeedEnabled(_lowCpuSpeedEnabled);
    }

    void Config::clear() {
	    _preferences.clear(); 
	}

	bool Config::isEmpty() {
		return _wifiSsid == "";
	}

	void Config::setValue(const char* key, String value) {
		_preferences.putString(key, value);
	}

    void Config::initLeds() {
        if (_ledRPin > 0) {
            ledcAttach(_ledRPin, 12000, 10);
            this->toggleRLed(true);
        }

        if (_ledWPin > 0) {
            ledcAttach(_ledWPin, 12000, 10);
            this->toggleWLed(false);
        }
    }

    void Config::toggleWLed(bool status) {
        if (_ledWPin > 0) {
            ledcWrite(_ledWPin, status ? 1020 : 1023);
        }
    }

    void Config::toggleRLed(bool status) {
        if (_ledRPin > 0) {
            ledcWrite(_ledRPin, status ? 1020 : 1023);
        }
    }

    void Config::setLedsStatus(bool status) {
        if (_ledsOn != status) {
            _ledsOn = status;
            _preferences.putBool("leds-on", status);
        }

        this->toggleWLed(status);
        this->toggleRLed(status);
    }

    bool Config::isLedsOn() {
        return _ledsOn;
    }

    void Config::setWifiSleepEnabled(bool status) 
    {
        if (_wifiSleepEnabled != status) {
            _wifiSleepEnabled = status;
            _preferences.putBool("wifi-sleep", status);
        }

        WiFi.setSleep(status);
    }

    bool Config::isWifiSleepEnabled() {
        return _wifiSleepEnabled;
    }

    void Config::setLowCpuSpeedEnabled(bool status) 
    {
        if (_lowCpuSpeedEnabled != status) {
            _lowCpuSpeedEnabled = status;
            _preferences.putBool("low-cpu-speed", status);
        }

        uint32_t minFreq = 80;
        uint32_t maxFreq = 240;

        String chip = ESP.getChipModel();

        if (chip == "ESP32-C3") {
            minFreq = 40;
            maxFreq = 160;
        }

        setCpuFrequencyMhz(status ? minFreq : maxFreq);
    }

    bool Config::isLowCpuSpeedEnabled() {
        return _lowCpuSpeedEnabled;
    }
}