/*
 * This file is a part of the SteamWar software.
 *
 * Copyright (C) 2021  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.region.flags.flagvalues.*;
import de.steamwar.bausystem.shared.EnumDisplay;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.EnumSet;
import java.util.Set;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
public enum Flag implements EnumDisplay {

	COLOR("FLAG_COLOR", ColorMode.class, ColorMode.YELLOW, false),
	TNT("FLAG_TNT", TNTMode.class, TNTMode.ONLY_TB, true),
	FIRE("FLAG_FIRE", FireMode.class, FireMode.ALLOW, true),
	FREEZE("FLAG_FREEZE", FreezeMode.class, FreezeMode.INACTIVE, true),
	PROTECT("FLAG_PROTECT", ProtectMode.class, ProtectMode.ACTIVE, true),
	ITEMS("FLAG_ITEMS", ItemMode.class, ItemMode.INACTIVE, true),
	NO_GRAVITY("FLAG_NO_GRAVITY", NoGravityMode.class, NoGravityMode.INACTIVE, true),
	;

	@Getter
	private static final Set<Flag> flags;
	@Getter
	private static final Set<Flag> resetFlags;

	static {
		flags = EnumSet.allOf(Flag.class);
		resetFlags = flags.stream().filter(flag -> flag.reset).collect(Collectors.toUnmodifiableSet());
	}

	private final String chatValue;
	private final Class<? extends Value<?>> valueType;
	private final Flag.Value<?> defaultValue;
	private final boolean reset;

	public Value<?> getFlagValueOf(final String name) {
		return this.defaultValue.getValueOf(name);
	}

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

	@Override
	public String getChatValue() {
		return chatValue;
	}

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

		T getValue();

		T getValueOf(final String name);

		T[] getValues();

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