package ru.dbotthepony.mc.otm.server.command

import com.mojang.brigadier.StringReader
import com.mojang.brigadier.arguments.ArgumentType
import com.mojang.brigadier.context.CommandContext
import com.mojang.brigadier.suggestion.Suggestions
import com.mojang.brigadier.suggestion.SuggestionsBuilder
import net.minecraft.commands.CommandSourceStack
import net.minecraft.commands.SharedSuggestionProvider
import net.minecraft.resources.ResourceLocation
import ru.dbotthepony.mc.otm.android.AndroidResearchManager
import ru.dbotthepony.mc.otm.android.AndroidResearchType
import java.util.concurrent.CompletableFuture

class AndroidResearchArgument : ArgumentType<AndroidResearchType> {
	override fun parse(reader: StringReader): AndroidResearchType {
		return AndroidResearchManager[ResourceLocation.read(reader)] ?: throw NoSuchElementException()
	}

	override fun <S : Any?> listSuggestions(context: CommandContext<S>, builder: SuggestionsBuilder): CompletableFuture<Suggestions> {
		return SharedSuggestionProvider.suggest(AndroidResearchManager.researchMap.map { it.key.toString() }, builder)
	}

	companion object {
		fun create(): AndroidResearchArgument = AndroidResearchArgument()

		fun getResearchType(context: CommandContext<CommandSourceStack>, name: String): AndroidResearchType {
			return context.getArgument(name, AndroidResearchType::class.java)
		}
	}
}
