// SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
// SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <Plasma/Containment>
#include <QSortFilterProxyModel>
#include <QVariant>

#include "applicationlistmodel.h"
#include "applicationusagemodel.h"
#include "delegatetoucharea.h"
#include "favouritesmodel.h"
#include "folioapplication.h"
#include "folioapplicationfolder.h"
#include "foliodelegate.h"
#include "foliosettings.h"
#include "foliowidget.h"
#include "homescreenstate.h"
#include "pagelistmodel.h"
#include "pagemodel.h"
#include "widgetcontainer.h"
#include "widgetsmanager.h"

class FolioSettings;
class PageListModel;
class WidgetsManager;
class HomeScreenState;
class FavouritesModel;
class ApplicationListModel;
class ApplicationListSearchModel;
class ApplicationUsageModel;
class ApplicationUsageStore;

class HomeScreen : public Plasma::Containment
{
    Q_OBJECT
    QML_ELEMENT
    QML_UNCREATABLE("HomeScreen is provided by the folio containment")

    Q_PROPERTY(FolioSettings *FolioSettings READ folioSettings CONSTANT)
    Q_PROPERTY(HomeScreenState *HomeScreenState READ homeScreenState CONSTANT)
    Q_PROPERTY(WidgetsManager *WidgetsManager READ widgetsManager CONSTANT)
    Q_PROPERTY(ApplicationListModel *ApplicationListModel READ applicationListModel CONSTANT)
    Q_PROPERTY(ApplicationListSearchModel *ApplicationListSearchModel READ applicationListSearchModel CONSTANT)
    Q_PROPERTY(ApplicationUsageModel *RecentApplicationsModel READ recentApplicationsModel CONSTANT)
    Q_PROPERTY(ApplicationUsageModel *MostUsedApplicationsModel READ mostUsedApplicationsModel CONSTANT)
    Q_PROPERTY(FavouritesModel *FavouritesModel READ favouritesModel CONSTANT)
    Q_PROPERTY(PageListModel *PageListModel READ pageListModel CONSTANT)
    Q_PROPERTY(bool overviewActive READ overviewActive NOTIFY overviewActiveChanged)

public:
    HomeScreen(QObject *parent, const KPluginMetaData &data, const QVariantList &args);
    ~HomeScreen() override;

    void configChanged() override;

    Q_INVOKABLE void triggerOverview();
    Q_INVOKABLE void triggerMinimizeAll() const;
    Q_INVOKABLE void activateVirtualDesktop(const QVariant &desktop) const;
    Q_INVOKABLE void createVirtualDesktop() const;
    Q_INVOKABLE void removeLastVirtualDesktop() const;
    Q_INVOKABLE void emptyTrash() const;

    FolioSettings *folioSettings();
    HomeScreenState *homeScreenState();
    WidgetsManager *widgetsManager();
    ApplicationListModel *applicationListModel();
    ApplicationListSearchModel *applicationListSearchModel();
    ApplicationUsageModel *recentApplicationsModel();
    ApplicationUsageModel *mostUsedApplicationsModel();
    FavouritesModel *favouritesModel();
    PageListModel *pageListModel();
    bool overviewActive() const;

Q_SIGNALS:
    void overviewActiveChanged();
    void showingDesktopChanged(bool showingDesktop);

private Q_SLOTS:
    void onOverviewEffectsChanged(const QString &interface, const QVariantMap &changed, const QStringList &invalidated);
    void onAppletAdded(Plasma::Applet *applet, const QRectF &geometryHint);
    void onAppletAboutToBeRemoved(Plasma::Applet *applet);

private:
    void setOverviewActive(bool overviewActive);
    void updateOverviewActive();

    FolioSettings *m_folioSettings{nullptr};
    HomeScreenState *m_homeScreenState{nullptr};
    WidgetsManager *m_widgetsManager{nullptr};
    ApplicationListModel *m_applicationListModel{nullptr};
    ApplicationListSearchModel *m_applicationListSearchModel{nullptr};
    ApplicationUsageStore *m_applicationUsageStore{nullptr};
    ApplicationUsageModel *m_recentApplicationsModel{nullptr};
    ApplicationUsageModel *m_mostUsedApplicationsModel{nullptr};
    FavouritesModel *m_favouritesModel{nullptr};
    PageListModel *m_pageListModel{nullptr};
    bool m_overviewActive{false};
};
