#compdef r2mod io.github.Foldex.r2mod

_r2mod ()
{
	local curcontext="$curcontext" state line R2_DIR CONFIG_DIR PLUGINS_DIR TMP_DIR
	typeset -A opt_args

	if [[ -n "$R2MOD_INSTALL_DIR" ]]; then
		# Custom install location
		R2_DIR="$R2MOD_INSTALL_DIR"
	elif [[ -d "$HOME/.var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/common/Risk of Rain 2" ]]; then
		# Flatpak install
		R2_DIR="$HOME/.var/app/com.valvesoftware.Steam/.local/share/Steam/steamapps/common/Risk of Rain 2"
	else
		# Default
		R2_DIR="$HOME/.local/share/Steam/steamapps/common/Risk of Rain 2"
	fi

	BEPIN_DIR="$R2_DIR/BepInEx"
	CONFIG_DIR="$BEPIN_DIR/config"
	PLUGINS_DIR="$BEPIN_DIR/plugins"
	PLUGINS_DISABLED_DIR="$BEPIN_DIR/plugins_disabled"
	PROFILES_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/r2mod_cli/profiles"
	TMP_DIR="/tmp/r2mod"

	_arguments -C \
		':command:->command' \
		'*::options:->options'

	case $state in
		(command)

			local -a subcommands
			subcommands=(
				'check:Check for Script Updates'
				'delete:Delete a Profile'
				'disable:Disable Mods'
				'edit:Edit Mod Configs'
				'enable:Enable Mods'
				'export:Export r2modman mod profile'
				'hold:Toggle Mod Updates'
				'import:Import r2modman mod profile'
				'install:Install New Mod'
				'load:Load a Profile'
				'list:List Installed Mods'
				'refresh:Force Refresh Package Cache'
				'remove:Uninstall a mod'
				'run:Launch Risk of Rain'
				'save:Save a Profile'
				'search:Search for Mods'
				'setup:Install a Fresh BepInEx Setup'
				'uninstall:Uninstall a mod'
				'update:Update All Existing Mods'
				'version:Print Version'
			)
			_describe -t commands 'r2mod' subcommands
			;;

		(options)
			case $line[1] in
				ed | edit)
					[[ ! -d "$CONFIG_DIR" ]] && return 1
					_files -W "$CONFIG_DIR"
					;;

				en | enable)
					[[ ! -d "$PLUGINS_DISABLED_DIR" ]] && return 1
					_files -W "$PLUGINS_DISABLED_DIR" -/
					;;

				ins | install)
					[[ ! -f "$TMP_DIR/comp_cache" ]] && return 1
					local mods=( $(< "$TMP_DIR/comp_cache") )
					compadd -M 'l:|=* r:|=* m:{[:lower:]}={[:upper:]}' -a mods
					;;

				imp | import)
					case $line[2] in
						"")
							[[ ! -d "$TMP_DIR/profile" ]] && return 1
							_files -W "$TMP_DIR/profile" -F "( config new *.* *_* )" -/
							;;

						*)
							local args=( "preview" )
							[[ ${args[*]} == "$line[-2]" ]] && return 1
							compadd -a args
							;;
					esac
					;;

				li | list | ls)
					local args=()

					case $line[2] in
						"")
							args=( "all" "count" "names" )
							;;
						c | count | n | names)
							args=( "all" )
							;;
						*)
							return 1
							;;
					esac


					[[ ${args[*]} == "${line[-2]}" ]] && return 1
					compadd -a args
					;;


				loa | load | del | delete | sav | save)
					[[ ! -d "$PROFILES_DIR" ]] && return 1
					_files -W "$PROFILES_DIR" -g '*.zip(:r)'
					;;

				un | uninstall | hol | hold | rem | remove | dis | disable)
					[[ ! -d "$PLUGINS_DIR" ]] && return 1
					_files -W "$PLUGINS_DIR" -F "( *R2API* MMHOOK RoR2BepInExPack bbepis-BepInExPack-* RiskofThunder-HookGenPatcher-*)" -/
					;;

			esac
			;;
	esac
	}

_r2mod "$@"
