/*
 * Copyright (C) 2026, KylinSoft 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.
 */

#include <QtTest>
#include <QColor>
#include <QImage>

#include "icon-heuristics.h"

using namespace UkuiQuick::IconHeuristics;

class TestIconHeuristics : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void coverage_counts_partial_alpha();
    void completeImagePixel_accepts_high_alpha_without_full_255();
    void completeImagePixel_repairs_corners_with_logo_near_corner();
    void completeImagePixel_uses_backdrop_color_instead_of_logo_color();
};

void TestIconHeuristics::coverage_counts_partial_alpha()
{
    QImage image(2, 2, QImage::Format_ARGB32);
    image.fill(Qt::transparent);
    image.setPixelColor(0, 0, QColor(255, 255, 255, 128));

    const qreal coverage = alphaCoverage(image, QRect(0, 0, 2, 2));

    QVERIFY2(coverage > 0.1, "Partial alpha pixels should contribute to coverage.");
    QVERIFY2(coverage < 0.2, "Coverage should remain fractional for a single half-opaque pixel.");
}

void TestIconHeuristics::completeImagePixel_accepts_high_alpha_without_full_255()
{
    QImage image(6, 6, QImage::Format_ARGB32);
    image.fill(Qt::transparent);

    const QColor backdropColor(20, 120, 220, 240);
    for (int y = 0; y < image.height(); ++y) {
        for (int x = 0; x < image.width(); ++x) {
            image.setPixelColor(x, y, backdropColor);
        }
    }
    image.setPixelColor(0, 0, QColor(0, 0, 0, 0));

    repairBackdropCorners(image);

    QVERIFY2(image.pixelColor(0, 0).alpha() > 0,
             "Corner completion should work for high-alpha backdrops even when no pixel reaches 255 alpha.");
}

void TestIconHeuristics::completeImagePixel_repairs_corners_with_logo_near_corner()
{
    QImage image(12, 12, QImage::Format_ARGB32);
    image.fill(QColor(20, 120, 220, 240));

    const QColor transparentColor(0, 0, 0, 0);
    image.setPixelColor(0, 0, transparentColor);
    image.setPixelColor(11, 0, transparentColor);
    image.setPixelColor(0, 11, transparentColor);
    image.setPixelColor(11, 11, transparentColor);

    const QColor logoColor(245, 245, 245, 240);
    const QList<QPoint> logoPoints {
        QPoint(9, 0), QPoint(10, 0),
        QPoint(9, 1), QPoint(10, 1), QPoint(11, 1),
        QPoint(9, 2), QPoint(10, 2), QPoint(11, 2)
    };
    for (const QPoint &point : logoPoints) {
        image.setPixelColor(point, logoColor);
    }

    repairBackdropCorners(image);

    QVERIFY2(image.pixelColor(11, 0).alpha() > 0,
             "A logo near the corner should not block repairing the transparent backdrop corner.");
    QCOMPARE(image.pixelColor(10, 1), logoColor);
    QCOMPARE(image.pixelColor(10, 2), logoColor);
}

void TestIconHeuristics::completeImagePixel_uses_backdrop_color_instead_of_logo_color()
{
    QImage image(12, 12, QImage::Format_ARGB32);
    const QColor backdropColor(20, 120, 220, 240);
    image.fill(backdropColor);

    const QColor transparentColor(0, 0, 0, 0);
    image.setPixelColor(11, 0, transparentColor);

    const QColor logoColor(245, 245, 245, 240);
    image.setPixelColor(11, 1, logoColor);
    image.setPixelColor(10, 1, logoColor);
    image.setPixelColor(10, 0, backdropColor);

    repairBackdropCorners(image);

    QCOMPARE(image.pixelColor(11, 0).red(), backdropColor.red());
    QCOMPARE(image.pixelColor(11, 0).green(), backdropColor.green());
    QCOMPARE(image.pixelColor(11, 0).blue(), backdropColor.blue());
}

QTEST_MAIN(TestIconHeuristics)
#include "test-icon-heuristics.moc"
