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

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

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;

public record AssetDetailsBankCompositionDraft(
        List<AssetWorkspaceBankCompositionFile> availableFiles,
        List<AssetWorkspaceBankCompositionFile> selectedFiles) {

    public AssetDetailsBankCompositionDraft {
        availableFiles = List.copyOf(Objects.requireNonNull(availableFiles, "availableFiles"));
        selectedFiles = List.copyOf(Objects.requireNonNull(selectedFiles, "selectedFiles"));
    }

    public AssetDetailsBankCompositionDraft removeFromAvailable(List<AssetWorkspaceBankCompositionFile> files) {
        final var removed = new LinkedHashSet<>(Objects.requireNonNull(files, "files"));
        return new AssetDetailsBankCompositionDraft(
                availableFiles.stream().filter(file -> !removed.contains(file)).toList(),
                selectedFiles);
    }

    public AssetDetailsBankCompositionDraft appendToSelected(List<AssetWorkspaceBankCompositionFile> files) {
        final var nextSelected = new java.util.ArrayList<>(selectedFiles);
        nextSelected.addAll(Objects.requireNonNull(files, "files"));
        return new AssetDetailsBankCompositionDraft(availableFiles, nextSelected);
    }

    public AssetDetailsBankCompositionDraft removeFromSelected(List<AssetWorkspaceBankCompositionFile> files) {
        final var removed = new LinkedHashSet<>(Objects.requireNonNull(files, "files"));
        return new AssetDetailsBankCompositionDraft(
                availableFiles,
                selectedFiles.stream().filter(file -> !removed.contains(file)).toList());
    }

    public AssetDetailsBankCompositionDraft appendToAvailable(List<AssetWorkspaceBankCompositionFile> files) {
        final var nextAvailable = new java.util.ArrayList<>(availableFiles);
        nextAvailable.addAll(Objects.requireNonNull(files, "files"));
        return new AssetDetailsBankCompositionDraft(nextAvailable, selectedFiles);
    }
}
