import 'dart:io';

import 'package:dynamic_color/dynamic_color.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:logger/logger.dart';
import 'package:prasule/util/color_schemes.g.dart';
import 'package:prasule/views/home.dart';
import 'package:shared_preferences/shared_preferences.dart';

var _materialYou = false;
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final s = await SharedPreferences.getInstance();

  if (!Platform.isAndroid) {
    await s.setBool("useMaterialYou", false);
  }

  _materialYou = s.getBool("useMaterialYou") ?? true;
  runApp(const MyApp());
}

/// Global logger for debugging
final logger = Logger();

/// The application itself
class MyApp extends StatelessWidget {
  /// The application itself
  const MyApp({super.key, this.locale});

  /// If Material You was applied
  ///
  /// Used to check if it is supported
  static bool appliedYou = false;

  /// Override locale, used for testing
  final Locale? locale;
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return (Platform.isAndroid)
        ? DynamicColorBuilder(
            builder: (light, dark) {
              appliedYou = light != null;
              return MaterialApp(
                debugShowCheckedModeBanner: false,
                localizationsDelegates: const [
                  AppLocalizations.delegate,
                  ...GlobalMaterialLocalizations.delegates,
                  ...GlobalCupertinoLocalizations.delegates,
                ],
                supportedLocales: AppLocalizations.supportedLocales,
                locale: locale,
                title: 'Prašule',
                theme: ThemeData(
                  colorScheme: _materialYou
                      ? light ?? lightColorScheme
                      : lightColorScheme,
                  useMaterial3: true,
                ),
                darkTheme: ThemeData(
                  useMaterial3: true,
                  colorScheme:
                      _materialYou ? dark ?? darkColorScheme : darkColorScheme,
                ),
                home: const HomeView(),
              );
            },
          )
        : Theme(
            data: ThemeData(
              useMaterial3: true,
              colorScheme:
                  (MediaQuery.of(context).platformBrightness == Brightness.dark)
                      ? darkColorScheme
                      : lightColorScheme,
            ),
            child: const CupertinoApp(
              localizationsDelegates: [
                AppLocalizations.delegate,
                ...GlobalMaterialLocalizations.delegates,
                ...GlobalCupertinoLocalizations.delegates,
              ],
              title: 'Prašule',
              home: HomeView(),
            ),
          );
  }
}
