import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../bat_theme.dart'; // PERBAIKAN: Path import diubah di sini

/// An application that uses a custom design system.
class BatCave extends StatefulWidget {
  final GlobalKey<NavigatorState>? navigatorKey;
  final BatThemeData? theme;
  final BatThemeData? darkTheme;
  final Widget? home;
  final Map<String, WidgetBuilder>? routes;
  final String? initialRoute;
  final RouteFactory? onGenerateRoute;
  final String title;
  final Color? color;
  final Locale? locale;
  final Iterable<LocalizationsDelegate<dynamic>>? localizationsDelegates;
  final bool debugShowCheckedModeBanner;
  final ThemeMode? themeMode;

  const BatCave({
    super.key,
    this.navigatorKey,
    this.home,
    this.routes = const <String, WidgetBuilder>{},
    this.initialRoute,
    this.onGenerateRoute,
    this.title = '',
    this.color,
    this.locale,
    this.localizationsDelegates,
    this.debugShowCheckedModeBanner = true,
    this.themeMode,
    this.theme,
    this.darkTheme,
  });

  @override
  State<BatCave> createState() => _BatCaveState();
}

class _BatCaveState extends State<BatCave> {
  Widget _batBuilder(BuildContext context, Widget? child) {
    final mode = widget.themeMode ?? ThemeMode.system;
    final platformBrightness = MediaQuery.platformBrightnessOf(context);
    final useDarkTheme = mode == ThemeMode.dark ||
        (mode == ThemeMode.system && platformBrightness == Brightness.dark);

    late final ThemeData theme;
    late final BatThemeData batThemeData;

    if (useDarkTheme) {
      final colors = const BatColors.dark();
      final buttonStyle = const BatButtonStyle.dark();
      batThemeData = widget.darkTheme
              ?.copyWith(colors: colors, buttonStyle: buttonStyle) ??
          BatThemeData(colors: colors, buttonStyle: buttonStyle);
    } else {
      final colors = const BatColors.light();
      final buttonStyle = const BatButtonStyle.light();
      batThemeData =
          widget.theme?.copyWith(colors: colors, buttonStyle: buttonStyle) ??
              BatThemeData(colors: colors, buttonStyle: buttonStyle);
    }

    theme = ThemeData(
      fontFamily: 'TT Firs Neue',
      brightness: useDarkTheme ? Brightness.dark : Brightness.light,
      extensions: <ThemeExtension<dynamic>>[
        batThemeData,
      ],
    );

    return Theme(
      data: theme,
      child: DefaultTextStyle(
        style: TextStyle(
          color: useDarkTheme ? BatPalette.white : BatPalette.grey,
        ),
        child: child ?? const SizedBox.shrink(),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      key: GlobalObjectKey(this),
      navigatorKey: widget.navigatorKey,
      home: widget.home,
      routes: widget.routes!,
      initialRoute: widget.initialRoute,
      onGenerateRoute: widget.onGenerateRoute,
      builder: _batBuilder,
      title: widget.title,
      color: widget.color,
      locale: widget.locale,
      localizationsDelegates: [
        DefaultMaterialLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
        ...?widget.localizationsDelegates,
      ],
      supportedLocales: const <Locale>[Locale('en', 'US'), Locale('id', 'ID')],
      debugShowCheckedModeBanner: widget.debugShowCheckedModeBanner,
      themeMode: widget.themeMode,
    );
  }
}
