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

import java.util.EnumSet;
import java.util.Set;

@Getter
public enum Flag implements EnumDisplay {

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

	@Getter
	private static final Set<Flag> flags;

	static {
		flags = EnumSet.allOf(Flag.class);
	}

	private final String chatValue;
	private final Class<? extends Value<?>> valueType;
	private final Flag.Value<?> defaultValue;
	private final Value<?>[] values;

	<T extends Enum<T> & Value<T>> Flag(String chatValue, final Class<? extends Value<T>> valueType, final Flag.Value<T> defaultValue) {
		this.chatValue = chatValue;
		this.valueType = valueType;
		this.defaultValue = defaultValue;
		this.values = defaultValue.getValues();
	}

	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();
		}
	}
}