package dev.sefiraat.netheopoiesis.api.items;

import com.google.common.base.Preconditions;
import com.xzavier0722.mc.plugin.slimefun4.storage.controller.SlimefunBlockData;
import com.xzavier0722.mc.plugin.slimefun4.storage.util.StorageCacheUtils;
import dev.sefiraat.netheopoiesis.Netheopoiesis;
import dev.sefiraat.netheopoiesis.Registry;
import dev.sefiraat.netheopoiesis.api.RecipeTypes;
import dev.sefiraat.netheopoiesis.api.events.PlantBeforeGrowthEvent;
import dev.sefiraat.netheopoiesis.api.interfaces.NetherPlant;
import dev.sefiraat.netheopoiesis.api.interfaces.SeedPaste;
import dev.sefiraat.netheopoiesis.api.plant.Growth;
import dev.sefiraat.netheopoiesis.api.plant.breeding.BreedResult;
import dev.sefiraat.netheopoiesis.api.plant.breeding.BreedResultType;
import dev.sefiraat.netheopoiesis.api.plant.breeding.BreedingPair;
import dev.sefiraat.netheopoiesis.api.plant.netheos.FlavourProfile;
import dev.sefiraat.netheopoiesis.implementation.Groups;
import dev.sefiraat.netheopoiesis.implementation.netheos.Paste;
import dev.sefiraat.netheopoiesis.api.plant.GrowthStages;
import dev.sefiraat.netheopoiesis.api.plant.Placements;
import dev.sefiraat.netheopoiesis.listeners.CustomPlacementListener;
import dev.sefiraat.netheopoiesis.utils.Keys;
import dev.sefiraat.netheopoiesis.utils.ParticleUtils;
import dev.sefiraat.netheopoiesis.utils.Skulls;
import dev.sefiraat.netheopoiesis.utils.StatisticUtils;
import dev.sefiraat.netheopoiesis.utils.Theme;
import dev.sefiraat.netheopoiesis.utils.WorldUtils;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.api.events.PlayerRightClickEvent;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockBreakHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack;
import io.github.thebusybiscuit.slimefun4.libraries.dough.skins.PlayerHead;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow;
import net.guizhanss.guizhanlib.minecraft.utils.compatibility.ParticleX;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.OverridingMethodsMustInvokeSuper;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * This class is used to define a Seed item that will grow as a {@link NetherPlant}
 */
public abstract class NetherSeed extends SlimefunItem implements NetherPlant, SeedPaste {

    @Nonnull
    public static final Set<BlockFace> BREEDING_DIRECTIONS = Set.of(
        BlockFace.NORTH,
        BlockFace.SOUTH,
        BlockFace.EAST,
        BlockFace.WEST
    );

    @Nonnull
    protected final Map<Location, UUID> ownerCache = new HashMap<>();
    @Nullable
    protected Growth description;
    @Nullable
    protected ItemStack displayPlant;
    @Nonnull
    protected Set<BreedingPair> breedingPairs = new HashSet<>();

    @Nullable
    protected ItemStack crushingDrop;
    @Nullable
    protected FlavourProfile flavourProfile;

    @ParametersAreNonnullByDefault
    protected NetherSeed(SlimefunItemStack item) {
        this(item, RecipeTypes.PLANT_BREEDING, new ItemStack[0], null);
    }

    @ParametersAreNonnullByDefault
    protected NetherSeed(SlimefunItemStack item,
                         RecipeType recipeType,
                         ItemStack[] recipe
    ) {
        this(item, recipeType, recipe, null);
    }

    @ParametersAreNonnullByDefault
    protected NetherSeed(SlimefunItemStack item,
                         RecipeType recipeType,
                         ItemStack[] recipe,
                         @Nullable ItemStack recipeOutput
    ) {
        super(Groups.SEEDS, item, recipeType, recipe, recipeOutput);
    }

    @Override
    public void preRegister() {
        addItemHandler(
            new BlockTicker() {
                @Override
                public boolean isSynchronized() {
                    return true;
                }

                @Override
                public void tick(Block block, SlimefunItem item, SlimefunBlockData data) {
                    if (item instanceof NetherSeed seed) {
                        onTick(block, seed, data);
                    }
                }
            },
            new BlockPlaceHandler(false) {
                @Override
                @ParametersAreNonnullByDefault
                public void onPlayerPlace(BlockPlaceEvent e) {
                    whenPlaced(e);
                }
            },
            new BlockBreakHandler(false, false) {
                @Override
                @ParametersAreNonnullByDefault
                public void onPlayerBreak(BlockBreakEvent blockBreakEvent, ItemStack itemStack, List<ItemStack> list) {
                    removePurificationRegistry(blockBreakEvent.getBlock());
                }
            },
            (BlockUseHandler) this::onBlockUse
        );
    }

