package hu.jgj52.pvpcore;

import hu.jgj52.pvpcore.Commands.*;
import hu.jgj52.pvpcore.Listeners.*;
import hu.jgj52.pvpcore.Utils.*;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;
import java.util.concurrent.ExecutionException;

public final class Main extends JavaPlugin {

    public Database db = new Database("188.245.120.192", 24454, "wolfpvp", "wolfpvp", "eVE9*!9qtj08jXuxe$#$a08jNxVdMTbt");

    public final String[] metadatas = {"Frozen"};

    public void tpToSpawn(Player player) {
        Location spawn = new Location(Bukkit.getWorld("world"), 0.5, 3, 0.5, 0, 0);
        for (PotionEffect potionEffect : player.getActivePotionEffects()) {
            player.removePotionEffect(potionEffect.getType());
        }
        player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_FALLING, 20, 1), true);
        player.setHealth(20D);
        player.setFoodLevel(20);
        player.setSaturation(5);
        player.setGameMode(GameMode.ADVENTURE);
        player.teleport(spawn);
        player.getInventory().clear();
        for (String key : metadatas) {
            for (MetadataValue value : player.getMetadata(key)) {
                if (value.getOwningPlugin() == this) {
                    player.removeMetadata(key, this);
                    break;
                }
            }
        }
    }

    /*páblik statik vojd rimúvPléjerFromDuel() {;
      ;
    };*/

    @Override
    public void onEnable() {
        getConfig().options().copyDefaults(true);
        saveDefaultConfig();
        saveConfig();

        getCommand("duel").setExecutor(new DuelCommand(this));
        getCommand("acceptduel").setExecutor(new AcceptDuelCommand(this));
        getCommand("denyduel").setExecutor(new DenyDuelCommand(this));
        getCommand("editkit").setExecutor(new EditKitCommand(this));
        getCommand("resetarenas").setExecutor(new ResetArenasCommand(this));
        getCommand("spectate").setExecutor(new SpectateCommand(this));
        getCommand("leave").setExecutor(new LeaveCommand(this));
        getCommand("createkit").setExecutor(new CreateKitCommand(this));
        getCommand("connect").setExecutor(new ConnectCommand(this));

        getServer().getPluginManager().registerEvents(new DuelRequestListener(this), this);
        getServer().getPluginManager().registerEvents(new KitListener(this), this);
        getServer().getPluginManager().registerEvents(new DuelEndListener(this), this);
        getServer().getPluginManager().registerEvents(new FreezeListener(), this);
        getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
        getServer().getPluginManager().registerEvents(new SpectatorListener(this), this);

        WorldReset worldReset = new WorldReset(this);
        worldReset.recreateArenaWorld();

        new BukkitRunnable() {
            @Override
            public void run() {
                try {
                    Database.QueryResult result = db.from("pvpcore_duel_requests").execute().get();
                    if (!result.isEmpty()) {
                        Map<String, Object> row = result.data.get(0);
                        if (Long.parseLong(row.get("timestamp").toString()) <= System.currentTimeMillis()) {
                            db.from("pvpcore_duel_requests").eq("timestamp", row.get("timestamp")).delete();
                            Player player = Bukkit.getPlayer(UUID.fromString(row.get("player").toString()));
                            Player enemy = Bukkit.getPlayer(UUID.fromString(row.get("enemy").toString()));
                            player.sendMessage("§cLejárt a párbajkérelmed " + enemy.getName() + " ellen!");
                            enemy.sendMessage("§c" + player.getName() + "-nak lejárt a párbajkérelme!");
                        }
                    }
                } catch (InterruptedException | ExecutionException e) {
                    throw new RuntimeException(e);
                }
                try {
                    Database.QueryResult result = db.from("pvpcore_connect").execute().get();
                    if (!result.isEmpty()) {
                        Map<String, Object> row = result.data.get(0);
                        if (Long.parseLong(row.get("timestamp").toString()) <= System.currentTimeMillis()) {
                            db.from("pvpcore_connect").eq("timestamp", row.get("timestamp")).delete();
                            Player player = Bukkit.getPlayer(UUID.fromString(row.get("player").toString()));
                            player.sendMessage("§cLejárt a bejelentkezési kulcsod!");
                        }
                    }
                } catch (InterruptedException | ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }
        }.runTaskTimer(this, 100L, 100L);

        db.from("pvpcore_duels").delete();
        db.from("pvpcore_duel_requests").delete();
        db.from("pvpcore_connect").delete();
    }

    @Override
    public void onDisable() {
        //hi
    }

    public List<Integer> usedArenas = new ArrayList<>();
}
