import 'dart:async';
import 'dart:convert';
import 'package:xp_nix/src/interfaces/i_activity_detector.dart';
import 'package:xp_nix/src/models/activity_event.dart';

/// Mock implementation of IActivityDetector for testing
class MockActivityDetector implements IActivityDetector {
  final StreamController<ActivityEvent> _activityController = StreamController<ActivityEvent>.broadcast();
  final List<ActivitySequenceItem> _activitySequence = [];
  int _currentSequenceIndex = 0;
  Timer? _sequenceTimer;

  String _currentApp = 'unknown';
  String _currentTitle = '';

  @override
  Stream<ActivityEvent> get activityStream => _activityController.stream;

  @override
  Future<void> start() async {
    // Start playing the activity sequence if one is set
    if (_activitySequence.isNotEmpty) {
      _playNextActivity();
    }
  }

  @override
  void stop() {
    _sequenceTimer?.cancel();
    _activityController.close();
  }

  @override
  Future<ActivityEvent> getCurrentActivity() async {
    return ActivityEvent(
      type: ActivityEventType.focusedBrowsing,
      application: '',
      metadata: '',
      timestamp: DateTime.now(),
    );
  }

  /// Set a sequence of activities to simulate
  void setActivitySequence(List<ActivitySequenceItem> sequence) {
    _activitySequence.clear();
    _activitySequence.addAll(sequence);
    _currentSequenceIndex = 0;
  }

  /// Manually trigger an activity change
  void simulateActivity(String application, String title, {Duration? duration}) {
    _currentApp = application;
    _currentTitle = title;

    final event = ActivityEvent(
      type: ActivityEventType.categorize(applicationId: application, applicationTitle: title),
      application: application,
      metadata: jsonEncode({'title': title, 'duration': duration?.inSeconds}),
      timestamp: DateTime.now(),
    );

    print('MockActivityDetector: Emitting event for $application - ${event.type}');
    _activityController.add(event);
  }

  /// Play the next activity in the sequence
  void _playNextActivity() {
    if (_currentSequenceIndex >= _activitySequence.length) {
      return; // Sequence complete
    }

    final item = _activitySequence[_currentSequenceIndex];
    simulateActivity(item.application, item.title, duration: item.duration);

    _currentSequenceIndex++;

    // Schedule next activity
    if (_currentSequenceIndex < _activitySequence.length) {
      _sequenceTimer = Timer(item.duration, _playNextActivity);
    }
  }
}

/// Represents an activity in a simulation sequence
class ActivitySequenceItem {
  final String application;
  final String title;
  final Duration duration;

  const ActivitySequenceItem({required this.application, required this.title, required this.duration});

  /// Factory for coding activity
  factory ActivitySequenceItem.coding({
    String application = 'vscode',
    String title = 'main.dart - MyProject',
    required Duration duration,
  }) => ActivitySequenceItem(application: application, title: title, duration: duration);

  /// Factory for browsing activity
  factory ActivitySequenceItem.browsing({
    String application = 'firefox',
    String title = 'Stack Overflow - How to...',
    required Duration duration,
  }) => ActivitySequenceItem(application: application, title: title, duration: duration);

  /// Factory for meeting activity
  factory ActivitySequenceItem.meeting({
    String application = 'zoom',
    String title = 'Team Standup Meeting',
    required Duration duration,
  }) => ActivitySequenceItem(application: application, title: title, duration: duration);

  /// Factory for collaboration activity
  factory ActivitySequenceItem.collaboration({
    String application = 'slack',
    String title = 'General Channel',
    required Duration duration,
  }) => ActivitySequenceItem(application: application, title: title, duration: duration);
}
