/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.features.tracer.rendering;

import de.steamwar.bausystem.features.tracer.TNTPoint;
import lombok.RequiredArgsConstructor;

import java.util.function.BiFunction;

/**
 * A Comparator for determining whether two records should be bundled
 */
@RequiredArgsConstructor
public enum BundleFilter {

    LOOSE((TNTPoint a, TNTPoint b) -> {
        if (a.isExplosion() != b.isExplosion()) return false;
        if (a.getLocation().distanceSquared(b.getLocation()) > BundleFilter.pixelSizeSquared * 8) return false;
        if (a.getVelocity().distanceSquared(b.getVelocity()) > BundleFilter.pixelSizeSquared * 8) return false;
        return true;
    }),

    DEFAULT((TNTPoint a, TNTPoint b) -> {
        if (a.isExplosion() != b.isExplosion()) return false;
        if (a.getTicksSinceStart() != b.getTicksSinceStart()) return null;
        if (a.getLocation().distanceSquared(b.getLocation()) > BundleFilter.pixelSizeSquared) return false;
        if (a.getVelocity().distanceSquared(b.getVelocity()) > BundleFilter.pixelSizeSquared) return false;
        return true;
    }),

    RAW((TNTPoint a, TNTPoint b) -> {
        if (a.isExplosion() != b.isExplosion()) return false;
        if (!a.getLocation().equals(b.getLocation())) return false;
        if (!a.getVelocity().equals(b.getVelocity())) return false;
        if (a.getTicksSinceStart() != b.getTicksSinceStart()) return null;
        return true;
    }),

    NONE((TNTPoint a, TNTPoint b) -> {
        return null;
    });

    /**
     * {@code null}: Bundling can be stopped from this point forward
     * {@code false}: No bundling allowed
     * {@code true}: Bundling should be applied
     */
    public final BiFunction<TNTPoint, TNTPoint, Boolean> function;

    private static final double pixelSize = 0.0625;
    private static final double pixelSizeSquared = pixelSize * pixelSize;
}
