# TiedUp! 1.20.1 - Makefile
# Simplifies build commands using Java 17

# Java 17 is required for Forge 1.20.1
JAVA_HOME := /usr/lib/jvm/java-17-openjdk
export JAVA_HOME

# Gradle wrapper
GRADLE := ./gradlew

# Mod info (keep in sync with gradle.properties)
MOD_NAME := TiedUp
MOD_VERSION := 0.5.6-ALPHA
JAR_FILE := build/libs/tiedup-$(MOD_VERSION).jar

# Colors for output
COLOR_RESET := \033[0m
COLOR_GREEN := \033[32m
COLOR_YELLOW := \033[33m
COLOR_BLUE := \033[34m

# Default target
.DEFAULT_GOAL := help

##@ General

.PHONY: help
help: ## Display this help message
	@echo ""
	@echo "$(COLOR_BLUE)TiedUp! 1.20.1 - Makefile Commands$(COLOR_RESET)"
	@echo ""
	@awk 'BEGIN {FS = ":.*##"; printf "Usage:\n  make $(COLOR_YELLOW)<target>$(COLOR_RESET)\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf "  $(COLOR_YELLOW)%-15s$(COLOR_RESET) %s\n", $$1, $$2 } /^##@/ { printf "\n$(COLOR_GREEN)%s$(COLOR_RESET)\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
	@echo ""

##@ Building

.PHONY: build
build: ## Build the mod
	@echo "$(COLOR_GREEN)Building $(MOD_NAME) with Java 17...$(COLOR_RESET)"
	@$(GRADLE) build

.PHONY: clean
clean: ## Clean build artifacts
	@echo "$(COLOR_YELLOW)Cleaning build files...$(COLOR_RESET)"
	@$(GRADLE) clean

.PHONY: rebuild
rebuild: clean build ## Clean and rebuild

.PHONY: release
release: clean ## Build for release (clean + build)
	@echo "$(COLOR_GREEN)Building release version of $(MOD_NAME)...$(COLOR_RESET)"
	@$(GRADLE) build
	@mkdir -p build/release
	@cp build/reobfJar/output.jar build/release/tiedup-$(MOD_VERSION).jar
	@echo "$(COLOR_GREEN)Release build complete!$(COLOR_RESET)"
	@ls -lh build/release/

##@ Running

.PHONY: run
run: ## Run Minecraft client
	@echo "$(COLOR_GREEN)Launching Minecraft client...$(COLOR_RESET)"
	@$(GRADLE) runClient

.PHONY: server
server: ## Run dedicated server
	@echo "$(COLOR_GREEN)Launching dedicated server...$(COLOR_RESET)"
	@$(GRADLE) runServer

.PHONY: client2
client2: ## Run second client instance
	@echo "$(COLOR_GREEN)Launching client 2...$(COLOR_RESET)"
	@$(GRADLE) runClient2

.PHONY: client3
client3: ## Run third client instance
	@echo "$(COLOR_GREEN)Launching client 3...$(COLOR_RESET)"
	@$(GRADLE) runClient3

.PHONY: data
data: ## Run data generators
	@echo "$(COLOR_GREEN)Running data generators...$(COLOR_RESET)"
	@$(GRADLE) runData

##@ Multiplayer Testing

.PHONY: mptest
mptest: mp-kill build _mp-setup ## Build and launch server + 2 clients
	@echo "$(COLOR_GREEN)Starting multiplayer test environment...$(COLOR_RESET)"
	@echo "$(COLOR_BLUE)Launching 3 terminals: Server, Dev1, Dev2$(COLOR_RESET)"
	@konsole --new-tab -e bash -c "cd '$(CURDIR)' && $(GRADLE) runServer; read -p 'Press enter to close'" &
	@sleep 2
	@konsole --new-tab -e bash -c "sleep 12 && cd '$(CURDIR)' && $(GRADLE) runClient; read -p 'Press enter to close'" &
	@konsole --new-tab -e bash -c "sleep 17 && cd '$(CURDIR)' && $(GRADLE) runClient2; read -p 'Press enter to close'" &
	@echo "$(COLOR_GREEN)Terminals launched. Clients will auto-connect after server starts.$(COLOR_RESET)"

.PHONY: _mp-setup
_mp-setup:
	@mkdir -p run run-client2 run-client3
	@echo "eula=true" > run/eula.txt
	@if [ ! -f run/server.properties ]; then \
		echo "# TiedUp Dev Server" > run/server.properties; \
		echo "online-mode=false" >> run/server.properties; \
		echo "enable-command-block=true" >> run/server.properties; \
		echo "spawn-protection=0" >> run/server.properties; \
		echo "gamemode=creative" >> run/server.properties; \
		echo "allow-flight=true" >> run/server.properties; \
	else \
		grep -q "online-mode=false" run/server.properties || \
		sed -i 's/online-mode=true/online-mode=false/' run/server.properties; \
	fi

.PHONY: mp-kill
mp-kill: ## Kill all Minecraft processes and clean locks
	@echo "$(COLOR_YELLOW)Killing all Minecraft/Java processes...$(COLOR_RESET)"
	@-fuser -k 25565/tcp 2>/dev/null; true
	@-pkill -9 -f "net.minecraft.server.Main" 2>/dev/null; true
	@-pkill -9 -f "net.minecraft.client.main.Main" 2>/dev/null; true
	@-pkill -9 -f "MinecraftServer" 2>/dev/null; true
	@-pkill -9 -f "cpw.mods.bootstraplauncher" 2>/dev/null; true
	@sleep 1
	@-rm -f run/world/session.lock run/*/session.lock 2>/dev/null; true
	@-rm -f run-client2/*/session.lock run-client3/*/session.lock 2>/dev/null; true
	@echo "$(COLOR_GREEN)Done$(COLOR_RESET)"

##@ Development

.PHONY: test
test: ## Run tests
	@$(GRADLE) test

.PHONY: idea
idea: ## Generate IntelliJ IDEA run configurations
	@$(GRADLE) genIntellijRuns

.PHONY: eclipse
eclipse: ## Generate Eclipse project files
	@$(GRADLE) eclipse

##@ Formatting

.PHONY: format
format: ## Format code using Prettier
	@npx --yes prettier --plugin prettier-plugin-java --tab-width 4 --write "src/**/*.java"

.PHONY: check-format
check-format: ## Check code formatting
	@npx --yes prettier --plugin prettier-plugin-java --tab-width 4 --check "src/**/*.java"

##@ Information

.PHONY: info
info: ## Show project information
	@echo ""
	@echo "$(COLOR_BLUE)  TiedUp! - Project Information$(COLOR_RESET)"
	@echo "$(COLOR_GREEN)Mod:$(COLOR_RESET)        $(MOD_NAME) $(MOD_VERSION)"
	@echo "$(COLOR_GREEN)Minecraft:$(COLOR_RESET)  1.20.1"
	@echo "$(COLOR_GREEN)Forge:$(COLOR_RESET)      47.4.10"
	@echo "$(COLOR_GREEN)Java:$(COLOR_RESET)       17 ($(JAVA_HOME))"
	@echo ""

.PHONY: version
version: ## Show Java and Gradle versions
	@$(JAVA_HOME)/bin/java -version
	@$(GRADLE) --version | head -5

##@ Maintenance

.PHONY: refresh
refresh: ## Refresh Gradle dependencies
	@$(GRADLE) --refresh-dependencies

.PHONY: setup
setup: ## Initial setup (first time)
	@echo "$(COLOR_GREEN)Running initial setup...$(COLOR_RESET)"
	@$(GRADLE) wrapper
	@echo "$(COLOR_GREEN)Setup complete! Run 'make build' to build the mod.$(COLOR_RESET)"

.PHONY: all
all: clean build ## Clean and build
