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

import net.minecraft.commands.Commands
import net.minecraft.commands.arguments.EntityArgument
import net.minecraft.world.entity.player.Player
import net.neoforged.neoforge.event.RegisterCommandsEvent
import net.neoforged.neoforge.server.command.EnumArgument
import ru.dbotthepony.mc.otm.capability.MatteryPlayer.UpgradeType
import ru.dbotthepony.mc.otm.capability.matteryPlayer

object ExopackCommand {
	fun register(event: RegisterCommandsEvent) {
		event.dispatcher.register(
			Commands.literal("exopack")
				.requires { it.hasPermission(Commands.LEVEL_GAMEMASTERS) }
				.then(Commands.literal("add")
					.executes { setExoPack(listOf(it.source.playerOrException), true) }
					.then(Commands.argument("targets", EntityArgument.players()).executes { setExoPack(EntityArgument.getPlayers(it, "targets"), true) })
				)
				.then(Commands.literal("remove")
					.executes { setExoPack(listOf(it.source.playerOrException), false) }
					.then(Commands.argument("targets", EntityArgument.players()).executes { setExoPack(EntityArgument.getPlayers(it, "targets"), false) })
				)
				.then(Commands.literal("upgrade")
					.then(Commands.argument("type", EnumArgument.enumArgument(UpgradeType::class.java))
						.then(Commands.literal("add")
							.executes { setUpgrade(listOf(it.source.playerOrException), it.getArgument("type", UpgradeType::class.java), true) }
							.then(Commands.argument("targets", EntityArgument.players()).executes { setUpgrade(
								EntityArgument.getPlayers(it, "targets"), it.getArgument("type", UpgradeType::class.java), true) }))
						.then(Commands.literal("remove")
							.executes { setUpgrade(listOf(it.source.playerOrException), it.getArgument("type", UpgradeType::class.java), false) }
							.then(Commands.argument("targets", EntityArgument.players()).executes { setUpgrade(
								EntityArgument.getPlayers(it, "targets"), it.getArgument("type", UpgradeType::class.java), false) })))
				)
		)
	}

	private fun setExoPack(players: Collection<Player>, hasExoPack: Boolean): Int {
		for (player in players) {
			player.matteryPlayer.hasExopack = hasExoPack
			// player.matteryPlayer.recreateExoPackMenu()
		}

		return players.size
	}

	private fun setUpgrade(players: Collection<Player>, type: UpgradeType, state: Boolean): Int {
		for (player in players) {
			player.matteryPlayer.let { type.prop.set(it, state) }
		}

		return players.size
	}
}
