/* -*- 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
 * 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 MAINWIDGET_H
#define MAINWIDGET_H

#include <QString>
#include <list>

//#include "IAudioMainWidow.h"
#include "VersionStrategy.h"
#include "Util.h"
#include "DBusClient.h"

using namespace UkuiAudioFramwork;

#define MAX_INDEX 4294967295

class MainWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MainWidget(QWidget* = nullptr);
    ~MainWidget() {}

private Q_SLOTS:
    void volumeChangedSlots(int, int, const QDBusVariant&);

    void muteChangedSlots(int, int, bool);

    void deviceChangedSlots(int, const QString&, const QString&);

    void deviceAdjustSlots(int);

    void settingsChangedSlots(const QString&, const QDBusVariant&);

    void addStreamSlots(int, int, int, bool, const QString&, const QString&);

    void removeStreamSlots(int, int);

private:
    DesktopEnvironmentVersion getDesktopEnvironmentVersion();

    int getVolume(int, int);

    void setVolume(int, int, int);

    double getBalance(int) const;

    void setBalance(int, double);

    bool getMute(int, int);

    void setMute(int, int, bool);

    void setEnabled(const QString&, const QString&, bool);

    bool isEnabled(const QString&, const QString&) const;

    ClientDefaultDeviceInfo getDefaultDevice(int, int);

    QList<ClientDeviceInfo> getAvailablePortList(const NodeType&);

    QList<StreamInfo> getSinkInputList() const;

    QList<StreamInfo> getSourceOutputList() const;

    QList<ClientStreamMediaInfo> getStreamMediaList() const;

    QList<ClientDefaultDeviceInfo> getSinkList() const;

    QList<ClientDefaultDeviceInfo> getSourceList() const;

    void updateAppDeviceList(QList<ClientDeviceInfo>&, const QList<ClientDefaultDeviceInfo>&);

    void setDefaultDevice(int, int, const QString&, const QString&);

    void setValue(const QString&, const QVariant&);

    QVariant getValue(const QString&) const;

    QList<ClientSoundThemeInfo> getSoundThemeList();

    QList<ClientSoundEffectFileInfo> getSoundEffectFileList(const QString&);

    void initUi();

    void initSystemVolumeSlider();

    void initSystemDeviceList(const NodeType&);

    void initCurrentDefaultDevice(const NodeType&);

    void initDeviceManagerList(const NodeType&);

    void initAppManagerList();

    void initAppCurrentDefaultDevice(int, const NodeType&, int);

    void initSwitchButtonStatus();

    void initSoundThemeList();

    void initData();

    void initSlots();

    void initClientManager();

    void updateSoundTheme();

    bool getAutoPauseStatus() const;

    void setAutoPauseStatus(bool);

    bool getMultiAudioCombineStatus(const NodeType&) const;

    void updateAppWidgetDeviceList(const NodeType&);

    QString getAppStreamNameByIndex(const NodeType&, int);

    void addAppManagerItem(int, int, int, bool, const QString&, const QString&);

    void setAppManagerSliderRange(int, int);

    void setAppManagerSliderValue(int);

private:
    static constexpr std::string_view m_kylinSettingsSystemStr = "kylin-settings-system";

    DesktopEnvironmentVersion m_version = DesktopEnvironmentVersion::DESKTOP_ENVIRONMENT_VERSION_UKUI2;
    IAudioMainWindow* m_pMainWid = nullptr;

    std::list<AppManagerItem> m_appWidgetList {};
    std::shared_ptr<VersionStrategy> m_pStrategy = nullptr;
    std::shared_ptr<ClientManager> m_pClientManager = nullptr;

    std::unordered_map<SelectComboxType, std::tuple<std::string, std::string>> m_selectComboxKeys {
        {SelectComboxType::SELECT_COMBOX_SOUNDEFFECT, std::tuple(THEME_NAME_KEY, "setSoundThemeName")},
        {SelectComboxType::SELECT_COMBOX_VOLUMEADJUST, std::tuple(VOLUME_CHANGED_KEY, "setVolumeChangedName")},
        {SelectComboxType::SELECT_COMBOX_NOTIFICTION, std::tuple(NOTIFY_GENERAL_KEY, "setNotifyGeneralName")},
    };

    std::unordered_map<uint32_t, QStringList> m_appKeys {
        {MAX_INDEX, QStringList{std::initializer_list<QString>{"kylin-settings-system", QObject::tr("System Volume")}}},
    };

    std::unordered_map<TitleLabelType, TitleLabelItem*> m_titleLabelItemMap {};
    std::unordered_map<SwitchButtonType, ISwitchButtonItem*> m_switchBtnItemMap {};
    std::unordered_map<VolumeSliderType, IVolumeSliderItem*> m_volumeSliderItemMap {};
    std::unordered_map<SelectComboxType, ISelectComboxItem*> m_selectComboxItemMap {};
    std::unordered_map<DetailSettingType, IDetailSettingsItem*> m_detailSettingsItemMap {};
};

#endif // MAINWIDGET_H
