package p.studio.workspaces.assets.details.bank;

import p.packer.messages.assets.AssetFamilyCatalog;
import p.studio.workspaces.assets.messages.AssetWorkspaceBankCompositionFile;

import java.util.List;
import java.util.Map;

final class AssetDetailsSoundBankCompositionFamilySupport extends AssetDetailsAbstractBankCompositionFamilySupport {
    private static final long MAX_BYTES = 1024L * 1024L;

    private final AssetBankCapacityService capacityService = new AssetBankCapacityService();
    private Map<String, Object> outputPipeline = Map.of();

    @Override
    public AssetDetailsBankCompositionDraft createDraft(p.studio.workspaces.assets.messages.AssetWorkspaceAssetDetails details) {
        outputPipeline = details.outputPipeline();
        return super.createDraft(details);
    }

    @Override
    public boolean supports(AssetFamilyCatalog assetFamily) {
        return assetFamily == AssetFamilyCatalog.SOUND_BANK;
    }

    @Override
    protected boolean canAdd(
            AssetDetailsBankCompositionDraft currentDraft,
            List<AssetWorkspaceBankCompositionFile> accepted,
            AssetWorkspaceBankCompositionFile candidate) {
        final long usedBytes = selectedBytes(currentDraft.selectedFiles()) + selectedBytes(accepted);
        return usedBytes + candidate.size() <= MAX_BYTES;
    }

    @Override
    public AssetDetailsBankCompositionCapacityState evaluate(AssetDetailsBankCompositionDraft draft) {
        return capacityService.evaluate(
                AssetFamilyCatalog.SOUND_BANK,
                draft.selectedFiles().size(),
                Map.of(),
                outputPipeline,
                selectedBytes(draft.selectedFiles()));
    }

    private long selectedBytes(List<AssetWorkspaceBankCompositionFile> files) {
        return files.stream().mapToLong(AssetWorkspaceBankCompositionFile::size).sum();
    }
}
