/*
 * This file is part of Lithium
 *
 * Lithium is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Lithium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Lithium. If not, see <https://www.gnu.org/licenses/>.
 */

package org.leavesmc.leaves.lithium.common.block.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.entity.TickingBlockEntity;
import org.jetbrains.annotations.NotNull;

public class SleepUntilTimeBlockEntityTickInvoker implements TickingBlockEntity {

    private final BlockEntity sleepingBlockEntity;
    private long sleepUntilTickExclusive;
    private final TickingBlockEntity delegate;

    public SleepUntilTimeBlockEntityTickInvoker(BlockEntity sleepingBlockEntity, long sleepUntilTickExclusive, TickingBlockEntity delegate) {
        this.sleepingBlockEntity = sleepingBlockEntity;
        this.sleepUntilTickExclusive = sleepUntilTickExclusive;
        this.delegate = delegate;
    }

    @Override
    public void tick() {
        //noinspection ConstantConditions
        long tickTime = this.sleepingBlockEntity.getLevel().getRedstoneGameTime(); // Luminol - Regionized threading for sleeping block entity
        if (tickTime >= this.sleepUntilTickExclusive) {
            ((SleepingBlockEntity) this.sleepingBlockEntity).setTicker(this.delegate);
            this.delegate.tick();
        }
    }

    @Override
    public boolean isRemoved() {
        return this.sleepingBlockEntity.isRemoved();
    }

    @Override
    @NotNull
    public BlockPos getPos() {
        return this.sleepingBlockEntity.getBlockPos();
    }

    @Override
    @NotNull
    public String getType() {
        //noinspection ConstantConditions
        return BlockEntityType.getKey(this.sleepingBlockEntity.getType()).toString();
    }

    @Override
    @NotNull
    public BlockEntity getTileEntity() {
        return this.sleepingBlockEntity;
    }

    @Override
    public void updateTicksForLithium(long redstoneGameTimeOffset) {
        this.sleepUntilTickExclusive += redstoneGameTimeOffset;
    }
}