package de.gecheckt.pdf.umbenenner.adapter.in.gui.editor;

import java.nio.file.Path;
import java.util.Objects;
import java.util.Properties;

/**
 * Snapshot of a configuration file loaded from disk.
 * <p>
 * This type is intentionally separate from the editable editor state. It represents the file
 * that was loaded, not the mutable values currently being edited.
 */
public final class GuiConfigurationFileSnapshot {

    private final Path filePath;
    private final Properties properties;

    /**
     * Creates a snapshot for the given file and properties.
     *
     * @param filePath path of the loaded file; must not be {@code null}
     * @param properties loaded properties; must not be {@code null}
     */
    public GuiConfigurationFileSnapshot(Path filePath, Properties properties) {
        this.filePath = Objects.requireNonNull(filePath, "filePath must not be null");
        this.properties = copyOf(properties);
    }

    /**
     * Returns the file path of the loaded configuration.
     *
     * @return the snapshot file path; never {@code null}
     */
    public Path filePath() {
        return filePath;
    }

    /**
     * Returns a defensive copy of the loaded properties.
     *
     * @return a copy of the loaded properties; never {@code null}
     */
    public Properties properties() {
        return copyOf(properties);
    }

    /**
     * Returns whether this snapshot was loaded from the given path.
     *
     * @param candidatePath the path to compare; may be {@code null}
     * @return {@code true} when the paths match, otherwise {@code false}
     */
    public boolean hasFilePath(Path candidatePath) {
        return filePath.equals(candidatePath);
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof GuiConfigurationFileSnapshot that)) {
            return false;
        }
        return filePath.equals(that.filePath) && properties.equals(that.properties);
    }

    @Override
    public int hashCode() {
        return Objects.hash(filePath, properties);
    }

    @Override
    public String toString() {
        return "GuiConfigurationFileSnapshot{"
                + "filePath=" + filePath
                + ", properties=" + properties
                + '}';
    }

    private static Properties copyOf(Properties source) {
        Properties copy = new Properties();
        copy.putAll(Objects.requireNonNull(source, "properties must not be null"));
        return copy;
    }
}
