CXX = g++
CXXFLAGS = -std=c++17 -Wall -pthread -O2
AUDIO_LIBS = -lSDL2 -lmpg123 -lm
GL_LIBS = -lglut -lGLEW -lGLU -lGL 
LIBS = $(GL_LIBS) $(AUDIO_LIBS)

TARGET = kielimallikansalainen
SRC = pixel_grid_audio.cpp
AUDIO_LIB = libaudioanalysis.a

# Check if audio library exists
ifeq ($(wildcard $(AUDIO_LIB)),)
$(error Audio library $(AUDIO_LIB) not found. Please run 'make -f Makefile_mp3' first to build it.)
endif

$(TARGET): $(SRC) $(AUDIO_LIB)
	$(CXX) $(CXXFLAGS) -o $@ $< -L. -laudioanalysis $(LIBS)
	@echo "✅ Audio-reactive pixel grid built successfully!"
	@echo "🎵 Usage: ./$(TARGET) [mp3_file.mp3]"

# Build the audio library if needed
$(AUDIO_LIB):
	@echo "Building audio library..."
	$(MAKE) -f Makefile_mp3

# Check dependencies
check-deps:
	@echo "🔍 Checking for required dependencies..."
	@pkg-config --exists sdl2 || (echo "❌ SDL2 not found. Install with: sudo apt-get install libsdl2-dev" && exit 1)
	@pkg-config --exists libmpg123 || (echo "❌ libmpg123 not found. Install with: sudo apt-get install libmpg123-dev" && exit 1)
	@pkg-config --exists glew || (echo "❌ GLEW not found. Install with: sudo apt-get install libglew-dev" && exit 1)
	@echo "✅ All dependencies found! (Note: GLUT and OpenGL assumed available)"

# Build both audio library and application
all: check-deps $(AUDIO_LIB) $(TARGET)

# Test with default MP3 file
test: $(TARGET)
	@if [ -f "contemporaneo-psy-matthsammcore-243522.mp3" ]; then \
		echo "🎵 Testing with default MP3 file..."; \
		./$(TARGET) contemporaneo-psy-matthsammcore-243522.mp3; \
	else \
		echo "🎵 No default MP3 file found. Usage: ./$(TARGET) your_music.mp3"; \
		./$(TARGET); \
	fi

# Clean build files
clean:
	rm -f $(TARGET)
	@echo "🧹 Cleaned audio pixel grid build files"

# Clean everything including audio library
clean-all: clean
	$(MAKE) -f Makefile_mp3 clean
	@echo "🧹 Cleaned all build files"

# Show usage information
info:
	@echo "🎵 Audio-Reactive Pixel Grid Visualizer"
	@echo "======================================"
	@echo "Target: $(TARGET)"
	@echo "Source: $(SRC)"
	@echo "Audio Library: $(AUDIO_LIB)"
	@echo ""
	@echo "Build Commands:"
	@echo "  make all       - Build everything"
	@echo "  make           - Build application (requires audio library)"
	@echo "  make test      - Build and run with default MP3"
	@echo "  make clean     - Clean application build files"
	@echo "  make clean-all - Clean everything"
	@echo "  make check-deps - Check for required dependencies"
	@echo ""
	@echo "Usage:"
	@echo "  ./$(TARGET)                    - Run without audio"
	@echo "  ./$(TARGET) your_music.mp3     - Run with your MP3 file"
	@echo ""
	@echo "Controls:"
	@echo "  SPACE - Play/Pause audio"
	@echo "  S     - Stop audio"
	@echo "  R     - Restart audio"
	@echo "  I     - Show audio info"
	@echo "  H     - Show help"
	@echo "  ESC   - Exit"

.PHONY: all check-deps test clean clean-all info 
