package com.tiedup.remake.tasks;

import com.tiedup.remake.state.IBondageState;
import java.util.UUID;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;

/**
 *
 * Based on original TimedInteractTask from 1.12.2
 *
 * Extends TimedTask with target tracking:
 * - Tracks the target entity's IBondageState state
 * - Can check if the same target is being interacted with (via UUID)
 * - Abstract method to set up target state
 */
public abstract class TimedInteractTask extends TimedTask {

    protected final IBondageState targetState; // The target's kidnapped state
    protected final LivingEntity targetEntity; // The target entity
    protected final UUID targetUUID; // Target's UUID for comparison

    /**
     * Create a new timed interaction task.
     *
     * @param targetState The target's IBondageState state
     * @param targetEntity The target entity
     * @param seconds Total duration in seconds
     * @param level The world
     */
    public TimedInteractTask(
        IBondageState targetState,
        LivingEntity targetEntity,
        int seconds,
        Level level
    ) {
        super(seconds, level);
        this.targetState = targetState;
        this.targetEntity = targetEntity;
        this.targetUUID = targetEntity.getUUID();
    }

    /**
     * Check if this task is targeting the same entity.
     * Uses UUID comparison to ensure uniqueness.
     *
     * @param entity The entity to compare with
     * @return true if same target
     */
    public boolean isSameTarget(LivingEntity entity) {
        if (entity == null) {
            return false;
        }
        return targetUUID.equals(entity.getUUID());
    }

    /**
     * Get the target's IBondageState state.
     *
     * @return The target's kidnapped state
     */
    public IBondageState getTargetState() {
        return targetState;
    }

    /**
     * Get the target entity.
     *
     * @return The target entity
     */
    public LivingEntity getTargetEntity() {
        return targetEntity;
    }

    /**
     * Get the target's UUID.
     *
     * @return The target's UUID
     */
    public UUID getTargetUUID() {
        return targetUUID;
    }

    /**
     * Check if the target entity is still valid (alive and exists).
     *
     * @return true if target is valid
     */
    public boolean isTargetValid() {
        return targetEntity != null && targetEntity.isAlive();
    }

    /**
     * Set up the target's state for this task.
     * This should be called when the task starts to initialize
     * any necessary state on the target.
     *
     * Implementation is task-specific (tying vs untying).
     */
    public abstract void setUpTargetState();

    /**
     * Called when the task completes successfully.
     * Default implementation does nothing - subclasses should override.
     */
    @Override
    protected void onComplete() {
        // Default: no-op, subclasses implement specific completion logic
    }

    /**
     * Send progress packets to relevant players.
     * Default implementation does nothing - subclasses should override.
     */
    @Override
    public void sendProgressPackets() {
        // Default: no-op, subclasses implement packet sending
    }
}
