/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2021 Marcus Britanicus (https://gitlab.com/marcusbritanicus)
 * Copyright (c) 2021 Abrar (https://gitlab.com/s96Abrar)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 **/

#pragma once

#include <QObject>

struct wl_registry;
struct wl_display;
struct wl_seat;
struct wl_shm;
// struct wl_compositor;

struct wl_registry_listener;

struct xdg_wm_base;
struct org_kde_kwin_idle;
struct zwlr_layer_shell_v1;
struct zwf_shell_manager_v2;
struct zwlr_screencopy_manager_v1;
struct zwlr_data_control_manager_v1;
struct zwlr_output_power_manager_v1;
struct zwlr_input_inhibit_manager_v1;
struct zwlr_foreign_toplevel_manager_v1;

namespace Wayland {
    class Registry;
    class XdgShell;
}

namespace WlrootsQt {
    class Idle;
    class LayerShell;
    class LayerSurface;
    class WindowManager;
    class ScreenCopyManager;
    class DataControlManager;
    class OutputPowerManager;
    class InputInhibitManager;
}

namespace Wayfire {
    class Shell;
}

class Wayland::Registry : public QObject {
    Q_OBJECT;

    public:
        enum ErrorType {
            EmptyShm,
            EmptyIdle,
            EmptySeat,
            EmptyXdgWmBase,
            EmptyCompositor,
            EmptyLayerShell,
            EmptyWayfireShell,
            EmptyToplevelManager,
            EmptyScreenCopyManager,
            EmptyDataControlManager,
            EmptyOutputPowerManager,
            EmptyInputInhibitManager,
        };
        Q_ENUM( ErrorType );

        Registry( wl_display *wlDisplay );
        ~Registry();

        void setup();

        operator wl_registry *();
        operator wl_registry *() const;

        wl_display *waylandDisplay();
        wl_seat *waylandSeat();
        wl_shm *waylandShm();

        /* Ready to use Wayland Classes */

        /**
         * LayerShell - Wlr Layer Shell protocol implementation
         */
        Wayland::XdgShell *xdgShell();

        /**
         * LayerShell - Wlr Layer Shell protocol implementation
         */
        WlrootsQt::LayerShell *layerShell();

        /**
         * WayfireShell - Wayfire Shell protocol implementation
         */
        Wayfire::Shell *wayfireShell();

        /**
         * InputInhibitManager - Wlr Input Inhibitor protocol implementation
         */
        WlrootsQt::InputInhibitManager *inputInhibitManager();

        /**
         * WindowManager - Wlr TopLevel Manager protocol implementation
         */
        WlrootsQt::WindowManager *windowManager();

        /**
         * ScreenCopyManager - ScreenCopy Manager protocol implementation
         */
        WlrootsQt::ScreenCopyManager *screenCopier();

        /**
         * DataControlManager - DataControl protocol implementation
         */
        WlrootsQt::DataControlManager *dataControlManager();

        /**
         * OutputPowerManager - Output Power Management protocol implementation
         */
        WlrootsQt::OutputPowerManager *outputPowerManager();

        /**
         * Idle - KDE's idle protocol implementation
         */
        WlrootsQt::Idle *idle();

    private:
        /** Raw C pointer to this class */
        wl_registry *mObj;

        /** wl_display object */
        wl_display *mWlDisplay;

        /** wl_seat object */
        wl_seat *mWlSeat;

        /** wl_shm object */
        wl_shm *mWlShm;

        /**
         * XdgShell objects;
         */
        xdg_wm_base *mXdgWmBase;
        XdgShell *mXdgShell;

        /**
         * Layer Shell Objects
         */
        zwlr_layer_shell_v1 *mWlrLayerShell;
        WlrootsQt::LayerShell *mLayerShell;

        /**
         * Wayfire Shell Objects
         */
        zwf_shell_manager_v2 *mWfShellMgr;
        Wayfire::Shell *mWayfireShell;

        /**
         * Wayfire Shell Objects
         */
        zwlr_input_inhibit_manager_v1 *mWlrInhibitMgr;
        WlrootsQt::InputInhibitManager *mInhibitManager;

        /**
         * TopLevel Objects
         */
        zwlr_foreign_toplevel_manager_v1 *mWlrWindowMgr;
        WlrootsQt::WindowManager *mWindowMgr;

        /**
         * ScreenCopyManager Objects
         */
        zwlr_screencopy_manager_v1 *mWlrScreenCopyMgr;
        WlrootsQt::ScreenCopyManager *mScreenCopyMgr;

        /**
         * DataControlManager Objects
         */
        zwlr_data_control_manager_v1 *mWlrDataControlMgr;
        WlrootsQt::DataControlManager *mDataControlMgr;

        /**
         * DataControlManager Objects
         */
        zwlr_output_power_manager_v1 *mWlrOutputPowerMgr;
        WlrootsQt::OutputPowerManager *mOutputPowerMgr;

        /**
         * Idle Objects
         */
        org_kde_kwin_idle *mKdeIdle;
        WlrootsQt::Idle *mIdle;

        static const wl_registry_listener mRegListener;
        static void globalAnnounce( void *data, wl_registry *registry, uint32_t name, const char *interface, uint32_t version );
        static void globalRemove( void *data, wl_registry *registry, uint32_t name );

        void handleAnnounce( uint32_t name, const char *interface, uint32_t version );
        void handleRemove( uint32_t name );

    signals:
        void errorOccured( ErrorType et );
};
