import 'package:flutter/material.dart';

import 'app_theme.dart';
import 'theme_controller.dart';

class ThemeScope extends InheritedWidget {
  const ThemeScope({super.key, required this.controller, required super.child});

  final ThemeController controller;

  static ThemeController of(BuildContext context) {
    final scope = context.dependOnInheritedWidgetOfExactType<ThemeScope>();
    return scope?.controller ?? ThemeController.app;
  }

  @override
  bool updateShouldNotify(ThemeScope oldWidget) {
    return oldWidget.controller != controller;
  }
}

class ScopedTheme extends StatelessWidget {
  const ScopedTheme({super.key, required this.controller, required this.child});

  final ThemeController controller;
  final Widget child;

  @override
  Widget build(BuildContext context) {
    return ThemeScope(
      controller: controller,
      child: ValueListenableBuilder<ThemeMode>(
        valueListenable: controller,
        builder: (context, mode, _) {
          return Theme(
            data: mode == ThemeMode.dark ? buildDarkTheme() : buildLightTheme(),
            child: child,
          );
        },
      ),
    );
  }
}
