/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 SteamWar.de-Serverteam
 *
 *   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 <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.features.tracer.rendering;

import lombok.Getter;
import lombok.Setter;

import java.util.*;

/**
 * A holder for the view data of a player trace render
 */
public class PlayerTraceShowData {

    /**
     * The bundle filter applied by this class
     */
    @Getter
    @Setter
    private BundleFilter bundleFilter;

    /**
     * A map for stating whether a flag is contained in this holder or not
     */
    private final Map<Class<? extends ViewFlag>, ViewFlag> viewFlags = new HashMap<>();

    public PlayerTraceShowData(BundleFilter bundleFilter, ViewFlag... viewFlags) {
        this.bundleFilter = bundleFilter;
        for (ViewFlag viewFlag : viewFlags) {
            this.viewFlags.put(viewFlag.getClass(), viewFlag);
        }
    }

    /**
     * A methode that returns the flags that should be used by renders according
     * to this holder. Especially handles inverse and required flags
     *
     * @return the flags that should be used in a render according to this holder
     */
    public Set<ViewFlag> getEffectiveViewFlags() {
        // Manage flags and required flags
        Set<ViewFlag> flagList = new HashSet<>();
        for (ViewFlag flag : viewFlags.values()) {
            flagList.add(flag);
            if (flag.required != null) {
                flagList.addAll(Arrays.asList(flag.required));
            }
        }

        // Manage inverse flags
        ViewFlag.inverseFlags.forEach(viewFlag -> {
            if (!flagList.remove(viewFlag)) {
                flagList.add(viewFlag);
            }
        });

        return flagList;
    }

    public boolean hasNoViewFlags() {
        return viewFlags.isEmpty();
    }

    public boolean hasViewFlag(Class<? extends ViewFlag> clazz) {
        return viewFlags.containsKey(clazz);
    }

    public boolean hasViewFlagOnly(Class<? extends ViewFlag> clazz) {
        return viewFlags.containsKey(clazz) && viewFlags.size() == 1;
    }

    // TODO ?
    @SuppressWarnings("unchecked")
    public <T extends ViewFlag> T getViewFlag(Class<T> clazz) {
        return (T) viewFlags.get(clazz);
    }

    public void addViewFlag(ViewFlag viewFlag) {
        viewFlags.put(viewFlag.getClass(), viewFlag);
    }

    public <T extends ViewFlag> void removeViewFlag(Class<T> clazz) {
        viewFlags.remove(clazz);
    }
}
