package com.tiedup.remake.network.action;

import com.tiedup.remake.network.base.AbstractProgressPacketWithRole;
import com.tiedup.remake.state.PlayerBindState;
import com.tiedup.remake.tasks.PlayerStateTask;
import java.util.function.BiConsumer;
import java.util.function.Function;
import net.minecraft.network.FriendlyByteBuf;

/**
 * Packet for synchronizing untying progress from server to client.
 *
 * Sent by server to update the client's untying task progress.
 * Includes role info so the progress bar can show different text
 * for helper vs victim.
 */
public class PacketUntying extends AbstractProgressPacketWithRole {

    /**
     * Create a new untying progress packet with role info.
     *
     * @param stateInfo Current elapsed time (-1 for completion/cancel)
     * @param maxState Total duration
     * @param isHelper true if the recipient is doing the untying
     * @param otherEntityName Name of the other party
     */
    public PacketUntying(
        int stateInfo,
        int maxState,
        boolean isHelper,
        String otherEntityName
    ) {
        super(stateInfo, maxState, isHelper, otherEntityName);
    }

    /**
     * Decode the packet from the network buffer.
     */
    public static PacketUntying decode(FriendlyByteBuf buf) {
        RolePacketData data = decodeRoleFields(buf);
        return new PacketUntying(
            data.stateInfo(),
            data.maxState(),
            data.isActiveRole(),
            data.otherEntityName()
        );
    }

    @Override
    protected Function<PlayerBindState, PlayerStateTask> getTaskGetter() {
        return state -> state.tasks().getClientUntyingTask();
    }

    @Override
    protected BiConsumer<PlayerBindState, PlayerStateTask> getTaskSetter() {
        return (state, task) -> state.tasks().setClientUntyingTask(task);
    }
}
