package com.tiedup.remake.v2.blocks;

import com.tiedup.remake.core.TiedUpMod;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;

/**
 * Block entity for Pet Bed.
 * Used by Master to make pets sleep. Players must crouch to use it.
 */
public class PetBedBlockEntity extends ObjBlockEntity {

    public static final ResourceLocation MODEL_LOCATION =
        ResourceLocation.fromNamespaceAndPath(
            TiedUpMod.MOD_ID,
            "models/obj/blocks/bed/model.obj"
        );

    public static final ResourceLocation TEXTURE_LOCATION =
        ResourceLocation.fromNamespaceAndPath(
            TiedUpMod.MOD_ID,
            "textures/block/pet_bed.png"
        );

    /** UUID of the pet currently using this bed (null if empty) */
    @Nullable
    private UUID occupantUUID = null;

    /** UUID of the Master who owns this bed (null = public) */
    @Nullable
    private UUID ownerUUID = null;

    public PetBedBlockEntity(
        BlockEntityType<?> type,
        BlockPos pos,
        BlockState state
    ) {
        super(type, pos, state);
    }

    @Override
    public ResourceLocation getModelLocation() {
        return MODEL_LOCATION;
    }

    @Override
    public ResourceLocation getTextureLocation() {
        return TEXTURE_LOCATION;
    }

    @Override
    public float getModelScale() {
        return 0.8f; // Scale down to fit in block
    }

    @Override
    public float[] getModelOffset() {
        return new float[] { 0.0f, 0.0f, 0.0f };
    }

    // OCCUPANCY

    public boolean isOccupied() {
        return occupantUUID != null;
    }

    @Nullable
    public UUID getOccupantUUID() {
        return occupantUUID;
    }

    /**
     * Have a pet occupy this bed.
     *
     * @param petUUID The pet's UUID
     * @return true if successful, false if already occupied
     */
    public boolean occupy(UUID petUUID) {
        if (isOccupied()) return false;

        this.occupantUUID = petUUID;
        setChanged();
        return true;
    }

    /**
     * Release the current occupant.
     */
    public void release() {
        this.occupantUUID = null;
        setChanged();
    }

    // OWNERSHIP

    @Nullable
    public UUID getOwnerUUID() {
        return ownerUUID;
    }

    public void setOwner(@Nullable UUID masterUUID) {
        this.ownerUUID = masterUUID;
        setChanged();
    }

    public boolean isOwnedBy(UUID uuid) {
        return ownerUUID != null && ownerUUID.equals(uuid);
    }

    public boolean isPublic() {
        return ownerUUID == null;
    }

    // NBT

    @Override
    protected void saveAdditional(CompoundTag tag) {
        super.saveAdditional(tag);
        if (occupantUUID != null) {
            tag.putUUID("occupant", occupantUUID);
        }
        if (ownerUUID != null) {
            tag.putUUID("owner", ownerUUID);
        }
    }

    @Override
    public void load(CompoundTag tag) {
        super.load(tag);
        if (tag.hasUUID("occupant")) {
            this.occupantUUID = tag.getUUID("occupant");
        }
        if (tag.hasUUID("owner")) {
            this.ownerUUID = tag.getUUID("owner");
        }
    }
}
