/*
 *   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.region.flags;

import de.steamwar.bausystem.shared.EnumDisplay;
import lombok.Getter;
import lombok.NonNull;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public final class Flag<T extends Enum<T> & Flag.Value<T>> implements EnumDisplay {

    @Getter
    private static final Set<Flag> flags = new HashSet<>();

    private static AtomicInteger counter = new AtomicInteger(0);

    public static final Flag<ColorMode> COLOR = new Flag<>("COLOR", "FLAG_COLOR", ColorMode.class, ColorMode.YELLOW);
    public static final Flag<TNTMode> TNT = new Flag<>("TNT", "FLAG_TNT", TNTMode.class, TNTMode.ONLY_TB);
    public static final Flag<FireMode> FIRE = new Flag<>("FIRE", "FLAG_FIRE", FireMode.class, FireMode.DENY);
    public static final Flag<FreezeMode> FREEZE = new Flag<>("FREEZE", "FLAG_FREEZE", FreezeMode.class, FreezeMode.INACTIVE);
    public static final Flag<ProtectMode> PROTECT = new Flag<>("PROTECT", "FLAG_PROTECT", ProtectMode.class, ProtectMode.ACTIVE);
    public static final Flag<ItemMode> ITEMS = new Flag<>("ITEMS", "FLAG_ITEMS", ItemMode.class, ItemMode.INACTIVE);
    public static final Flag<NoGravityMode> NO_GRAVITY = new Flag<>("NO_GRAVITY", "FLAG_NO_GRAVITY", NoGravityMode.class, NoGravityMode.INACTIVE);
    public static final Flag<TestblockMode> TESTBLOCK = new Flag<>("TESTBLOCK", "FLAG_TESTBLOCK", TestblockMode.class, TestblockMode.NO_VALUE);
    public static final Flag<ChangedMode> CHANGED = new Flag<>("CHANGED", "FLAG_CHANGED", ChangedMode.class, ChangedMode.NO_CHANGE);

    private String name;
    private int ordinal;

    @Getter
    private String chatValue;

    @Getter
    private final Class<? extends Flag.Value<T>> valueType;

    @Getter
    private final T defaultValue;

    @Getter
    private final Flag.Value<?>[] values;

    private Flag(String name, String chatValue, Class<? extends Flag.Value<T>> valueType, T defaultValue) {
        flags.add(this);

        this.name = name;
        this.ordinal = counter.getAndIncrement();
        this.chatValue = chatValue;

        this.valueType = valueType;
        this.defaultValue = defaultValue;
        this.values = defaultValue.getValues();
    }

    public static Flag<?> valueOf(@NonNull String name) {
        for (Flag<?> flag : flags) {
            if (flag.name.equalsIgnoreCase(name)) {
                return flag;
            }
        }
        throw new IllegalArgumentException("No enum constant Flag." + name);
    }

    public Flag.Value<?> valueOfValue(@NonNull String name) {
        for (Flag.Value<?> value : values) {
            if (value.name().equalsIgnoreCase(name)) {
                return value;
            }
        }
        throw new IllegalArgumentException("No enum constant Flag." + this.name + "." + name);
    }

    public String name() {
        return name;
    }

    public int ordinal() {
        return ordinal;
    }

    public boolean oneOf(Flag<?>... flags) {
        for (Flag<?> flag : flags) {
            if (flag == this) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        return name;
    }

    public interface Value<T extends Enum<T> & Value<T>> extends EnumDisplay {

        String name();

        int ordinal();

        T getValue();

        T getValueOf(final String name);

        T[] getValues();

        default String getName() {
            return this.getValue().name().toLowerCase();
        }
    }
}
