/*
 * 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.mask.above;

import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.command.util.SuggestionHelper;
import com.sk89q.worldedit.extension.input.InputParseException;
import com.sk89q.worldedit.extension.input.ParserContext;
import com.sk89q.worldedit.function.mask.Mask;
import de.steamwar.bausystem.features.worldedit.utils.FAWEMaskParser;
import de.steamwar.linkage.Linked;
import de.steamwar.linkage.PluginCheck;

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

@Linked
@PluginCheck("FastAsyncWorldEdit")
public class FAWEAboveMaskParser extends FAWEMaskParser {

    public FAWEAboveMaskParser() {
        super("#above");
    }

    @Override
    protected Stream<String> getSuggestions(String argumentInput, int index) {
        if (index == 0) {
            return WorldEdit.getInstance().getPatternFactory().getSuggestions(argumentInput).stream();
        }
        if (index == 1) {
            return SuggestionHelper.suggestPositiveIntegers(argumentInput);
        }
        return Stream.empty();
    }

    @Override
    protected Mask parseFromInput(@Nonnull String[] arguments, ParserContext context) throws InputParseException {
        if (arguments.length != 2) {
            throw new InputParseException("Expected 2 arguments");
        }
        Mask mask = WorldEdit.getInstance().getMaskFactory().parseFromInput(arguments[0], context);
        int distance;
        try {
            distance = Integer.parseInt(arguments[1]);
        } catch (NumberFormatException e) {
            throw new InputParseException("Distance must be a number");
        }
        return new AboveMask(mask, distance);
    }
}
