package com.gregtechceu.gtceu.common.machine.trait.multiblock;

import com.gregtechceu.gtceu.api.machine.trait.multiblock.MultiblockMachineTrait;
import com.gregtechceu.gtceu.api.sync_system.annotations.RerenderOnChanged;
import com.gregtechceu.gtceu.api.sync_system.annotations.SyncToClient;

import net.minecraft.core.BlockPos;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier;

import static com.gregtechceu.gtceu.api.machine.multiblock.MultiblockControllerMachine.DEFAULT_STRUCTURE;

public class MultiblockFluidRendererTrait extends MultiblockMachineTrait {

    @SyncToClient
    @RerenderOnChanged
    private Set<BlockPos> fluidBlockOffsets = new HashSet<>();

    private final Supplier<Set<BlockPos>> offsetGetter;

    public MultiblockFluidRendererTrait(Supplier<Set<BlockPos>> offsetGetter) {
        super();
        this.offsetGetter = offsetGetter;
    }

    public Set<BlockPos> getFluidOffsets() {
        if (fluidBlockOffsets.isEmpty() && getMachine().isFormed()) {
            fluidBlockOffsets = offsetGetter.get();
            syncDataHolder.markClientSyncFieldDirty("fluidBlockOffsets");
        }
        return fluidBlockOffsets;
    }

    @Override
    public void onStructureInvalid(String name) {
        if (DEFAULT_STRUCTURE.equals(name)) {
            fluidBlockOffsets.clear();
        }
    }
}
