package com.tiedup.remake.tasks;

import net.minecraft.world.level.Level;

/**
 *
 * Based on original TimedTask from 1.12.2, refactored for continuous click requirement.
 *
 * A TimedTask represents a progressive action that requires continuous clicking:
 * - Progress increases when player clicks on target (update() called)
 * - Progress decreases when player is not clicking (activeThisTick = false)
 * - Task completes when progress reaches maxProgress
 * - Task cancels when progress drops to 0
 *
 * This ensures players must HOLD click on the target to complete the action.
 */
public abstract class TimedTask {

    protected int progress = 0; // Current progress (ticks)
    protected int maxProgress; // Target progress (seconds * 20 ticks)
    protected boolean activeThisTick = false; // Was update() called this tick?
    protected boolean stopped = false; // Task has been stopped/completed
    protected final Level level; // World reference
    protected final int seconds; // Total duration in seconds (for display)

    /**
     * Create a new timed task.
     *
     * @param seconds Total duration in seconds
     * @param level The world (for game time)
     */
    public TimedTask(int seconds, Level level) {
        this.seconds = seconds;
        this.maxProgress = seconds * 20; // Convert seconds to ticks
        this.level = level;
        this.stopped = false;
    }

    /**
     * Start the task.
     * Resets progress and stopped state.
     */
    public void start() {
        this.progress = 0;
        this.stopped = false;
        this.activeThisTick = false;
    }

    /**
     * Called when player clicks on target.
     * Marks this tick as "active" - progress will increase.
     */
    public synchronized void update() {
        activeThisTick = true;
    }

    /**
     * Called every tick to process progress.
     * Increments if active (clicking), decrements if not.
     */
    public void tick() {
        if (stopped) return;

        if (activeThisTick) {
            // Player is clicking on target - progress up
            progress++;
            if (progress >= maxProgress) {
                // Task complete!
                onComplete();
            }
        } else {
            // Player is not clicking - progress down
            progress--;
            if (progress <= 0) {
                progress = 0;
                stop();
            }
        }

        // Reset for next tick
        activeThisTick = false;
    }

    /**
     * Reset the task to its initial state.
     */
    public void reset() {
        this.progress = 0;
        this.stopped = false;
        this.activeThisTick = false;
    }

    /**
     * Stop/cancel the task.
     * Marks the task as stopped.
     */
    public void stop() {
        this.stopped = true;
    }

    /**
     * Check if the task has been stopped.
     *
     * @return true if stopped
     */
    public boolean isStopped() {
        return stopped;
    }

    /**
     * Check if the task has become outdated.
     * In the new progress-based system, a task is outdated if progress is 0.
     *
     * @return true if outdated (no progress)
     */
    public boolean isOutdated() {
        return progress <= 0;
    }

    /**
     * Get the current progress in ticks.
     *
     * @return Current progress ticks
     */
    public int getProgress() {
        return progress;
    }

    /**
     * Get the max progress in ticks.
     *
     * @return Max progress ticks
     */
    public int getMaxProgress() {
        return maxProgress;
    }

    /**
     * Get the current state (elapsed time in seconds).
     * Calculates from progress ticks for display compatibility.
     *
     * @return Elapsed seconds (progress / 20)
     */
    public int getState() {
        return progress / 20;
    }

    /**
     * Get progress as a percentage (0.0 to 1.0).
     *
     * @return Progress percentage
     */
    public float getProgressPercent() {
        return (float) progress / maxProgress;
    }

    /**
     * Check if the task is complete (progress >= maxProgress).
     *
     * @return true if task is done
     */
    public boolean isOver() {
        return progress >= maxProgress;
    }

    /**
     * Check if the task is complete.
     *
     * @return true if complete
     */
    public boolean isComplete() {
        return progress >= maxProgress;
    }

    /**
     * Get the total duration in seconds.
     *
     * @return Total seconds
     */
    public int getMaxSeconds() {
        return seconds;
    }

    /**
     * Get the remaining seconds.
     *
     * @return Remaining seconds
     */
    public int getSecondsRemaining() {
        return Math.max(0, (maxProgress - progress) / 20);
    }

    /**
     * Called when the task completes successfully.
     * Subclasses should override to implement completion logic.
     */
    protected abstract void onComplete();

    /**
     * Send progress packets to relevant players.
     * Subclasses should override to send UI updates.
     */
    public abstract void sendProgressPackets();
}
