import 'package:flutter/material.dart';
import 'package:habo/model/habo_model.dart';
import 'package:habo/settings/settings_manager.dart';
import 'package:habo/repositories/repository_factory.dart';
import 'package:habo/services/backup_service.dart';
import 'package:habo/services/notification_service.dart';
import 'package:habo/services/ui_feedback_service.dart';
import 'package:habo/services/biometric_auth_service.dart';
import 'package:habo/services/home_widget_service.dart';

class ServiceLocator {
  static final ServiceLocator _instance = ServiceLocator._internal();
  static ServiceLocator get instance => _instance;
  ServiceLocator._internal();

  GlobalKey<ScaffoldMessengerState>? _scaffoldKey;
  HaboModel? _haboModel;
  SettingsManager? _settingsManager;

  RepositoryFactory? _repositoryFactory;
  BackupService? _backupService;
  NotificationService? _notificationService;
  UIFeedbackService? _uiFeedbackService;
  BiometricAuthService? _biometricAuthService;
  HomeWidgetService? _homeWidgetService;

  RepositoryFactory get repositoryFactory => _repositoryFactory!;
  BackupService get backupService => _backupService!;
  NotificationService get notificationService => _notificationService!;
  UIFeedbackService get uiFeedbackService => _uiFeedbackService!;
  BiometricAuthService get biometricAuthService => _biometricAuthService!;
  HomeWidgetService get homeWidgetService => _homeWidgetService!;

  void initialize(
    GlobalKey<ScaffoldMessengerState> scaffoldKey,
    HaboModel haboModel,
    SettingsManager settingsManager,
  ) {
    _scaffoldKey = scaffoldKey;
    _haboModel = haboModel;
    _settingsManager = settingsManager;

    _repositoryFactory = RepositoryFactory(haboModel);
    _backupService = BackupService(
      null, // uiFeedbackService - would be _uiFeedbackService
      null, // backupRepository
      habitRepository: _repositoryFactory!.habitRepository,
      eventRepository: _repositoryFactory!.eventRepository,
      categoryRepository: _repositoryFactory!.categoryRepository,
    );
    _notificationService = NotificationService();
    _uiFeedbackService = UIFeedbackService(scaffoldKey);
    _biometricAuthService = BiometricAuthService();
    _homeWidgetService = HomeWidgetService();
  }

  void reset() {
    _scaffoldKey = null;
    _haboModel = null;
    _settingsManager = null;
    _repositoryFactory = null;
    _backupService = null;
    _notificationService = null;
    _uiFeedbackService = null;
    _biometricAuthService = null;
    _homeWidgetService = null;
  }
}
