package tools;

import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
import com.sk89q.worldedit.math.BlockVector3;
import com.yaohun.wheatwar.WheatWar;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;

import java.io.File;

public class GameUtil {
    public static String hideName(String audience) {
        if (audience.length() <= 2) {
            return "**";
        }
        char firstChar = audience.charAt(0);
        char lastChar = audience.charAt(audience.length() - 1);
        StringBuilder maskedString = new StringBuilder();
        for (int i = 1; i < audience.length() - 1; i++) {
            maskedString.append('*');
        }
        return String.valueOf(firstChar) + maskedString + lastChar;
    }
    public static void sendMessage(CommandSender sender, String message) {
        sender.sendMessage("§c[系统]§a" + message);
    }
    public static void sendAllSound(String sound) {
        for (Player player : Bukkit.getOnlinePlayers()) {
            if (sound.contains("_")) {
                player.playSound(player.getLocation(), Sound.valueOf(sound), 1, 1);
            } else {
                player.playSound(player.getLocation(), sound, 0.5F, 1);
            }
        }
    }
    public static void sendAllTitle(String title,String subTitle,int t1,int t2,int t3) {
        for (Player player : Bukkit.getOnlinePlayers()) {
            player.sendTitle(title,subTitle,t1,t2,t3);
        }
    }

    public static Vector getRandomVector(double radius){
        double x = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
        double y = RandomUtil.getRandomDouble(0, 0.5, 1);
        double z = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
        return new Vector(x, y, z);
    }
    public static Vector getRandomVector(double radius,double radiusY){
        double x = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
        double y = RandomUtil.getRandomDouble(0+radiusY, 0.5+radiusY, 1);
        double z = RandomUtil.getRandomDouble(0-radius, 0+radius, 1);
        return new Vector(x, y, z);
    }

    public static void loadSchematics(String fileName) {
        World world = Bukkit.getWorld("world");
        File schema_file = new File("plugins/FastAsyncWorldEdit/schematics",fileName+".schem");
        Location location = new Location(world, -233,74,258);
        EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(world));
        Bukkit.getScheduler().runTaskAsynchronously(
                WheatWar.inst(), () -> {
                    try {
                        Clipboard clipboard = ClipboardFormats.findByFile(schema_file).load(schema_file);
                        clipboard.paste(BukkitAdapter.adapt(world), BlockVector3.at(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
                        clipboard.flush();
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        editSession.close();
                    }
                }
        );
    }
}
