/*
 * Copyright (C) 2025  Tim Zimmermann <tim@aglais.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.aglais.zms.db.core.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * A utility class designed to manage and track changes to a list of data objects of type {@code T}.
 * The class maintains individual trackers for each element in the list and provides methods to
 * check if any of the elements have been modified, reset the list to its original state, and update
 * the original state of all elements to reflect their current states.
 *
 * @param <T> The type of the data objects contained in the list being managed.
 */
public class ModifiedStateTrackerList<T> implements IModifiedStateTracker<List<T>> {

    private final List<T> data;
    private final List<ModifiedStateTracker<T>> trackers = new ArrayList<>();

    /**
     * Creates a new instance of the {@code ModifiedStateTrackerList} class, initializing the current
     * and original state of each data object in the list being tracked.
     *
     * @param data The list of data objects of type {@code T} to be managed by this tracker. Must not be null.
     */
    public ModifiedStateTrackerList(List<T> data) {
        this.data = data;

        createTrackers();
    }

    @Override
    public List<T> getData() {
        return data;
    }

    @Override
    public boolean isModified() {
        // First check for deleted and changed elements
        for (ModifiedStateTracker<T> tracker : trackers) {
            if (tracker.isModified() || !data.contains(tracker.getData())) {
                return true;
            }
        }

        // Check for added elements
        for (T element : data) {
            boolean found = false;
            for (ModifiedStateTracker<T> tracker : trackers) {
                if (Objects.equals(element, tracker.getData())) {
                    found = true;
                    break;
                }
            }

            if (!found) {
                return true;
            }
        }

        return false;
    }

    @Override
    public void applyChanges() {
        createTrackers();
    }

    @Override
    public void discardChanges() {
        data.clear();

        for (ModifiedStateTracker<T> tracker : trackers) {
            tracker.discardChanges();
            data.add(tracker.getData());
        }
    }

    /**
     * Clears the current list of trackers and creates new trackers for each item in the data collection.
     * Each item is wrapped in a ModifiedStateTracker and added to the trackers list.
     */
    private void createTrackers() {
        trackers.clear();

        for (T item : data) {
            trackers.add(new ModifiedStateTracker<>(item));
        }
    }
}