package com.yutian.minerwar.util;

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.yutian.minerwar.MinerWar;
import com.yutian.minerwar.game.Region;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import tools.RandomUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class discardCode {

    public void initGoldOreGenerate(){
        Region region = MinerWar.getGame().getGameRegion();
        Bukkit.getConsoleSender().sendMessage("[日志 - 矿工战争] 游戏场地:");
        long startGoldTime = System.currentTimeMillis(); // 记录开始时间
        List<Location> chunkStartLocations = new ArrayList<>();
        // 获取区域的最小和最大坐标
        int regionMinX = (int) Math.floor(region.getMin().getX());
        int regionMaxX = (int) Math.floor(region.getMax().getX());
        int regionMinZ = (int) Math.floor(region.getMin().getZ());
        int regionMaxZ = (int) Math.floor(region.getMax().getZ());
        World world = region.getWorld(); // 假设 Region 可以获取 World 对象
        // 按区块（16x16）的范围迭代
        for (int x = regionMinX; x <= regionMaxX; x += 30) {
            for (int z = regionMinZ; z <= regionMaxZ; z += 30) {
                // 计算每个区块的左上角位置 (x=0, z=0 对应的局部位置)
                int newX = x + RandomUtil.getRandomInt(-10,10);
                int newZ = z + RandomUtil.getRandomInt(-10,10);
                Block highestBlock = world.getHighestBlockAt(newX, newZ);
                Material blockType = highestBlock.getType();
                if (blockType.equals(Material.AIR) || blockType.name().contains("WATER") || blockType.name().contains("LAVA")) {
                }else {
                    chunkStartLocations.add(highestBlock.getLocation()); // 将区块起始位置添加到列表
                }
            }
        }
        // 输出结果，调试查看区块的起始位置
        Bukkit.getConsoleSender().sendMessage(" - 区块数量: "+chunkStartLocations.size()+"个");
        Collections.shuffle(chunkStartLocations);
        List<Location> locationList = new ArrayList<>();
        for (Location location : chunkStartLocations) {
            if (locationList.size() >= MinerWar.getGameManager().getGoldOreGoal()) {
                break;
            }
            Location loc = location.add(0,1,0);
            world.getBlockAt(loc).setType(Material.GOLD_ORE);
            locationList.add(loc);
        }
        long GoldendTime = System.currentTimeMillis(); // 记录结束时间
        long Goldduration = GoldendTime - startGoldTime; // 计算耗时
        Bukkit.getConsoleSender().sendMessage(" - 金矿坐标: 已储存 "+Goldduration+" ms");
        Bukkit.getConsoleSender().sendMessage(" - 清理杂草: 启动中");
        long startGrassTime = System.currentTimeMillis(); // 记录开始时间
        // 清理这个区域中所有的非白名单方块
        for (int y = 60; y <= 130; y++) {
            for (int x = (int) Math.floor(region.getMin().getX()-50); x <= region.getMax().getX()+50; x++) {
                for (int z = (int) Math.floor(region.getMin().getZ()-50); z <= region.getMax().getZ()+50; z++) {
                    Block block = region.getWorld().getBlockAt(x, y, z);
                    if (!block.getType().equals(Material.AIR)) {
                        if(block.getType() == Material.TALL_GRASS){
                            block.setType(Material.AIR);
                        }else if(block.getType() == Material.SUNFLOWER){
                            block.setType(Material.AIR);
                        }
                    }
                }
            }
        }
        long endTime = System.currentTimeMillis(); // 记录结束时间
        long duration = endTime - startGrassTime; // 计算耗时
        Bukkit.getConsoleSender().sendMessage(" - 清理杂草: 清理完成 "+duration+" ms");
        File schema_file = new File("plugins/FastAsyncWorldEdit/schematics","spawn.schem");
        Location location = new Location(world, 0,72,0);
        EditSession editSession = WorldEdit.getInstance().newEditSession(BukkitAdapter.adapt(world));
        Bukkit.getScheduler().runTaskAsynchronously(
                MinerWar.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();
                    }
                }
        );
        Bukkit.getConsoleSender().sendMessage(" - 村民小屋: 已刷新");
    }
}
