import 'package:diplomaticquarterapp/theme/theme_notifier.dart';
import 'package:diplomaticquarterapp/models/Appointments/toDoCountProviderModel.dart';
import 'package:diplomaticquarterapp/routes.dart';
import 'package:diplomaticquarterapp/services/robo_search/event_provider.dart';
import 'package:diplomaticquarterapp/services/robo_search/search_provider.dart';
import 'package:diplomaticquarterapp/theme/theme_value.dart';
import 'package:diplomaticquarterapp/uitl/HMG_Geofence.dart';
import 'package:diplomaticquarterapp/uitl/LocalNotification.dart';
import 'package:diplomaticquarterapp/uitl/PlatformBridge.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:hexcolor/hexcolor.dart';
import 'package:provider/provider.dart';

import 'Constants.dart';
import 'config/size_config.dart';
import 'core/model/geofencing/requests/GeoZonesRequestModel.dart';
import 'core/service/geofencing/GeofencingServices.dart';
import 'core/viewModels/project_view_model.dart';
import 'locator.dart';
import 'pages/pharmacies/compare-list.dart';

void main() async {
  setupLocator();
  runApp(ChangeNotifierProvider<ThemeNotifier>(create: (context) => ThemeNotifier(defaultTheme), child: MyApp()));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    PlatformBridge.init(context);
    LocalNotification.init(onNotificationClick: (payload){
      LocalNotification.getInstance().showNow(title: "Payload", subtitle: payload, payload: payload);
    });

    final themeNotifier = Provider.of<ThemeNotifier>(context);
    precacheImage(AssetImage('assets/images/powerd-by.jpg'), context);
    return LayoutBuilder(
      builder: (context, constraints) {
        return OrientationBuilder(
          builder: (context, orientation) {
            SizeConfig().init(constraints, orientation);
            return MultiProvider(
              providers: [
                ChangeNotifierProvider<ProjectViewModel>(
                  create: (context) => ProjectViewModel(),
                ),
                ChangeNotifierProvider<ToDoCountProviderModel>(create: (context) => ToDoCountProviderModel()),
                ChangeNotifierProvider<SearchProvider>(create: (context) => SearchProvider()),
                ChangeNotifierProvider.value(
                  value: SearchProvider(),
                ),
                StreamProvider.value(
                  value: RobotProvider().intStream(),
                  initialData: RobotProvider().setValue({}),
                ),
                ChangeNotifierProvider<CompareList>(
                    create: (context) => CompareList()),
              ],
              child: Consumer<ProjectViewModel>(
                builder: (context, projectProvider, child) => MaterialApp(

                  showSemanticsDebugger: false,
                  title: 'Diplomatic Quarter App',
                  locale: projectProvider.appLocal,
                  localizationsDelegates: [
                    TranslationBaseDelegate(),
                    GlobalMaterialLocalizations.delegate,
                    GlobalWidgetsLocalizations.delegate,
                    GlobalCupertinoLocalizations.delegate,
                  ],
                  supportedLocales: [
                    const Locale('ar', ''), // Arabic
                    const Locale('en', ''), // English
                  ],
                  theme: ThemeData(
                    fontFamily: projectProvider.isArabic ? 'Cairo' : 'WorkSans',
                    primarySwatch: Colors.blue,
                    visualDensity: VisualDensity.adaptivePlatformDensity,
                    brightness: Brightness.light,
                    pageTransitionsTheme: const PageTransitionsTheme(
                      builders: {
                        TargetPlatform.android: ZoomPageTransitionsBuilder(),
                        TargetPlatform.iOS: CupertinoPageTransitionsBuilder(),
                      },
                    ),
                    hintColor: Colors.grey[400],
                    textTheme: TextTheme(
                      headline1: TextStyle(
                        color: Color(0xffB8382C),
                      ),
                    ),
                    disabledColor: Colors.grey[300],
                    errorColor: Color.fromRGBO(235, 80, 60, 1.0),
                    scaffoldBackgroundColor: Color(0xffE9E9E9), // Colors.grey[100],
                    textSelectionColor: Color.fromRGBO(80, 100, 253, 0.5),
                    textSelectionHandleColor: Colors.grey,
                    canvasColor: Colors.white,
                    backgroundColor: Color.fromRGBO(255, 255, 255, 1),
                    highlightColor: Colors.grey[100].withOpacity(0.4),
                    splashColor: Colors.transparent,
                    primaryColor: Color(0xff515A5D),
                    toggleableActiveColor: secondaryColor,
                    indicatorColor: secondaryColor,
                    bottomSheetTheme: BottomSheetThemeData(backgroundColor: HexColor('#E0E0E0')),
                    cursorColor: Colors.grey,
                    iconTheme: IconThemeData(),
                    appBarTheme: AppBarTheme(
                      color: Color(0xff515A5D),
                      brightness: Brightness.light,
                      elevation: 0.0,
                      actionsIconTheme: IconThemeData(
                        color: Colors.grey[800],
                      ),
                    ),
                  ),
                  initialRoute: SPLASH,
                  routes: routes,
                  debugShowCheckedModeBanner: false,
                ),
              ),
            );
          },
        );
      },
    );
  }
}
