// SPDX-License-Identifier: GPL-3.0-or-later

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH
*
* This file is part of nymea-energy-plugin-nymea.
*
* nymea-energy-plugin-nymea.s free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nymea-energy-plugin-nymea.s distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nymea-energy-plugin-nymea. If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef CHARGINGCONFIGURATION_H
#define CHARGINGCONFIGURATION_H

#include <QTime>
#include <QDebug>
#include <QMetaObject>

#include <typeutils.h>

class ChargingConfiguration
{
    Q_GADGET
    Q_PROPERTY(QUuid evChargerId READ evChargerId WRITE setEvChargerId)
    Q_PROPERTY(QUuid assignedCarId READ assignedCarId WRITE setAssignedCarId USER true)
    Q_PROPERTY(ChargingMode chargingMode READ chargingMode WRITE setChargingMode)
    Q_PROPERTY(QDateTime endDateTime READ endDateTime WRITE setEndDateTime USER true)
    Q_PROPERTY(QList<int> repeatDays READ repeatDays WRITE setRepeatDays USER true)
    Q_PROPERTY(uint targetPercentage READ targetPercentage WRITE setTargetPercentage USER true)
    Q_PROPERTY(uint dailySpotMarketPercentage READ dailySpotMarketPercentage WRITE setDailySpotMarketPercentage USER true)
    Q_PROPERTY(bool lockOnUnplug READ lockOnUnplug WRITE setLockOnUnplug USER true)
    Q_PROPERTY(bool stopOnTargetPercentage READ stopOnTargetPercentage WRITE setStopOnTargetPercentage USER true)

public:
    enum ChargingMode {
        ChargingModeNormal = 0,
        ChargingModeEco,
        ChargingModeEcoWithTargetTime
    };
    Q_ENUM(ChargingMode)


    ChargingConfiguration(const ThingId &evChargerId = ThingId());

    ThingId evChargerId() const;
    void setEvChargerId(const ThingId &evChargerId);

    ThingId assignedCarId() const;
    void setAssignedCarId(const ThingId &assignedCarId);

    ChargingMode chargingMode() const;
    void setChargingMode(ChargingMode chargingMode);

    QDateTime endDateTime() const;
    void setEndDateTime(const QDateTime &endDateTime);

    QList<int> repeatDays() const;
    void setRepeatDays(const QList<int> &repeatDays);

    uint targetPercentage() const;
    void setTargetPercentage(uint targetPercentage);

    uint dailySpotMarketPercentage() const;
    void setDailySpotMarketPercentage(uint dailySpotMarketPercentage);

    bool lockOnUnplug() const;
    void setLockOnUnplug(bool lockOnUnplug);

    bool stopOnTargetPercentage() const;
    void setStopOnTargetPercentage(bool stopOnTargetPercentage);

    // Internal properties

    // For push notifications regarding this evcharger/car.
    // May be removed in the future and only sent out text ids which are to be translated in the app
    QLocale locale() const;
    void setLocale(const QLocale &locale);

    // For manual charging restore when moving back to normal mode
    bool manualChargingEnabled() const;
    void setManualChargingEnabled(bool manualChargingEnabled);

    double manualMaxChargingCurrent() const;
    void setManualMaxChargingCurrent(double manualMaxChargingCurrent);

    uint manualDesiredPhaseCount() const;
    void setManualDesiredPhaseCount(uint manualDesiredPhaseCount);

    QDateTime nextEndTime(const QDateTime &currentDateTime) const;

    bool operator==(const ChargingConfiguration &other) const;
    bool operator!=(const ChargingConfiguration &other) const;

private:
    ThingId m_evChargerId;
    ThingId m_assignedCarId;
    ChargingMode m_chargingMode = ChargingModeNormal;
    QDateTime m_endDateTime = QDateTime();
    QList<int> m_repeatDays;
    uint m_targetPercentage = 80;
    uint m_dailySpotMarketPercentage = 0;
    bool m_lockOnUnplug = false;
    bool m_stopOnTargetPercentage = false;
    QLocale m_locale;

    // Not expose to API with properties
    bool m_manualChargingEnabled = false;
    double m_manualMaxChargingCurrent = 6;
    uint m_manualDesiredPhaseCount = 3;
};

QDebug operator<<(QDebug debug, const ChargingConfiguration &chargingConfiguration);

class ChargingConfigurations: public QList<ChargingConfiguration>
{
    Q_GADGET
    Q_PROPERTY(int count READ count)
public:
    ChargingConfigurations();
    ChargingConfigurations(const QList<ChargingConfiguration> &other);
    ChargingConfigurations(std::initializer_list<ChargingConfiguration> args):QList(args) {}
    Q_INVOKABLE QVariant get(int index) const;
    Q_INVOKABLE void put(const QVariant &variant);

};

Q_DECLARE_METATYPE(ChargingConfigurations)


#endif // CHARGINGCONFIGURATION_H
