package com.tiedup.remake.entities.kidnapper.components;

import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.syncher.EntityDataAccessor;

/**
 * KidnapperDataSerializer - Manages NBT serialization and deserialization.
 *
 * Handles:
 * - Orchestrating component save/load (appearance, aggression, captive, state, camp, job)
 * - Direct entity data (KidnappingMode, GetOutState)
 * - Backward compatibility (AIState → KidnapperState)
 *
 * <p><b>Low complexity</b> - Already component-based, minimal orchestration needed.</p>
 */
public class KidnapperDataSerializer {

    // FIELDS

    /** Host callbacks */
    private final IDataSerializerHost host;

    /** Entity data accessor for kidnapping mode */
    private final EntityDataAccessor<Boolean> dataKidnappingMode;

    // CONSTRUCTOR

    public KidnapperDataSerializer(
        IDataSerializerHost host,
        EntityDataAccessor<Boolean> dataKidnappingMode
    ) {
        this.host = host;
        this.dataKidnappingMode = dataKidnappingMode;
    }

    // NBT SERIALIZATION

    /**
     * Save all data to NBT.
     * Orchestrates component saves and direct entity data.
     *
     * @param tag The NBT tag to save to
     */
    public void saveToNBT(CompoundTag tag) {
        // Save appearance data
        host.getAppearance().saveToNBT(tag);

        // Save aggression system data
        host.getAggressionSystem().saveToNBT(tag);

        // Save captive manager data
        host.getCaptiveManager().saveToNBT(tag);

        // Save direct entity data
        tag.putBoolean(
            "KidnappingMode",
            host.getEntityData(dataKidnappingMode)
        );
        tag.putBoolean("GetOutState", host.isGetOutState());

        // Save AI state
        tag.putString(
            host.getStateManager().getNBTKey(),
            host.getStateManager().serializeState()
        );

        // Save camp manager state
        host.getCampManager().saveToNBT(tag);

        // Save job manager state
        host.getJobManager().save(tag);
    }

    /**
     * Load all data from NBT.
     * Orchestrates component loads and direct entity data with backward compatibility.
     *
     * @param tag The NBT tag to load from
     */
    public void loadFromNBT(CompoundTag tag) {
        // Restore appearance data
        host.getAppearance().loadFromNBT(tag);

        // Restore aggression system data
        host.getAggressionSystem().loadFromNBT(tag);

        // Restore captive manager data
        host.getCaptiveManager().loadFromNBT(tag);

        // Restore direct entity data with null checks
        if (tag.contains("KidnappingMode")) {
            host.setEntityData(
                dataKidnappingMode,
                tag.getBoolean("KidnappingMode")
            );
        }
        if (tag.contains("GetOutState")) {
            host.setGetOutState(tag.getBoolean("GetOutState"));
        }

        // Load AI state with backward compatibility
        String stateKey = host.getStateManager().getNBTKey();
        if (tag.contains(stateKey)) {
            host.getStateManager().deserializeState(tag.getString(stateKey));
        } else if (tag.contains("AIState")) {
            // Backward compatibility: old saves used "AIState" instead of "KidnapperState"
            host.getStateManager().deserializeState(tag.getString("AIState"));
        }

        // Load camp manager state
        host.getCampManager().loadFromNBT(tag);

        // Load job manager state
        host.getJobManager().load(tag);
    }
}
