/*
 * Peony-Qt
 *
 * Copyright (C) 2023, KylinSoft Information Technology 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 <https://www.gnu.org/licenses/>.
 *
 * Authors: Yue Lan <lanyue@kylinos.cn>
 *
 */

#ifndef DESKTOPBACKGROUNDWINDOW_H
#define DESKTOPBACKGROUNDWINDOW_H

#include <QMainWindow>
#include <QGSettings>
#include "advanced-desktop-icon-view.h"
#include "windowmanager/windowmanager.h"

namespace KWayland {
namespace Client {
class PlasmaShellSurface;
}
}

namespace Peony {
class DesktopMenu;
}
class DesktopBackgroundWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit DesktopBackgroundWindow(QScreen *screen, int desktopWindowId, QWidget *parent = nullptr);
    ~DesktopBackgroundWindow() override;

    int id() const;

    QScreen *screen() const;
    AdvancedDesktopIconView *getIconView();
    void setId(int id);
    void setWindowId(kdk::WindowId id);
    kdk::WindowId getWindowId();

    bool event(QEvent *event) override;
    void invaidScreen();

Q_SIGNALS:
    void setDefaultZoomLevel(AdvancedDesktopIconView::ZoomLevel level);
    void setSortType(int sortType);
    void setSortOrder(int sortOrder);
    void updateWindow(const QRect &geometry);
    void markFilePos(QPoint pos);
    void clearOtherViewSelection();

public Q_SLOTS:
    void setWindowGeometry(const QRect &geometry);

protected Q_SLOTS:
    void updateWindowGeometry();

protected:
    void paintEvent(QPaintEvent *event) override;
    QPoint getRelativePos(const QPoint &pos);
    /**
     * 图片填充方式
     * @brief 从给定的图片中，截取一个与屏幕比例相同的矩形。(Rect,居中)。
     * @param pixmap 图片
     * @return
     */
    QRect getSourceRect(const QPixmap &pixmap);
    QRect getSourceRect(const QPixmap &pixmap, const QRect &screenGeometery);
    QRect getDestRect(const QPixmap &pixmap);

private:
    int m_id = -1;
    QScreen *m_screen = nullptr;
    QGSettings *m_panelSetting = nullptr;
    AdvancedDesktopIconView *m_desktopIconView = nullptr;

    KWayland::Client::PlasmaShellSurface *m_shellSurface = nullptr;
    Peony::DesktopMenu *m_menu = nullptr;
    kdk::WindowId m_windowId;
};

#endif // DESKTOPBACKGROUNDWINDOW_H
