import 'package:flutter/material.dart';

/// 전역 테마 모드를 관리하는 컨트롤러.
class ThemeController extends ChangeNotifier {
  ThemeController({ThemeMode initialMode = ThemeMode.system})
    : _mode = initialMode;

  ThemeMode _mode;

  ThemeMode get mode => _mode;

  void update(ThemeMode mode) {
    if (_mode == mode) return;
    _mode = mode;
    notifyListeners();
  }

  void cycle() {
    switch (_mode) {
      case ThemeMode.system:
        update(ThemeMode.light);
        break;
      case ThemeMode.light:
        update(ThemeMode.dark);
        break;
      case ThemeMode.dark:
        update(ThemeMode.system);
        break;
    }
  }
}

/// [ThemeController]를 하위 위젯에 제공하는 Inherited 위젯.
class ThemeControllerScope extends InheritedNotifier<ThemeController> {
  const ThemeControllerScope({
    super.key,
    required ThemeController controller,
    required super.child,
  }) : super(notifier: controller);

  static ThemeController of(BuildContext context) {
    final scope = context
        .dependOnInheritedWidgetOfExactType<ThemeControllerScope>();
    assert(scope != null, 'ThemeControllerScope가 위젯 트리에 없습니다.');
    return scope!.notifier!;
  }
}
