import 'dart:html';

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:mitra_corporate/screen/splash_screen.dart/splash_screen.dart';

import 'app/constant.dart';
import 'app/route.dart';

import 'app/register_webview_rtub.dart'
    if (dart.library.html) 'app/register_webview.dart';

void main() {
  // runApp(const MyApp());
  // registerWebViewWebImplementation();
  WidgetsFlutterBinding.ensureInitialized();
  Constant.setBaseUrl(window.location.host, window.location.protocol);
  runApp(ProviderScope(observers: [], child: MyApp()));

  // FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  // runApp(ProviderScope(observers: [Logger()], child: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // localizationsDelegates: [
      //   DefaultMaterialLocalizations.delegate,
      //   DefaultCupertinoLocalizations.delegate,
      //   DefaultWidgetsLocalizations.delegate,
      // ],
      // locale: Locale("id", "ID"),
      debugShowCheckedModeBanner: false,
      title: 'App Mitra',
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      // supportedLocales: [
      //   Locale('en', ''),
      //   Locale('id', ''), // arabic, no country code
      // ],
      theme: ThemeData(
          useMaterial3: true,
          colorScheme: ColorScheme.light(
            brightness: Brightness.light,
            primary: Constant.primaryRed,
            surfaceTint: Color(0xff904a46),
            onPrimary: Color(0xffffffff),
            primaryContainer: Color(0xffffdad7),
            onPrimaryContainer: Color(0xff3b0809),
            secondary: Color(0xff775654),
            onSecondary: Color(0xffffffff),
            secondaryContainer: Color(0xffffdad7),
            onSecondaryContainer: Color(0xff2c1514),
            tertiary: Color(0xff735b2e),
            onTertiary: Color(0xffffffff),
            tertiaryContainer: Color(0xffffdea7),
            onTertiaryContainer: Color(0xff271900),
            error: Color(0xffba1a1a),
            onError: Color(0xffffffff),
            errorContainer: Color(0xffffdad6),
            onErrorContainer: Color(0xff410002),
            surface: Color(0xfffff8f7),
            onSurface: Color(0xff231919),
            onSurfaceVariant: Color(0xff534342),
            outline: Color(0xff857371),
            outlineVariant: Color(0xffd8c2c0),
            shadow: Color(0xff000000),
            scrim: Color(0xff000000),
            inverseSurface: Color(0xff382e2d),
            inversePrimary: Color(0xffffb3ae),
            primaryFixed: Color(0xffffdad7),
            onPrimaryFixed: Color(0xff3b0809),
            primaryFixedDim: Color(0xffffb3ae),
            onPrimaryFixedVariant: Color(0xff733330),
            secondaryFixed: Color(0xffffdad7),
            onSecondaryFixed: Color(0xff2c1514),
            secondaryFixedDim: Color(0xffe7bdb9),
            onSecondaryFixedVariant: Color(0xff5d3f3d),
            tertiaryFixed: Color(0xffffdea7),
            onTertiaryFixed: Color(0xff271900),
            tertiaryFixedDim: Color(0xffe2c28c),
            onTertiaryFixedVariant: Color(0xff594319),
            surfaceDim: Color(0xffe8d6d4),
            surfaceBright: Color(0xfffff8f7),
            surfaceContainerLowest: Color(0xffffffff),
            surfaceContainerLow: Color(0xfffff0ef),
            surfaceContainer: Color(0xfffceae8),
            surfaceContainerHigh: Color(0xfff6e4e2),
            surfaceContainerHighest: Color(0xfff1dedd),
          ),
          scaffoldBackgroundColor: Constant.backgroundColor,
          dialogBackgroundColor: Colors.white,
          buttonTheme: ButtonThemeData(
            buttonColor: Constant.primaryRed,
          ),
          cardColor: Colors.white,
          cardTheme: CardTheme(color: Colors.white),
          datePickerTheme: DatePickerThemeData(
              locale: Locale("id", 'ID'),
              backgroundColor: Colors.white,
              headerForegroundColor: Colors.white,
              headerBackgroundColor: Constant.primaryRed),
          elevatedButtonTheme: ElevatedButtonThemeData(
              style: ButtonStyle(
                  backgroundColor: WidgetStateProperty.resolveWith((states) {
                    if (states.contains(WidgetState.pressed)) {
                      return Constant.primaryRed.withOpacity(0.5);
                    }
                    return Constant.primaryRed;
                  }),
                  shape: WidgetStatePropertyAll(RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8))))),
          fontFamily: "OpenSans",
          primaryColor: Constant.primaryRed,
          primaryColorLight: Constant.primaryRed,
          primarySwatch: MaterialColor(0xffFF0000, {
            0: Color(0xffFF0000),
            50: Color(0xffFF0000),
            100: Color(0xffFF0000),
            200: Color(0xffFF0000),
            300: Color(0xffFF0000),
            400: Color(0xffFF0000),
            500: Color(0xffFF0000),
            600: Color(0xffFF0000),
            700: Color(0xffFF0000),
            800: Color(0xffFF0000),
            900: Color(0xffFF0000)
          }),
          // primarySwatch: Colors.blue,
          visualDensity: VisualDensity.adaptivePlatformDensity),
      initialRoute: splashRoute,

      onGenerateRoute: AppRoute.generateRoute,
      home: SplashScreen(),
    );
  }
}

class Logger extends ProviderObserver {
  @override
  void didUpdateProvider(
    ProviderBase<Object?> provider,
    Object? previousValue,
    Object? newValue,
    ProviderContainer container,
  ) {
    // var a = jsonEncode(newValue);
    print('''
{
  "provider": "${provider.name ?? provider.runtimeType}",
  "newValue": "$newValue"
}''');
  }
}
