// SPDX-FileCopyrightText: 2024 KylinSoft Co., Ltd.
//
// SPDX-License-Identifier: GPL-1.0-or-later

#pragma once

#include "ukuishell.h"

#include <QtWaylandClient/private/qwaylandshellsurface_p.h>

#include <QSize>

namespace QtWaylandClient
{

class QWaylandShmBuffer;
class Popup;
class Toplevel;
class UkuiBlur;
class KdeSlide;
class XdgDecorationManagerV1;
class XdgDialogV1;
class QWaylandXdgToplevelIconManagerV1;

class UkuiShellSurface : public QWaylandShellSurface, public QtWayland::xdg_surface, public QtWayland::ukui_surface
{
    Q_OBJECT
public:
    UkuiShellSurface(UkuiShell *shell, ::xdg_surface *xgdSurface, ::ukui_surface *ukuiSurface, QWaylandWindow *window);
    ~UkuiShellSurface() override;

    UkuiShell *shell();
    Toplevel *toplevel();
    Popup *popup();

    // --------- QWaylandShellSurface interface begin------------//
    bool resize(QWaylandInputDevice *inputDevice, Qt::Edges edges) override;
    bool move(QWaylandInputDevice *inputDevice) override;
    bool showWindowMenu(QWaylandInputDevice *seat) override;
    void setTitle(const QString &title) override;
    void setAppId(const QString &appId) override;
    void setWindowFlags(Qt::WindowFlags flags) override;
    bool isExposed() const override;
    bool handleExpose(const QRegion &) override;
    void applyConfigure() override;
    bool wantsDecorations() const override;
    void propagateSizeHints() override;
    void setContentGeometry(const QRect &rect) override;
    void setWindowPosition(const QPoint &rect, bool need_commit) override;
    void setSizeHints();
    bool requestActivate() override;
    bool requestActivateOnShow() override;
    void setIcon(const QIcon &icon) override;
    // Not implemented yet
    void raise() override;
    void lower() override;
    void setContentOrientationMask(Qt::ScreenOrientations orientation) override;
    void sendProperty(const QString &name, const QVariant &value) override;
    // --------- QWaylandShellSurface interface end------------//

    void setSkipTaskbar(bool skip);
    void setSkipSwitcher(bool skip);
    void setNoTitlebar(bool noTitlebar);
    void setTheme(int theme);
    void setWindowRadius(int radius);
    void setBorderWidth(int width);
    void setBorderColor(QColor color);
    void setShadowRadius(int radius);
    void setShadowOffset(int offset);
    void setShadowColor(QColor color);
    void setWindowRole(int role);
    void setWindowIcon(const QString &icon);

protected:
    void requestWindowStates(Qt::WindowStates states) override;
    void xdg_surface_configure(uint32_t serial) override;
    void ukui_surface_position(int32_t x, int32_t y) override;

protected:
    bool eventFilter(QObject *watched, QEvent *event) override;

private:
    void initWindowByProperties();
    void slotWindowPropertyChanged(const char *property_name);

    void setToplevel();
    void setPopup(QWaylandWindow *parent);
    void setGrabPopup(QWaylandWindow *parent, QWaylandInputDevice *device, int serial);

    UkuiShell *m_shell = nullptr;
    QWaylandWindow *m_window = nullptr;
    Toplevel *m_toplevel = nullptr;
    Popup *m_popup = nullptr;
    UkuiBlur *m_blur = nullptr;
    KdeSlide *m_slide = nullptr;

    bool m_configured = false;
    QRegion m_exposeRegion;
    uint m_pendingConfigureSerial = 0;
    uint m_appliedConfigureSerial = 0;

    QString m_appId;
    QString m_activationToken;

    bool m_pendingPosition = false;
    QPoint m_surfacePosition;

    bool m_noTitlebar = false;
};

} // namespace QtWaylandClient
