package com.infinityraider.agricraft.content.world;

import com.infinityraider.agricraft.AgriCraft;
import com.infinityraider.agricraft.api.v1.AgriApi;
import com.infinityraider.agricraft.content.core.BlockCrop;
import com.infinityraider.agricraft.content.core.TileEntityCrop;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessor;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureProcessorType;
import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.util.Optional;

@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
public class CropStickProcessor extends StructureProcessor {
    private static final String FIELD_GENOME = "IL_FIELD_0";
    private static final String FIELD_GROWTH = "IL_FIELD_1";

    public static final Codec<CropStickProcessor> CODEC = RecordCodecBuilder.create(instance -> instance.group(
            ResourceLocation.CODEC.optionalFieldOf("structure", new ResourceLocation("empty")).forGetter(CropStickProcessor::getStructure))
            .apply(instance, instance.stable(CropStickProcessor::new))
    );

    private final ResourceLocation structure;

    public CropStickProcessor(ResourceLocation structure) {
        this.structure = structure;
    }

    public ResourceLocation getStructure() {
        return this.structure;
    }

    @Override
    protected StructureProcessorType<?> getType() {
        return AgriCraft.instance.getStructureRegistry().cropStickProcessor;
    }

    @Nullable
    @Override
    public StructureTemplate.StructureBlockInfo process(LevelReader world, BlockPos structureOrigin, BlockPos parentOrigin, StructureTemplate.StructureBlockInfo templateInfo, StructureTemplate.StructureBlockInfo worldInfo, StructurePlaceSettings settings, @Nullable StructureTemplate template) {
        // check if the block is a crop block
        BlockState state = worldInfo.state;
        if (!(state.getBlock() instanceof BlockCrop)) {
            return worldInfo;
        }
        // check if the crop block is just single crop sticks
        BlockCrop blockCrop = (BlockCrop) state.getBlock();
        if (!blockCrop.hasCropSticks(state) || blockCrop.hasCrossSticks(state) || blockCrop.hasPlantOrWeeds(state)) {
            return worldInfo;
        }
        // set the nbt to load to the tile entity
        if (worldInfo.nbt.contains(FIELD_GENOME, Tag.TAG_COMPOUND) && worldInfo.nbt.contains(FIELD_GROWTH, Tag.TAG_COMPOUND)) {
            AgriApi.getWorldGenPlantManager().generateGenomeFor(this.getStructure(), settings.getRandom(worldInfo.pos)).ifPresent(genome -> {
                TileEntityCrop.GENOME_WRITER.accept(Optional.of(genome), worldInfo.nbt.getCompound(FIELD_GENOME));
                TileEntityCrop.GROWTH_WRITER.accept(genome.getPlant().getInitialGrowthStage(), worldInfo.nbt.getCompound(FIELD_GROWTH));
            });
        }
        // return the world info
        return worldInfo;
    }
}

@@ -3050,16 +3050,34 @@
         
+ boolean planted =
  AgriApi
@@ -3181,17 +3181,11 @@
 s)).
-ifPresent
+map
 (gen
@@ -3441,24 +3441,510 @@
 D_GROWTH));%0A
+                return true;%0A            %7D).orElse(false);%0A            // if a plant has been set, we must also set the block state to indicate that there is a plant%0A            if(planted) %7B%0A                return new StructureTemplate.StructureBlockInfo(worldInfo.pos, blockCrop.applyPlant(state), worldInfo.nbt.copy());%0A            %7D else %7B%0A                AgriCraft.instance.getLogger().debug(%22Skipped generation of random plant as none are registered for %22 + this.getStructure());%0A
             
@@ -3944,18 +3944,16 @@
        %7D
-);
 %0A       

Fix crop sticks generating with wrong block state in villages