/*
 *   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.lobby.particle.elements.custom;

import de.steamwar.lobby.particle.ParticleElement;
import de.steamwar.lobby.particle.ParticleTickData;
import de.steamwar.lobby.particle.elements.DelegatingParticleElement;

public class YOffset extends DelegatingParticleElement {

    private double multiplication;
    private double minY;
    private double maxY;
    private boolean inverted;
    private boolean bounce;

    public YOffset(ParticleElement particleElement, double speed, double minY, double maxY, boolean inverted, boolean bounce) {
        super(particleElement);
        this.multiplication = speed;
        this.minY = minY;
        this.maxY = maxY;
        this.inverted = inverted;
        this.bounce = bounce;
    }

    @Override
    public String attribute() {
        return null;
    }

    @Override
    public void tick(ParticleTickData particleTickData) {
        double y;
        if (bounce) {
            double value = ((particleTickData.getDeg() * multiplication) % 360) / 180;
            if (inverted) {
                if (value <= 1) {
                    y = maxY - (maxY - minY) * value;
                } else {
                    y = minY + (maxY - minY) * (value - 1);
                }
            } else {
                if (value <= 1) {
                    y = minY + (maxY - minY) * value;
                } else {
                    y = maxY - (maxY - minY) * (value - 1);
                }
            }
        } else {
            double value = ((particleTickData.getDeg() * multiplication) % 360) / 360;
            if (inverted) {
                y = maxY - (maxY - minY) * value;
            } else {
                y = minY + (maxY - minY) * value;
            }
        }
        ParticleTickData current = particleTickData.withLocation(particleTickData.getLocation().clone().add(0, y, 0));
        particleElement.tick(current);
    }
}
