package com.tiedup.remake.v2.bondage.component;

import com.google.gson.JsonObject;
import java.util.function.Function;
import org.jetbrains.annotations.Nullable;

public enum ComponentType {
    LOCKABLE("lockable", LockableComponent::fromJson),
    RESISTANCE("resistance", ResistanceComponent::fromJson),
    GAGGING("gagging", GaggingComponent::fromJson);

    private final String jsonKey;
    private final Function<JsonObject, IItemComponent> factory;

    ComponentType(
        String jsonKey,
        Function<JsonObject, IItemComponent> factory
    ) {
        this.jsonKey = jsonKey;
        this.factory = factory;
    }

    public String getJsonKey() {
        return jsonKey;
    }

    public IItemComponent create(JsonObject config) {
        return factory.apply(config);
    }

    @Nullable
    public static ComponentType fromKey(String key) {
        for (ComponentType type : values()) {
            if (type.jsonKey.equals(key)) return type;
        }
        return null;
    }
}
