import 'dart:io';
import '../interfaces/i_desktop_enhancer.dart';

/// Sway desktop enhancer with minimal visual changes
/// 
/// As requested, this implementation does not perform aesthetic desktop config changes
/// for Sway systems. It maintains API compatibility while providing basic notifications.
class SwayEnhancer implements IDesktopEnhancer {
  String _currentTheme = 'default';

  @override
  Future<void> celebrateLevelUp(int level) async {
    print('🎉 Celebrating level up to $level! (Sway)');
    
    // Update internal theme tracking for API compatibility
    _updateThemeForLevel(level);

    // Send celebration notification via notify-send
    try {
      await Process.run('notify-send', [
        '🎉 LEVEL UP!',
        'Welcome to Level $level!\nKeep up the great productivity!',
        '--urgency=normal',
        '--expire-time=5000',
        '--app-name=XP Nix',
      ]);
    } catch (e) {
      print('Could not send level up notification: $e');
    }

    // Optional: Try to send via Sway notification daemon if available
    await _trySwayNotification(level);
  }

  @override
  Future<void> applyThemeForLevel(int level) async {
    // Update internal theme tracking only
    _updateThemeForLevel(level);
    
    print('🎨 Theme updated to $_currentTheme for level $level (Sway - visual changes disabled)');
    
    // No actual visual changes are applied for Sway as requested
    // This maintains compatibility with the API while respecting the no-visual-changes requirement
  }

  void _updateThemeForLevel(int level) {
    if (level >= 25) {
      _currentTheme = 'legendary';
    } else if (level >= 15) {
      _currentTheme = 'master';
    } else if (level >= 10) {
      _currentTheme = 'advanced';
    } else if (level >= 5) {
      _currentTheme = 'intermediate';
    } else {
      _currentTheme = 'default';
    }
  }

  /// Try to send notification via Sway-specific methods if available
  Future<void> _trySwayNotification(int level) async {
    try {
      // Check if mako (Sway notification daemon) is running
      final makoCheck = await Process.run('pgrep', ['mako']);
      if (makoCheck.exitCode == 0) {
        // Use makoctl if available for enhanced notifications
        final makoctl = await Process.run('which', ['makoctl']);
        if (makoctl.exitCode == 0) {
          // Could add mako-specific notification features here if desired
          print('📱 Mako notification daemon detected');
        }
      }
    } catch (e) {
      // Silently ignore errors in optional notification enhancements
    }
  }

  @override
  String getCurrentThemeInfo() {
    return 'Theme: $_currentTheme (Sway - visual effects disabled)';
  }

  @override
  Future<void> refreshBaseConfig() async {
    print('🔄 Refresh base config requested (Sway - no config changes applied)');
    // No-op for Sway as we don't modify configs
  }

  @override
  Future<void> restoreBackup() async {
    print('🔧 Restore backup requested (Sway - no backups to restore)');
    // No-op for Sway as we don't create backups
  }

  /// Get information about Sway enhancer capabilities
  Map<String, dynamic> getCapabilityInfo() {
    return {
      'window_manager': 'Sway',
      'visual_enhancements': false,
      'notifications': true,
      'config_backup': false,
      'theme_tracking': true,
      'current_theme': _currentTheme,
      'supported_features': [
        'Level-up notifications',
        'Theme name tracking',
        'API compatibility',
      ],
      'disabled_features': [
        'Visual effects',
        'Config modifications',
        'Backup/restore',
        'Desktop theming',
      ],
    };
  }

  /// Check if notification systems are available
  static Future<Map<String, bool>> checkNotificationAvailability() async {
    final availability = <String, bool>{};

    try {
      // Check for notify-send (libnotify)
      final notifySend = await Process.run('which', ['notify-send']);
      availability['notify-send'] = notifySend.exitCode == 0;
    } catch (e) {
      availability['notify-send'] = false;
    }

    try {
      // Check for mako (Sway notification daemon)
      final mako = await Process.run('pgrep', ['mako']);
      availability['mako'] = mako.exitCode == 0;
    } catch (e) {
      availability['mako'] = false;
    }

    try {
      // Check for dunst (alternative notification daemon)
      final dunst = await Process.run('pgrep', ['dunst']);
      availability['dunst'] = dunst.exitCode == 0;
    } catch (e) {
      availability['dunst'] = false;
    }

    return availability;
  }
}