package de.hessj.environmentex;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftItem;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerAttemptPickupItemEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;

import de.hessj.helper.Helper.Type;
import net.kyori.adventure.inventory.Book;
import net.kyori.adventure.text.Component;

public class Filter implements Listener {

    long creationTime = 0;
    public de.hessj.helper.Helper helper= new de.hessj.helper.Helper();

    public void addToBook(Player pl, Material m, String name) {
        if (creationTime == 0) {
            BookMeta bm = (BookMeta) pl.getInventory().getItemInMainHand().getItemMeta();
            if (bm.getPageCount() < 100) {

                ItemStack b = pl.getInventory().getItemInMainHand();
                Boolean isWritten = false;
                List<Component> pages = new ArrayList<>();

                for (Component p : bm.pages()) {

                    String answer = p.toString().substring(p.toString().indexOf("content=\""));
                    answer = answer.substring(answer.indexOf("\""));
                    answer = answer.substring(1, answer.indexOf("\"", answer.indexOf("\"") + 1));

                    if (answer != "") {
                        pages.add(p);
                    }
                }
                Component testp = null;
                for (Component p : pages) {

                    String answer = p.toString().substring(p.toString().indexOf("content=\""));
                    answer = answer.substring(answer.indexOf("\""));
                    answer = answer.substring(1, answer.indexOf("\"", answer.indexOf("\"") + 1));

                    if (answer.equals(m.toString())) {
                        isWritten = true;
                        testp = p;
                        break;
                    }

                }
                if (isWritten) {

                    pl
                            .sendMessage(helper.R3SMessage(Type.INFO, "[ItemFilter] " + name + " wurde aus dem Buch ausgetragen!"));
                    pages.remove(testp);

                } else {
                    pages.add(Component.text(m.toString()));
                    pl.sendMessage(helper.R3SMessage(Type.INFO, "[ItemFilter] " + name + " wurde in das Buch übertragen!"));
                }
                bm.pages(pages);
                b.setItemMeta(bm);

            } else {
                pl
                        .sendMessage(helper.R3SMessage(Type.ERROR, "[ItemFilter] Du kannst nur bis zu 100 Items filtern! Entferne ein paar Seiten aus dem Buch!"));
            }
            creationTime = Instant.now().getEpochSecond();
            Bukkit.getScheduler().scheduleSyncDelayedTask(App.main, new Runnable() {
                public void run() {
                    creationTime = 0;
                    return;
                }
            }, 15);
        }
    }

    @EventHandler
    public void selectFilter(PlayerInteractEvent e) {
        if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
            if (e.getPlayer().getInventory().getItemInMainHand() != null
                    && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.WRITABLE_BOOK)
                    && e.getPlayer().getInventory().getItemInMainHand().hasItemMeta()
                    && e.getPlayer().getInventory().getItemInMainHand().getItemMeta().hasDisplayName() &&
                    e.getPlayer().getInventory().getItemInMainHand().getItemMeta().displayName()
                            .equals(Component.text("Filter"))) {

                Location loc = e.getClickedBlock().getLocation();

                if (loc.getNearbyEntitiesByType(CraftItem.class, 1, 2, 1).size() > 1) {
                    e.getPlayer().sendMessage(helper.R3SMessage(Type.ERROR, "[ItemFilter] Bitte nur ein Item gleichzeitig!"));
                } else {
                    for (Entity en : loc.getNearbyEntitiesByType(CraftItem.class, 1, 2, 1)) {
                        CraftItem itm = (CraftItem) en;
                        addToBook(e.getPlayer(), itm.getItemStack().getType(), itm.getItemStack().getType().name());
                        break;
                    }
                }
            }
        }
    }

    @EventHandler
    public void onPickup(PlayerAttemptPickupItemEvent e) {
        BookMeta bm = null;
        Book b = null;

        for (ItemStack is : e.getPlayer().getInventory().getContents()) {
            if (is != null && is.getType().equals(Material.WRITABLE_BOOK)) {
                bm = (BookMeta) is.getItemMeta();
                b = bm;

                if (is.hasItemMeta() && is.getItemMeta().hasDisplayName()
                        && is.getItemMeta().displayName().equals(Component.text("Filter"))) {

                    for (Component p : b.pages()) {

                        String answer = p.toString().substring(p.toString().indexOf("content=\""));
                        answer = answer.substring(answer.indexOf("\""));
                        answer = answer.substring(1, answer.indexOf("\"", answer.indexOf("\"") + 1));

                        if (answer.equals(e.getItem().getItemStack().getType().toString())) {
                            e.setCancelled(true);
                        }

                    }

                }

                return;
            }
        }
    }
}
