// 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>

class QWindow;

namespace QtWaylandClient
{

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

class UkuiShellSurfaceV1 : public QWaylandShellSurface, public QtWayland::xdg_surface, public QtWayland::ukui_surface_v1
{
    Q_OBJECT
public:
    UkuiShellSurfaceV1(UkuiShell *shell, ::xdg_surface *xgdSurface, ::ukui_surface_v1 *ukuiSurface, ::ukui_decoration_v1 *decoration, QWaylandWindow *window);
    ~UkuiShellSurfaceV1() 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) override;
    void setSizeHints();
    bool requestActivate() override;
    bool requestActivateOnShow() 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;
    std::any surfaceRole() const override;
    // --------- QWaylandShellSurface interface end------------//

    void setSkipTaskbar(bool skip);
    void setSkipSwitcher(bool skip);
    void setNoTitlebar(bool noTitlebar);
    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_v1_position(int32_t x, int32_t y) override;
    void ukui_surface_v1_surface_state(uint32_t flags) 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;
    QtWayland::ukui_decoration_v1 *m_ukuiDecoration = 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
