/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2021  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.region;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.utils.RegionExtensionType;
import de.steamwar.bausystem.region.utils.RegionType;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.worlddata.WorldData;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.io.File;
import java.util.function.Consumer;
import java.util.function.Function;


@UtilityClass
public class RegionUtils {

    public void actionBar(Region region, String s, Object... objects) {
        Bukkit.getOnlinePlayers()
                .stream()
                .filter(player -> region.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL))
                .filter(player -> !region.isGlobal() || Region.getRegion(player.getLocation()).isGlobal())
                .forEach(player -> player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(BauSystem.MESSAGE.parse(s, player, objects))));
    }

    public static void message(Region region, String message, Object... objects) {
        Bukkit.getOnlinePlayers()
                .stream()
                .filter(player -> region.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL))
                .filter(player -> !region.isGlobal() || Region.getRegion(player.getLocation()).isGlobal())
                .forEach(player -> BauSystem.MESSAGE.send(message, player, objects));
    }

    public static void message(Region region, Function<Player, String> function) {
        Bukkit.getOnlinePlayers()
                .stream()
                .filter(player -> region.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL))
                .filter(player -> !region.isGlobal() || Region.getRegion(player.getLocation()).isGlobal())
                .forEach(player -> {
                    String message = function.apply(player);
                    if (message == null) {
                        return;
                    }

                    player.sendMessage(message);
                });
    }

    public static void message(Region region, Consumer<Player> function) {
        Bukkit.getOnlinePlayers()
                .stream()
                .filter(player -> region.inRegion(player.getLocation(), RegionType.NORMAL, RegionExtensionType.NORMAL))
                .filter(player -> !region.isGlobal() || Region.getRegion(player.getLocation()).isGlobal())
                .forEach(player -> {
                    function.accept(player);
                });
    }

    static void save(Region region) {
        if (region.getPrototype() != null) {
            region.regionData.add("prototype", region.getPrototype().getName());
        }
        region.regionData.add("flagStorage", FlagStorage.toYAPION(region.getFlagStorage()));
        WorldData.write();
    }

}
