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 tying progress from server to client.
 *
 * Sent by server to update the client's tying task progress.
 * Includes role info so the progress bar can show different text
 * for kidnapper vs victim.
 */
public class PacketTying extends AbstractProgressPacketWithRole {

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

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

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

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