/*
 *   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.worldedit.pattern.gradient;

import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock;
import org.bukkit.Axis;

import java.util.Random;

public class GradientPattern implements Pattern {

    private final Axis axis;
    private final Pattern first;
    private final Pattern second;
    private final BlockVector3 min;
    private final int size;
    private final Random random = new Random();

    public GradientPattern(Axis axis, Pattern first, Pattern second, BlockVector3 min, int size) {
        this.axis = axis;
        this.first = first;
        this.second = second;
        this.min = min;
        this.size = size;
    }

    @Override
    public BaseBlock applyBlock(BlockVector3 position) {
        int percent = 0;
        switch (axis) {
            case X:
                percent = (int) ((position.getX() - min.getX()) / (double) size * 100);
                break;
            case Y:
                percent = (int) ((position.getY() - min.getY()) / (double) size * 100);
                break;
            case Z:
                percent = (int) ((position.getZ() - min.getZ()) / (double) size * 100);
                break;
        }
        if (random.nextInt(100) <= Math.abs(percent)) {
            return first.applyBlock(position);
        } else {
            return second.applyBlock(position);
        }
    }
}
