package p.packer.services;

import p.packer.messages.PackerProjectContext;
import p.packer.messages.InitWorkspaceRequest;
import p.packer.models.PackerRegistryEntry;
import p.packer.models.PackerRegistryState;
import p.packer.models.PackerRuntimeAsset;
import p.packer.models.PackerRuntimeSnapshot;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class PackerRuntimeLoader implements PackerRuntimeSnapshotLoader {
    private final PackerWorkspaceFoundation workspaceFoundation;
    private final PackerAssetDeclarationParser parser;

    public PackerRuntimeLoader(
            PackerWorkspaceFoundation workspaceFoundation,
            PackerAssetDeclarationParser parser) {
        this.workspaceFoundation = Objects.requireNonNull(workspaceFoundation, "workspaceFoundation");
        this.parser = Objects.requireNonNull(parser, "parser");
    }

    @Override
    public PackerRuntimeSnapshot load(PackerProjectContext project, long generation) {
        final PackerProjectContext safeProject = Objects.requireNonNull(project, "project");
        workspaceFoundation.initWorkspace(new InitWorkspaceRequest(safeProject));

        final PackerRegistryState registry = workspaceFoundation.loadRegistry(safeProject);
        final Map<Path, PackerRegistryEntry> registryByRoot = registry.assets().stream()
                .collect(Collectors.toMap(
                        entry -> PackerWorkspacePaths.assetRoot(safeProject, entry.root()),
                        entry -> entry));

        final List<PackerRuntimeAsset> assets = new ArrayList<>();
        final Path assetsRoot = PackerWorkspacePaths.assetsRoot(safeProject);
        if (Files.isDirectory(assetsRoot)) {
            try (Stream<Path> paths = Files.find(assetsRoot, Integer.MAX_VALUE, (path, attrs) ->
                    attrs.isRegularFile() && path.getFileName().toString().equals("asset.json"))) {
                final List<Path> manifests = paths
                        .map(path -> path.toAbsolutePath().normalize())
                        .sorted(Comparator.naturalOrder())
                        .toList();
                for (Path manifestPath : manifests) {
                    final Path assetRoot = manifestPath.getParent();
                    final Optional<PackerRegistryEntry> registryEntry = Optional.ofNullable(registryByRoot.get(assetRoot));
                    assets.add(new PackerRuntimeAsset(
                            assetRoot,
                            manifestPath,
                            registryEntry,
                            parser.parse(manifestPath)));
                }
            } catch (IOException exception) {
                throw new p.packer.exceptions.PackerRegistryException(
                        "Unable to build runtime snapshot for " + safeProject.projectId(),
                        exception);
            }
        }

        return new PackerRuntimeSnapshot(generation, registry, assets);
    }
}
