package de.hessj.dailyquests;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.components.CustomModelDataComponent;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.Scoreboard;
import net.kyori.adventure.text.Component;
import net.md_5.bungee.api.ChatColor;
import de.hessj.helper.Helper.Type;

public class App extends JavaPlugin {
    public static App main;
    public de.hessj.helper.Helper helper = new de.hessj.helper.Helper();
    public ArrayList<ItemStack> rewardList = new ArrayList<ItemStack>();
    public ItemStack diamondDust;
    public NamespacedKey nskdiamondDust;
    public NamespacedKey nskDD = new NamespacedKey(this, "R3S_SR_DIAMONDDUST");

    @Override
    public void onEnable() {
        getLogger().info("Plugin enabled!");
        main = this;
        setupRewards();
        DailyQuestsListeners.getTodaysQuest();
        LocalDateTime start = LocalDateTime.now();
        LocalDateTime nextMidnight = start.plusDays(1).withHour(0).withMinute(0).withSecond(0);
        ZonedDateTime zdt = ZonedDateTime.of(nextMidnight, ZoneId.of("Europe/Berlin"));
        Duration duration = Duration.between(start, zdt);
        long secUntil = duration.toMillis() / 1000;
        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
            @Override
            public void run() {
                resetQuestScheduler();
            }
        }, 20 * secUntil); // Wird beim nächsten 0Uhr ausgeführt

        listenerRegistration();
    }

    public void resetQuestScheduler() {
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
            @Override
            public void run() {
                getConfig().set("players", null);
                saveConfig();
                for (Player p : Bukkit.getServer().getOnlinePlayers()) {
                    DailyQuestsListeners.getDailyQuests(p);
                    Entry<Scoreboard, Boolean> actionMapEntry = DailyQuestsCommands.boards.get(p.getName());
                    Scoreboard board = actionMapEntry.getKey();
                    if (!actionMapEntry.getValue()) {
                        p.setScoreboard(Bukkit.getServer().getScoreboardManager().getNewScoreboard());
                        DailyQuestsCommands.boards.put(p.getName(), new SimpleEntry<Scoreboard, Boolean>(board, false));
                    } else {
                        p.setScoreboard(Bukkit.getServer().getScoreboardManager().getNewScoreboard());
                        p.setScoreboard(board);

                        DailyQuestsCommands.boards.put(p.getName(), new SimpleEntry<Scoreboard, Boolean>(board, true));
                    }
                    p.sendMessage(helper.R3SMessage(Type.INFO, "[DailyQuests] Du hast eine neue Quest erhalten!"));
                }
                saveConfig();
            }
        }, 0L, (24 * 60 * 60) * 20L); // Alle 24std neue Quest
    }

    @Override
    public void onDisable() {
        getLogger().info("Plugin disabled!");
    }

    private void listenerRegistration() {
        PluginManager pM = Bukkit.getPluginManager();
        pM.registerEvents(new DailyQuestsListeners(), this);
        getCommand("quest").setExecutor(new DailyQuestsCommands());
    }

    private void setupRewards() {
		
        nskdiamondDust = new NamespacedKey(App.main, "R3SDiamondDust");
        diamondDust = new ItemStack(Material.BARRIER);
        ItemMeta mdiamondDust = diamondDust.getItemMeta();
        mdiamondDust.setCustomModelData(1000010);


      


        CustomModelDataComponent cmdc4 = mdiamondDust.getCustomModelDataComponent();
        List<String> list4 = Arrays.asList("diamond_dust");
        cmdc4.setStrings(list4);
        mdiamondDust.setCustomModelDataComponent(cmdc4);






        mdiamondDust.displayName(Component.translatable(ChatColor.WHITE + "Diamant-Staub"));
        mdiamondDust.getPersistentDataContainer().set(nskdiamondDust, PersistentDataType.STRING, "R3SDiamondDust");
        diamondDust.setItemMeta(mdiamondDust);
        RecipeChoice rc = new RecipeChoice.ExactChoice(diamondDust);
        ShapedRecipe ddRecipe = new ShapedRecipe(nskDD,
                new ItemStack(Material.DIAMOND));
        ddRecipe.shape("** ", "** ", "   ");
        ddRecipe.setIngredient('*', rc);
		if (getServer().getRecipe(nskDD) == null) {
        App.main.getServer().addRecipe(ddRecipe);
		}
        rewardList.add(new ItemStack(Material.EXPERIENCE_BOTTLE, 1));
        rewardList.add(new ItemStack(Material.APPLE, 10));
        rewardList.add(new ItemStack(Material.POTATO, 5));
        rewardList.add(new ItemStack(Material.CARROT, 5));
        rewardList.add(new ItemStack(Material.COOKED_BEEF, 10));
        rewardList.add(new ItemStack(Material.COOKED_CHICKEN, 10));
        rewardList.add(new ItemStack(Material.COOKED_CHICKEN, 10));
        rewardList.add(new ItemStack(Material.COOKED_COD, 10));
        rewardList.add(new ItemStack(Material.COOKED_MUTTON, 10));
        rewardList.add(new ItemStack(Material.COOKED_PORKCHOP, 10));
        rewardList.add(new ItemStack(Material.COOKED_RABBIT, 10));
        rewardList.add(new ItemStack(Material.COOKED_SALMON, 10));
        rewardList.add(new ItemStack(Material.IRON_INGOT, 5));
        rewardList.add(new ItemStack(Material.COPPER_INGOT, 5));
        rewardList.add(new ItemStack(Material.GOLD_NUGGET, 5));
        rewardList.add(new ItemStack(Material.FIREWORK_ROCKET, 5));
        rewardList.add(new ItemStack(Material.SADDLE, 1));
        rewardList.add(new ItemStack(Material.MELON_SEEDS, 5));
        rewardList.add(new ItemStack(Material.PUMPKIN_SEEDS, 5));
        rewardList.add(new ItemStack(Material.BEETROOT_SEEDS, 5));
        rewardList.add(new ItemStack(Material.DIAMOND, 1));
        rewardList.add(new ItemStack(Material.CAKE, 1));
        rewardList.add(new ItemStack(Material.ENDER_PEARL, 5));
    }
}