/*
    KWin - the KDE window manager
    This file is part of the KDE project.

    SPDX-FileCopyrightText: 2020 Méven Car <meven.car@enioka.com>

    SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kwin_wayland_test.h"
#include "abstract_client.h"
#include "abstract_wayland_output.h"
#include "deleted.h"
#include "platform.h"
#include "screens.h"
#include "wayland_server.h"
#include "workspace.h"

#include <KWayland/Client/output.h>
#include <KWayland/Client/server_decoration.h>
#include <KWayland/Client/surface.h>

#include <KWaylandServer/outputconfiguration_v2_interface.h>
#include <KWaylandServer/outputdevice_v2_interface.h>
#include <KWaylandServer/outputmanagement_v2_interface.h>

#include <KWaylandServer/display.h>

using namespace KWin;
using namespace KWayland::Client;

static const QString s_socketName = QStringLiteral("wayland_test_kwin_outputmanagement-0");

class TestOutputManagement : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();
    void init();
    void cleanup();

    void testOutputDeviceDisabled();
    void testOutputDeviceRemoved();
};

void TestOutputManagement::initTestCase()
{
    qRegisterMetaType<KWin::Deleted*>();
    qRegisterMetaType<KWin::AbstractClient*>();
    qRegisterMetaType<KWin::AbstractOutput*>();
    qRegisterMetaType<AbstractOutput *>();
    qRegisterMetaType<KWin::AbstractOutput*>("AbstractOutput *");
    qRegisterMetaType<KWayland::Client::Output*>();

    QSignalSpy applicationStartedSpy(kwinApp(), &Application::started);
    QVERIFY(applicationStartedSpy.isValid());
    kwinApp()->platform()->setInitialWindowSize(QSize(1280, 1024));
    QVERIFY(waylandServer()->init(s_socketName));
    QMetaObject::invokeMethod(kwinApp()->platform(), "setVirtualOutputs", Qt::DirectConnection, Q_ARG(int, 2));

    kwinApp()->start();
    QVERIFY(applicationStartedSpy.wait());
    const auto outputs = kwinApp()->platform()->enabledOutputs();
    QCOMPARE(outputs.count(), 2);
    QCOMPARE(outputs[0]->geometry(), QRect(0, 0, 1280, 1024));
    QCOMPARE(outputs[1]->geometry(), QRect(1280, 0, 1280, 1024));
    Test::initWaylandWorkspace();
}

void TestOutputManagement::init()
{
    QVERIFY(Test::setupWaylandConnection(Test::AdditionalWaylandInterface::OutputManagementV2 |
                                         Test::AdditionalWaylandInterface::OutputDeviceV2));

    workspace()->setActiveOutput(QPoint(640, 512));
    //put mouse in the middle of screen one
    KWin::Cursors::self()->mouse()->setPos(QPoint(640, 512));
}

void TestOutputManagement::cleanup()
{
    Test::destroyWaylandConnection();
}

void TestOutputManagement::testOutputDeviceDisabled()
{
    // This tests checks that OutputConfiguration::apply aka Platform::requestOutputsChange works as expected
    // when disabling and enabling virtual OutputDevice

    QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
    QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
    auto size = QSize(200,200);

    QSignalSpy outputEnteredSpy(surface.data(), &KWayland::Client::Surface::outputEntered);
    QSignalSpy outputLeftSpy(surface.data(), &KWayland::Client::Surface::outputLeft);

    QSignalSpy outputEnabledSpy(kwinApp()->platform(), &Platform::outputEnabled);
    QSignalSpy outputDisabledSpy(kwinApp()->platform(), &Platform::outputDisabled);

    auto c = Test::renderAndWaitForShown(surface.data(), size, Qt::blue);
    //move to be in the first screen
    c->moveResize(QRect(QPoint(100,100), size));
    //we don't don't know where the compositor first placed this window,
    //this might fire, it might not
    outputEnteredSpy.wait(5);
    outputEnteredSpy.clear();
    QCOMPARE(waylandServer()->display()->outputs().count(), 2);

    QCOMPARE(surface->outputs().count(), 1);
    Output *firstOutput = surface->outputs().first();
    QCOMPARE(firstOutput->globalPosition(), QPoint(0,0));
    QSignalSpy modesChangedSpy(firstOutput, &Output::modeChanged);

    QSignalSpy screenChangedSpy(screens(), &KWin::Screens::changed);

    Test::WaylandOutputManagementV2 *outManagement = Test::waylandOutputManagementV2();

    auto outputDevices = Test::waylandOutputDevicesV2();
    QCOMPARE(outputDevices.count(), 2);

    Test::WaylandOutputDeviceV2 *device = outputDevices.first();
    QCOMPARE(device->enabled(), true);
    QSignalSpy outputDeviceEnabledChangedSpy(device, &Test::WaylandOutputDeviceV2::enabledChanged);
    Test::WaylandOutputConfigurationV2 *config;

    // Disables an output
    config = outManagement->createConfiguration();
    QSignalSpy configAppliedSpy(config, &Test::WaylandOutputConfigurationV2::applied);
    config->enable(device->object(), false);
    config->apply();
    QVERIFY(configAppliedSpy.wait());

    QCOMPARE(outputDeviceEnabledChangedSpy.count(), 1);
    QCOMPARE(device->enabled(), false);
    QCOMPARE(screenChangedSpy.count(), 3);
    QCOMPARE(outputLeftSpy.count(), 1);
    QCOMPARE(outputEnteredSpy.count(), 1); // surface was moved to other screen
    QCOMPARE(surface->outputs().count(), 1);
    QCOMPARE(screens()->count(), 1);
    QCOMPARE(modesChangedSpy.count(), 0);
    QCOMPARE(outputEnabledSpy.count(), 0);
    QCOMPARE(outputDisabledSpy.count(), 1);

    screenChangedSpy.clear();
    outputLeftSpy.clear();
    outputEnteredSpy.clear();
    outputDeviceEnabledChangedSpy.clear();
    outputEnabledSpy.clear();
    outputDisabledSpy.clear();

    // Enable the disabled output
    config = outManagement->createConfiguration();
    QSignalSpy configAppliedSpy2(config, &Test::WaylandOutputConfigurationV2::applied);
    config->enable(device->object(), true);
    config->apply();
    QVERIFY(configAppliedSpy2.wait());

    QVERIFY(outputEnteredSpy.wait());

    QCOMPARE(outputDeviceEnabledChangedSpy.count(), 1);
    QCOMPARE(device->enabled(), true);
    QCOMPARE(screenChangedSpy.count(), 3);
    QCOMPARE(outputLeftSpy.count(), 1);
    QCOMPARE(outputEnteredSpy.count(), 1); // surface moved back to first screen
    QCOMPARE(surface->outputs().count(), 1);
    QCOMPARE(screens()->count(), 2);
    QCOMPARE(modesChangedSpy.count(), 0);
    QCOMPARE(outputEnabledSpy.count(), 1);
    QCOMPARE(outputDisabledSpy.count(), 0);
}

void TestOutputManagement::testOutputDeviceRemoved()
{
    // This tests checks that OutputConfiguration::apply aka Platform::requestOutputsChange works as expected
    // when removing a virtual OutputDevice

    QScopedPointer<KWayland::Client::Surface> surface(Test::createSurface());
    QScopedPointer<Test::XdgToplevel> shellSurface(Test::createXdgToplevelSurface(surface.data()));
    auto size = QSize(200,200);

    QSignalSpy outputEnteredSpy(surface.data(), &KWayland::Client::Surface::outputEntered);
    QSignalSpy outputLeftSpy(surface.data(), &KWayland::Client::Surface::outputLeft);

    QSignalSpy outputEnabledSpy(kwinApp()->platform(), &Platform::outputEnabled);
    QSignalSpy outputDisabledSpy(kwinApp()->platform(), &Platform::outputDisabled);
    QSignalSpy outputRemovedSpy(kwinApp()->platform(), &Platform::outputRemoved);

    auto c = Test::renderAndWaitForShown(surface.data(), size, Qt::blue);
    //move to be in the first screen
    c->move(QPoint(100,100));
    //we don't don't know where the compositor first placed this window,
    //this might fire, it might not
    outputEnteredSpy.wait(5);
    outputEnteredSpy.clear();
    QCOMPARE(waylandServer()->display()->outputs().count(), 2);

    QCOMPARE(surface->outputs().count(), 1);
    Output *firstOutput = surface->outputs().first();
    QCOMPARE(firstOutput->globalPosition(), QPoint(0,0));
    QSignalSpy modesChangedSpy(firstOutput, &Output::modeChanged);

    QSignalSpy screenChangedSpy(screens(), &KWin::Screens::changed);

    QCOMPARE(Test::waylandOutputDevicesV2().count(), 2);

    Test::WaylandOutputDeviceV2 *device = Test::waylandOutputDevicesV2().first();
    QCOMPARE(device->enabled(), true);

    QSignalSpy outputDeviceEnabledChangedSpy(device, &Test::WaylandOutputDeviceV2::enabledChanged);

    AbstractOutput *output = kwinApp()->platform()->outputs().first();
    // Removes an output
    QMetaObject::invokeMethod(kwinApp()->platform(), "removeOutput", Qt::DirectConnection, Q_ARG(AbstractOutput *, output));

    // Let the new state propagate
    QVERIFY(outputEnteredSpy.wait());

    QCOMPARE(waylandServer()->display()->outputs().count(), 1);
    QCOMPARE(waylandServer()->display()->outputDevices().count(), 1);

    QCOMPARE(Test::waylandOutputDevicesV2().count(), 1);
    QCOMPARE(outputDeviceEnabledChangedSpy.count(), 1);
    QCOMPARE(device->enabled(), false);
    QCOMPARE(outputLeftSpy.count(), 1);
    QCOMPARE(outputEnteredSpy.count(), 1); // surface moved to the other screen
    QCOMPARE(surface->outputs().count(), 1);
    QCOMPARE(screens()->count(), 1);
    QCOMPARE(screenChangedSpy.count(), 3);
    QCOMPARE(modesChangedSpy.count(), 0);
    QCOMPARE(outputEnabledSpy.count(), 0);
    QCOMPARE(outputDisabledSpy.count(), 1);
    QCOMPARE(outputRemovedSpy.count(), 1);
}

WAYLANDTEST_MAIN(TestOutputManagement)
#include "outputmanagement_test.moc"