    /**
     * Can be overridden by implementations to add an effect when the block if right-clicked.
     *
     * @param event The {@link PlayerRightClickEvent} being sent from Slimefun
     */
    protected void onBlockUse(@Nonnull PlayerRightClickEvent event) {

    }

    @ParametersAreNonnullByDefault
    private void onTick(Block block, NetherSeed seed, SlimefunBlockData data) {
        final Location location = block.getLocation();
        int growthStage = Integer.parseInt(data.getData(Keys.SEED_GROWTH_STAGE));
        onTickAlways(location, seed, data);
        if (growthStage >= getGrowthStages().stages()) {
            onTickFullyGrown(location, seed, data);
            tryBreed(block, seed);
        } else {
            tryGrow(block, seed, data, location, growthStage);
        }
        registerPurificationValue(block);
    }

    @ParametersAreNonnullByDefault
    private void tryGrow(Block block, NetherSeed seed, SlimefunBlockData data, Location location, int growthStage) {
        final double growthRandom = ThreadLocalRandom.current().nextDouble();
        if (growthRandom <= getGrowthRate() && getGrowthStages().stages() > growthStage) {
            PlantBeforeGrowthEvent event = new PlantBeforeGrowthEvent(location, seed, growthStage);
            Bukkit.getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                return;
            }
            updateGrowthStage(block, growthStage + 1);
            if (getGrowthStages().stages() == growthStage + 1) {
                onFullyMatures(location, seed, data);
                finalGrowthDisplay(location);
            }
        }
    }

    private void tryBreed(@Nonnull Block motherBlock, @Nonnull NetherSeed mother) {
        final double breedRandom = ThreadLocalRandom.current().nextDouble();
        if (breedRandom > getGrowthRate()) {
            // No breed attempt this tick
            return;
        }

        for (BlockFace face : BREEDING_DIRECTIONS) {
            final Block middleBlock = motherBlock.getRelative(face);
            // There must be space for the new block
            if (middleBlock.getType() != Material.AIR) {
                return;
            }
            final Block potentialMate = middleBlock.getRelative(face);
            final SlimefunItem mateItem = StorageCacheUtils.getSfItem(potentialMate.getLocation());

            if (mateItem instanceof NetherSeed mate) {
                final BreedResult result = Registry.getInstance().getBreedResult(mother.getId(), mate.getId());

                if (result.getResultType() == BreedResultType.NO_PAIRS) {
                    // No matching breeding pairs, lets feedback to the player then move to the next direction
                    breedInvalidDisplay(middleBlock.getLocation());
                } else if (result.getResultType() == BreedResultType.SUCCESS) {
                    // Breed was a success - spawn child, log discovery
                    final NetherSeed child = result.getMatchedPair().getChild();
                    trySetChildSeed(motherBlock.getLocation(), middleBlock, child);
                    StatisticUtils.unlockDiscovery(getOwner(motherBlock.getLocation()), child.getId());
                } else if (result.getResultType() == BreedResultType.SPREAD) {
                    // Breed failed, spread success - spawn copy of mother
                    trySetChildSeed(motherBlock.getLocation(), middleBlock, mother);
                }
            }
        }
    }

    @ParametersAreNonnullByDefault
    private void trySetChildSeed(Location motherLocation, Block cloneBlock, NetherSeed childSeed) {
        final Location cloneBlockLocation = cloneBlock.getLocation();
        cloneBlock.setType(Material.PLAYER_HEAD);
        PlayerHead.setSkin(cloneBlock, childSeed.getGrowthStages().get(0).getPlayerSkin(), false);
        PaperLib.getBlockState(cloneBlock, false).getState().update(true, false);
        Slimefun.getDatabaseManager().getBlockDataController().createBlock(cloneBlockLocation, childSeed.getId());
        StorageCacheUtils.setData(cloneBlockLocation, Keys.SEED_GROWTH_STAGE, "0");
        StorageCacheUtils.setData(cloneBlockLocation, Keys.BLOCK_OWNER, getOwner(motherLocation).toString());
        breedSuccess(cloneBlock.getLocation());
    }

    @Override
    public void postRegister() {
        new BlockMenuPreset(this.getId(), this.getItemName()) {

            @Override
            public void init() {
                setSize(9);
                addMenuOpeningHandler(HumanEntity::closeInventory);
            }

            @Override
            public void newInstance(@Nonnull BlockMenu menu, @Nonnull Block block) {
                final String ownerUuidString = StorageCacheUtils.getData(block.getLocation(), Keys.BLOCK_OWNER);
                if (ownerUuidString != null) {
                    final UUID ownerUuid = UUID.fromString(ownerUuidString);
                    addOwner(block.getLocation(), ownerUuid);
                }
            }

            @Override
            public boolean canOpen(@Nonnull Block block, @Nonnull Player player) {
                return true;
            }

            @Override
            public int[] getSlotsAccessedByItemTransport(ItemTransportFlow itemTransportFlow) {
                return new int[0];
            }
        };
    }

    @Nonnull
    public UUID getOwner(@Nonnull Location location) {
        UUID uuid = ownerCache.get(location);
        // Owner cannot be null if called correctly
        Preconditions.checkNotNull(uuid, "Owner is null, has this been called correctly");
        return uuid;
    }

    public void addOwner(@Nonnull Location location, @Nonnull UUID uuid) {
        ownerCache.put(location, uuid);
    }

    public boolean isMature(@Nonnull Block block) {
        return isMature(block.getLocation());
    }

    public boolean isMature(@Nonnull Location location) {
        final String stageString = StorageCacheUtils.getData(location, Keys.SEED_GROWTH_STAGE);
        if (stageString == null) {
            return false;
        }
        final int growthStage = Integer.parseInt(stageString);
        return growthStage >= getGrowthStages().stages();
    }

    public void updateGrowthStage(@Nonnull Location location, int growthStage) {
        updateGrowthStage(location.getBlock(), growthStage);
    }

    public void updateGrowthStage(@Nonnull Block block, int growthStage) {
        if (block.getType() == Material.PLAYER_HEAD || block.getType() == Material.PLAYER_WALL_HEAD) {
            final Skulls nextTexture = getGrowthStages().get(growthStage - 1);
            PlayerHead.setSkin(block, nextTexture.getPlayerSkin(), false);
            PaperLib.getBlockState(block, false).getState().update(true, false);
            StorageCacheUtils.setData(block.getLocation(), Keys.SEED_GROWTH_STAGE, String.valueOf(growthStage));
            growthDisplay(block.getLocation());
        }
    }

    /**
     * This method is fired when the block is placed
     * see {@link CustomPlacementListener}
     *
     * @param event The {@link BlockPlaceEvent} triggered from the block placement
     */
    public void whenPlaced(@Nonnull BlockPlaceEvent event) {
        final Block block = event.getBlock();
        final Block blockBelow = block.getRelative(BlockFace.DOWN);
        final Location location = block.getLocation();
        final SlimefunItem itemBelow = StorageCacheUtils.getSfItem(blockBelow.getLocation());

        if (itemBelow instanceof NetherCrux crux
            && WorldUtils.inNether(block.getWorld())
            && getPlacements().contains(crux.getId())
        ) {
            final UUID uuid = event.getPlayer().getUniqueId();
            StorageCacheUtils.setData(location, Keys.SEED_GROWTH_STAGE, "0");
            StorageCacheUtils.setData(location, Keys.BLOCK_OWNER, uuid.toString());
            ownerCache.put(location, uuid);
            return;
        }
        // Wasn't placable, so cancel the event
        event.setCancelled(true);
        Slimefun.getDatabaseManager().getBlockDataController().removeBlock(location);
    }

    /**
     * This method should validate a seed's fields have been initialised correctly
     * before it's registered. This will also set the Display plant used.
     * Be sure to call super() if extending further
     */
    @OverridingMethodsMustInvokeSuper
    protected abstract boolean validateSeed();

    /**
     * Sets the {@link Growth} of the plant
     *
     * @param growth The required {@link Growth}
     */
    @Nonnull
    public NetherSeed setGrowth(@Nonnull Growth growth) {
        this.description = growth;
        String[] lore = new String[0];
        if (this.getItem().getItemMeta().hasLore()) {
            lore = this.getItem().getItemMeta().getLore().toArray(lore);
        }
        this.displayPlant = new CustomItemStack(
            this.description.getFullyGrownPlant(),
            this.getItemName(),
            lore
        );
        return this;
    }

    /**
     * Adds a possible BreedingPair that will result in this seed as a child.
     * Can have multiple pairs resulting in the same child.
     *
     * @param mother       The ID of the potential Mother
     * @param father       The ID of the potential Mother
     * @param breedChance  The chance for the breed to return this seed
     * @param spreadChance The chance that the Mother will spread
     * @return Returns self
     */
    @Nonnull
    @ParametersAreNonnullByDefault
    public NetherSeed addBreedingPair(String mother, String father, double breedChance, double spreadChance) {
        this.breedingPairs.add(new BreedingPair(this, mother, father, breedChance, spreadChance));
        return this;
    }

    /**
     * Gets all the possible ways this plant can be bred
     *
     * @return The {@link Set} of {@link BreedingPair}s this plant can be bred from
     */
    @Nonnull
    public Set<BreedingPair> getBreedingPairs() {
        return this.breedingPairs;
    }

    /**
     * Adds a flavour profile to this seed. When registered, a NetherSeed with a FlavourProfile
     * will register its paste version
     *
     * @param sweet  The sweetness of this seed
     * @param sour   The sourness of this seed
     * @param salty  The saltiness of this seed
     * @param bitter The bitterness of this seed
     * @param umami  The unamai(ness?) of this seed
     * @return Returns self
     */
    @Nonnull
    @ParametersAreNonnullByDefault
    public NetherSeed addFlavourProfile(int sweet, int sour, int salty, int bitter, int umami) {
        this.flavourProfile = new FlavourProfile(sweet, sour, salty, bitter, umami);
        return this;
    }

    /**
     * Adds a flavour profile to this seed. When registered, a NetherSeed with a FlavourProfile
     * will register it's powdered version as well its paste version
     *
     * @param flavourProfile The {@link FlavourProfile} for this item
     * @return Returns self
     */
    @Nonnull
    @ParametersAreNonnullByDefault
    public NetherSeed addFlavourProfile(@Nonnull FlavourProfile flavourProfile) {
        this.flavourProfile = flavourProfile;
        return this;
    }

    @Override
    @Nullable
    public FlavourProfile getFlavourProfile() {
        return this.flavourProfile;
    }

    /**
     * Tries to register the seed (if it passes validation) first into the Registry, then its
     * breeding pairs and finally with Slimefun.
     *
     * @param addon The addon registering this Seed
     */
    public void tryRegister(@Nonnull SlimefunAddon addon) {
        if (validateSeed()) {
            if (this.flavourProfile != null) {
                registerPaste();
            }
            if (this.description == null) {
                Netheopoiesis.logWarning(this.getId() + " has no Growth, it will not be registered.");
            } else {
                Registry.getInstance().addPlant(this);
                register(addon);
            }
        }
    }

    private void registerPaste() {
        final SlimefunItemStack stack = Theme.themedSlimefunItemStack(
            "NPS_PASTE_" + this.getId().replace("NPS_", ""),
            Material.RABBIT_STEW,
            Theme.PASTE,
            "下界浆糊: " + ChatColor.stripColor(this.getItemName()),
            "这种江湖富含营养,",
            "是猪灵的最爱.",
            "可以做成美味的下界丸子.",
            "",
            Theme.CLICK_INFO.asTitle("甜", this.flavourProfile.getSweet()),
            Theme.CLICK_INFO.asTitle("酸", this.flavourProfile.getSour()),
            Theme.CLICK_INFO.asTitle("咸", this.flavourProfile.getSalty()),
            Theme.CLICK_INFO.asTitle("苦", this.flavourProfile.getBitter()),
            Theme.CLICK_INFO.asTitle("鲜", this.flavourProfile.getUmami())
        );
        this.crushingDrop = stack;

        final Paste paste = new Paste(
            Groups.PASTES,
            stack,
            RecipeTypes.CRUSHING,
            RecipeTypes.createCrushingRecipe(this),
            this.flavourProfile
        );
        paste.register(Netheopoiesis.getInstance());
    }

    @Nullable
    @Override
    public ItemStack crushingDrop() {
        return this.crushingDrop;
    }

    @Nonnull
    @Override
    public GrowthStages getGrowthStages() {
        return description == null ? GrowthStages.VINEY_RED : description.getStages();
    }

    @Nullable
    public ItemStack getDisplayPlant() {
        return displayPlant;
    }

    @Nonnull
    @Override
    public Theme getTheme() {
        return description == null ? Theme.SEED : description.getStages().getTheme();
    }

    @Nonnull
    @Override
    public Set<String> getPlacements() {
        return description == null ? Placements.NULL : description.getPlacements();
    }

    @Override
    public double getGrowthRate() {
        return (description == null ? 0.05 : description.getGrowthRate()) * Netheopoiesis.GROWTH_RATE_MULTIPLIER;
    }

    @Override
    public int getPurificationValue() {
        return (description == null ? 0 : description.getPurificationValue());
    }

    private void growthDisplay(@Nonnull Location location) {
        ParticleUtils.randomSpread(WorldUtils.centre(location), 0.5, 4, getTheme().getDustOptions(1f));
    }

    private void finalGrowthDisplay(@Nonnull Location location) {
        ParticleUtils.randomSpread(WorldUtils.centre(location), Particle.WAX_ON, 0.5, 4);
    }

    private void breedInvalidDisplay(@Nonnull Location location) {
        ParticleUtils.randomSpread(WorldUtils.centre(location), 0.5, 2, new Particle.DustOptions(Color.BLACK, 1));
    }

    private void breedSuccess(@Nonnull Location location) {
        ParticleUtils.randomSpread(WorldUtils.centre(location), ParticleX.ITEM_SLIME, 0.5, 4);
    }
}
