/*
 * Copyright 2023 KylinSoft Co., Ltd.
 *
 * This program is 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.
 *
 * This program is 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef UPMCPUCONTROL_H
#define UPMCPUCONTROL_H

#include <QObject>
#include <QVector>

#include "upmcpucoreinfo.h"
#include "upmcpuoccupy.h"
#include "../upmcustomhardwareconfig.h"

enum em_auto_switch_cpu_core_config
{
    cpu_sampling_interval = 0,
    offline_cpu_occupy,
    online_cpu_occupy,
    reserve_core_num,
    max_auto_switch_cpu_core_config
};

typedef struct stu_auto_switch_cpu_core_config
{
    int cpuSamplingInterval;
    int offlineCpuOccupy;
    int onlineCpuOccupy;
    int reserveCoreNum;
    bool configActive;
}AutoSwitchCpuCoreConfig;

class UpmCpuControl : public QObject
{
    Q_OBJECT
public:
    explicit UpmCpuControl(QObject *parent = nullptr);

    int getCpuCoreNum();
    void setCorePolicy(int cpuCoreId, int cpuPolicy);
    QString getCpuCorePolicy(int cpuCoreId);
    void setCoreMaxFreq(int cpuCoreId, int cpuFrequency);
    int getDefaultCoreMaxFreq(int cpuCoreId);
    void setCoreDefaultMaxFreq(int cpuCoreId);

    void setAllCorePolicy(int cpuPolicy);
    void setAllCoreMaxFreq(int cpuFrequency);
    void setAllCoreDefaultMaxFreq();

    int getCpuCoreOnlineState(int cpuCoreId);
    void setCpuCoreOnlineState(int cpuCoreId, int online);

    void systemIdleReserveCpu0(bool systmIdle);
    void enableBalanceModeFrequency(bool enable);
    void enableSystemIdleReserveCpu0(bool enable, int cpuPolicy);
    void enableAutoSwitchCpuCore(bool enable, int cpuPolicy);

private:
    int m_cpuCoreNum;
    int m_cpuPolicy;
    QVector<UpmCpuCoreInfo *> m_allCpuCoreInfo;
    int m_balanceMaxCpuFrequency = 0;
    bool m_balanceModeSetMaxFreq = false;

    bool m_idleReserveCpu0[max_policy];

    UpmCpuOccupy *m_cpuOccupy;
    AutoSwitchCpuCoreConfig m_configs[max_policy];
    int m_maxCpuOnlineCore;
    int m_cpuOnlineCore;

    void initSystemIdleReserveCpu0();
    void initBalanceModeLimitCpuFreq();
    void initAutoSwitchCpuCore();
    void initAutoSwitchCpuCoreConfig(int cpuPolicy);

private slots:
    void dealCpuSwitchSignal(bool);

signals:

};

#endif // UPMCPUCONTROL_H
