import fr.akame.uhc.API;
import fr.akame.uhc.utils.ActionBar;
import fr.akame.uhc.utils.msg.ProgressBar;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;

public class LoadingChunkV2 {

    private World world;
    private int size;
    private BukkitTask task;
    private int nChunk;
    private int last;
    private long startTime;

    public LoadingChunkV2(World world) {
        this.size = 1000 + 100;
        this.world = world;
        world.setGameRuleValue("randomTickSpeed", "0");
        load();
    }

    private void load() {
        new Thread(() -> {
            this.startTime = System.currentTimeMillis();
            this.world.getEntities().forEach(ent -> {
                if (ent != null && ent.getType() != EntityType.MINECART_CHEST) {
                    ent.remove();
                }
                return;
            });
            this.task = Bukkit.getScheduler().runTaskTimer(API.getAPI(), new Runnable() {
                private int todo = LoadingChunkV2.this.size * 2 * (LoadingChunkV2.this.size * 2) / 256;
                private int x = -LoadingChunkV2.this.size;
                private int z = -LoadingChunkV2.this.size;

                @Override
                public void run() {
                    for (int i = 0; i < 50; ++i) {
                        Chunk chunk = LoadingChunkV2.this.world.getChunkAt(LoadingChunkV2.this.world.getBlockAt(this.x, 64, this.z));
                        chunk.load(true);
                        chunk.load(false);
                        int percentage = LoadingChunkV2.this.nChunk * 100 / this.todo;
                        if (percentage > LoadingChunkV2.this.last) {
                            LoadingChunkV2.this.last = percentage;
                            for (Player player : Bukkit.getOnlinePlayers()) {
                                ActionBar.send(player, ChatColor.YELLOW + "Prégénération: " + ChatColor.GREEN + percentage + "%"
                                        + " §8[§r" + ProgressBar.getProgressBar(percentage, 100, 40, "|", ChatColor.YELLOW, ChatColor.GRAY) + "§8]");
                            }

                        }
                        this.z += 16;
                        if (this.z >= LoadingChunkV2.this.size) {
                            this.z = -LoadingChunkV2.this.size;
                            this.x += 16;
                        }
                        if (this.x >= LoadingChunkV2.this.size) {
                            LoadingChunkV2.this.task.cancel();
                            int calculedTime = Math.round((System.currentTimeMillis() - LoadingChunkV2.this.startTime) / 1000L);
                            System.out.println("Finished preload after " + calculedTime + "s");
                            return;
                        }
                        LoadingChunkV2.this.nChunk++;
                    }
                }
            }, 0L, 0L);
        }).run();
    }

}