// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:get_it/get_it.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
import 'package:remever/common/storage.dart';
import 'package:remever/services/core/theme_service.dart';

// Project imports:

/// Описание функции для построения содержимого [ThemeModeListener]
typedef ThemeModeBuilder =
    Widget Function(BuildContext context, ThemeMode themeMode);

class ThemeModeListener extends StatelessWidget {
  ///
  /// Слушатель изменений темы
  ///
  const ThemeModeListener({required this.builder, super.key});

  /// Построитель
  final ThemeModeBuilder builder;

  @override
  Widget build(BuildContext context) {
    final ThemeService service = GetIt.I.get<ThemeService>();

    return ValueListenableBuilder<Box<ThemeMode>>(
      valueListenable: hiveTheme.listenable(),
      builder: (BuildContext context, _, Widget? child) {
        if (child != null) return child;

        return builder(context, service.themeMode);
      },
    );
  }
}
