/* -*- Mode: C++; indent-tabs-mode: nil; tab-width: 4 -*-
 * -*- coding: utf-8 -*-
 *
 * Copyright (C) 2025 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
 * 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 __PANEL_CONFIG_H__
#define __PANEL_CONFIG_H__

#include <QObject>
#include <QDBusContext>
#include <QtRemoteObjects/qremoteobjectnode.h>
#include "general_config_define.h"
#include "rep_remote_config_replica.h"

class RemoteConfigReplica;

#define PEEK_SERVER_STATE_NO_RET() \
if (!isConnected()) {    \
qDebug()<<"disconnect";\
        if (calledFromDBus())  { \
                    sendErrorReply(QDBusError::NoServer, "can't connect to panel"); \
        }\
        return;\
}

#define PEEK_SERVER_STATE_RET_STRING() \
if (!isConnected()) {    \
     if (calledFromDBus())   \
        sendErrorReply(QDBusError::NoServer, "can't connect to panel"); \
        return ""; \
}

#define PEEK_SERVER_STATE_RET_INT() \
if (!isConnected()) {    \
        if (calledFromDBus())   \
        sendErrorReply(QDBusError::NoServer, "can't connect to panel"); \
        return 999; \
}

#define PEEK_SERVER_STATE_RET_BOOL() \
if (!isConnected()) {    \
     if (calledFromDBus())   \
        sendErrorReply(QDBusError::NoServer, "can't connect to panel"); \
        return false; \
}

#define LOG_CHANGE(ENUM_TYPE, NAME) \
qDebug() << __FUNCTION__ << NAME << "changed to:" << NAME

class ConfigUkPanel: public QObject, QDBusContext
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "org.ukui.Framework.UI.Panel")
public:

    explicit ConfigUkPanel(QObject *parent = nullptr);
    ~ConfigUkPanel();
public Q_SLOTS:
    /**
     * @brief GetMergeIcons
     * @return
     */
    int GetMergeIconsType() const;

    /**
     * @brief SetMergeIcons
     * @return
     */
    bool SetMergedIconType(int type);

    /**
     * @brief GetPanelLocation
     * @return
     */
    int GetPanelPosition() const;

    /**
     * @brief SetPanelLocation
     */
    bool SetPanelPosition(int location);

    /**
     * @brief GetPanelSize
     * @return
     */
    int GetPanelDimensions() const;

    /**
     * @brief SetPanelSize
     * @param size
     */
    bool SetPanelDimensions(int size);

    /**
     * @brief GetAutoHide
     * @return
     */
    bool IsAutoHidden() const;

    /**
     * @brief SetAutoHide
     */
    bool SetAutoHidden(bool isHide);


    /**
     * @brief GetPanelLock
     * @return
     */
    bool IsPanelLocked() const;

    /**
     * @brief SetPanelLock
     * @param isLock
     */
    bool SetPanelLocked(bool isLock);

    /**
     * @brief GetShowPanelOnAllScreens
     * @return
     */
    bool IsShowPanelOnAllScreens() const;

    /**
     * @brief SetShowPanelOnAllScreens
     * @param isShow
     */
    bool SetShowPanelOnAllScreens(bool isShow);

    /**
     * @brief GetShowSystemTrayOnAllPanels
     */
    bool IsShowSystemTrayOnAllPanels() const;

    /**
     * @brief SetShowSystemTrayOnAllPanels
     * @param isShow
     */
    bool SetShowSystemTrayOnAllPanels(bool isShow);

    /**
     * @brief GetTaskBarIconsShowedOn
     * @return
     */
    int GetTaskBarIconDisplayLocation() const;

    /**
     * @brief SetTaskBarIconsShowedOn
     */
    bool SetTaskBarIconDisplayLocation(int showOn);

    bool IsShowTaskView() const;

    bool SetShowTaskView(bool isShow);

    bool IsShowSearch() const;

    bool SetShowSearch(bool isShow);
private Q_SLOTS:
    void initConnection();
Q_SIGNALS:
    void connectStateChanged(bool isSuccess);
    void mergeIconsChanged(int);
    void locationChanged(int);
    void sizeChanged(int);
    void taskBarIconsShowOnChanged(int);

    void autoHideChanged(bool);
    void lockChanged(bool);
    void showOnAllScreenChanged(bool);
    void systemTrayOnAllPanels(bool);

private:

    void connectToSource();
    /**
     * @brief connectSignal
     *
     */
    void connectSignal() const;
    void initEnum();
    void sourceStateChanged(QRemoteObjectReplica::State state, QRemoteObjectReplica::State oldState);
    bool isConnected() const;
    /**
     * @brief island
     * 三岛不支持：合并图标、调整任务栏位置、在所有任务栏显示托盘,现在没有接口获取三岛状态、先记录不实现.
     * @return
     */
    bool island() const;

    template<typename T>
    bool convertEnumValue(const QString &str, const QMetaEnum &meta, T &result) const;

    template<typename T>
    bool convertEnumValue(const int &value, const QMetaEnum &meta, T &result) const;

private Q_SLOTS:
    void onMergeIconsChanged(GeneralConfigDefine::MergeIcons mergeIcons);
    void onPanelSizePolicyChanged(GeneralConfigDefine::PanelSizePolicy sizePolicy);
    void onPanelAutoHideChanged(bool isAutoHide);
    void onPanelLockChanged(bool isLock);
    void onShowSystemTrayOnAllPanelsChanged(bool isAll);
    void onPanelLocationChanged(GeneralConfigDefine::PanelLocation location);
    void onShowPanelOnAllScreensChanged(bool isAll);
    void onTaskBarIconsShowedOnChanged(GeneralConfigDefine::TaskBarIconsShowedOn showPolicy);
    void onDisableWidgetsChanged(QStringList list);
private:
    QMetaEnum   m_mergeEnum;
    QMetaEnum   m_locationEnum;
    QMetaEnum   m_sizeEnum;
    QMetaEnum   m_taskBarShowOnEnum;
    const QString C_TASK_VIEW = "org.ukui.panel.taskView";
    const QString C_SEARCH_V = "org.ukui.panel.search";
    QRemoteObjectNode m_qroNode;
    RemoteConfigReplica *m_configReplica;
    QRemoteObjectReplica::State m_currentState;
    QRemoteObjectDynamicReplica *m_pIrep;

    // 基础配置项
    const char* c_mergeIcons = "mergeIcons";
    const char* c_panelLocation = "panelLocation";
    const char* c_panelSizePolicy = "panelSizePolicy";
    const char* c_allowCustomSize = "allowCustomSize";
    const char* c_panelAutoHide = "panelAutoHide";
    const char* c_panelLock = "panelLock";
    const char* c_panelWidgets = "panelWidgets";
    const char* c_disabledWidgets = "disabledWidgets";
    const char* c_trayIconsInhibited = "trayIconsInhibited";
    const char* c_taskBarIconsShowedOn = "taskBarIconsShowedOn";
    const char* c_panelType = "panelType";
    const char* c_showPanelOnAllScreens = "showPanelOnAllScreens";
    const char* c_showSystemTrayOnAllPanels = "showSystemTrayOnAllPanels";

    // 备用配置项
    const char* c_panelLocationNext = "panelLocationNext";
    const char* c_panelSizePolicyNext = "panelSizePolicyNext";
    const char* c_panelLockNext = "panelLockNext";
    const char* c_panelWidgetsNext = "panelWidgetsNext";
    const char* c_disabledWidgetsNext = "disabledWidgetsNext";

};


#endif //__PANEL_CONFIG_H__
