#!/bin/bash

# Cordova Audio Recorder Plugin Installation Script for Ionic1/AngularJS Projects
# This script ensures clean installation with proper dependency management

set -e  # Exit on any error

echo "🎵 Cordova Audio Recorder Plugin - Ionic1/AngularJS Installation"
echo "================================================================"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Function to print colored output
print_status() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# Check if we're in a Cordova project
if [ ! -f "config.xml" ]; then
    print_error "config.xml not found. Please run this script from your Cordova project root directory."
    exit 1
fi

# Check if iOS platform is added
if [ ! -d "platforms/ios" ]; then
    print_warning "iOS platform not found. Adding iOS platform..."
    cordova platform add ios
fi

# Remove existing plugin if installed
print_status "Checking for existing audio recorder plugin..."
if cordova plugin list | grep -q "cordova-plugin-audio-recorder"; then
    print_warning "Existing audio recorder plugin found. Removing..."
    cordova plugin remove cordova-plugin-audio-recorder
fi

# Install the plugin
print_status "Installing cordova-plugin-audio-recorder..."
cordova plugin add ./cordova-plugin-audio-recorder

# Verify installation
print_status "Verifying plugin installation..."
if cordova plugin list | grep -q "cordova-plugin-audio-recorder"; then
    print_success "Plugin installed successfully!"
else
    print_error "Plugin installation failed!"
    exit 1
fi

# Check iOS-specific requirements
print_status "Checking iOS platform requirements..."

# Verify frameworks are properly linked
if [ -f "platforms/ios/frameworks.json" ]; then
    if grep -q "AVFoundation.framework" platforms/ios/frameworks.json; then
        print_success "AVFoundation.framework is properly linked"
    else
        print_warning "AVFoundation.framework not found in frameworks.json"
    fi
    
    if grep -q "AudioToolbox.framework" platforms/ios/frameworks.json; then
        print_success "AudioToolbox.framework is properly linked"
    else
        print_warning "AudioToolbox.framework not found in frameworks.json"
    fi
fi

# Check Info.plist for microphone permission
if [ -f "platforms/ios/*/Info.plist" ]; then
    if grep -q "NSMicrophoneUsageDescription" platforms/ios/*/Info.plist; then
        print_success "Microphone usage description found in Info.plist"
    else
        print_warning "Microphone usage description not found in Info.plist"
    fi
fi

# Build the project
print_status "Building iOS project..."
cordova build ios

print_success "Installation completed successfully!"
echo ""
echo "📋 Next Steps:"
echo "1. Open your project in Xcode: open platforms/ios/*.xcworkspace"
echo "2. Ensure microphone permissions are properly configured"
echo "3. Test the plugin functionality in your app"
echo ""
echo "🔧 Plugin Methods Available:"
echo "  - navigator.audioRecorder.startRecording(success, error, options)"
echo "  - navigator.audioRecorder.stopRecording(success, error)"
echo "  - navigator.audioRecorder.pauseRecording(success, error)"
echo "  - navigator.audioRecorder.resumeRecording(success, error)"
echo "  - navigator.audioRecorder.getRecordingState(success, error)"
echo "  - navigator.audioRecorder.convertToBlob(filePath, success, error)"
echo "  - navigator.audioRecorder.requestPermission(success, error)"
echo "  - navigator.audioRecorder.checkPermission(success, error)"
echo "  - navigator.audioRecorder.recordWithInterval(success, error, options)"
echo ""
echo "📖 For usage examples, see the README.md file"
