/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2022  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.WorldEdit;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.pattern.Pattern;
import com.sk89q.worldedit.regions.Region;
import de.steamwar.bausystem.features.worldedit.utils.FAWEPatternParser;
import de.steamwar.bausystem.utils.WorldEditUtils;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.PluginCheck;
import org.bukkit.Axis;

import javax.annotation.Nonnull;
import java.util.stream.Stream;

@Linked
@PluginCheck("FastAsyncWorldEdit")
public class FAWEGradientPatternParser extends FAWEPatternParser {

    public FAWEGradientPatternParser() {
        super("#gradient");
    }

    @Override
    protected Stream<String> getSuggestions(String input, int index) {
        if (index == 0) {
            return Stream.of("x", "y", "z");
        }
        if (index == 1 || index == 2) {
            return WorldEdit.getInstance().getPatternFactory().getSuggestions(input).stream();
        }
        return Stream.empty();
    }

    @Override
    protected Pattern parseFromInput(@Nonnull String[] arguments, ParserContext context) throws InputParseException {
        if (arguments.length < 2) {
            throw new InputParseException("Not enough arguments given for gradient pattern");
        }
        Axis axis = null;
        switch (arguments[0]) {
            case "x":
                axis = Axis.X;
                break;
            case "y":
                axis = Axis.Y;
                break;
            case "z":
                axis = Axis.Z;
                break;
            default:
                throw new InputParseException("No axis given for gradient pattern");
        }
        Region region = context.getSelection();
        Pattern from = WorldEditUtils.getPatternFactory().parseFromInput(arguments[1], context);
        Pattern to = WorldEditUtils.getPatternFactory().parseFromInput(arguments[2], context);
        int size = 0;
        switch (axis) {
            case X:
                size = region.getWidth();
                break;
            case Y:
                size = region.getHeight();
                break;
            case Z:
                size = region.getLength();
                break;
        }
        return new GradientPattern(axis, from, to, region.getMinimumPoint(), size);
    }
}
