//general
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:aman_kassa_flutter/shared/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_screenutil/screenutil_init.dart';
import 'package:google_fonts/google_fonts.dart';
//service & tools
import 'package:aman_kassa_flutter/redux/store.dart';
import 'core/locator.dart';
import 'core/router.dart';
import 'core/services/navigator_service.dart';
import 'core/services/dialog_service.dart';
import './widgets/dialog_manager.dart';
//pages
import 'views/start_up/start_up_view.dart';


//main start
void main() async {
  HttpOverrides.global = MyHttpOverrides();

  WidgetsFlutterBinding.ensureInitialized();
  HttpOverrides.global = MyHttpOverrides();
  //initialize locator
  await LocatorInjector.setupLocator();

  LicenseRegistry.addLicense(() async* {
    final license = await rootBundle.loadString('assets/google_fonts/OFL.txt');
    yield LicenseEntryWithLineBreaks(['google_fonts'], license);
  });

  await Redux.init();
  runApp(MainApplication());
}

class MainApplication extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return StoreProvider<AppState>(
        store: Redux.store,
        child: ScreenUtilInit(
          designSize: Size(411.43, 683.43),
          allowFontScaling: false,
          builder: () => MaterialApp(
            theme: ThemeData(
              backgroundColor: backgroundColor,
              primaryColor: primaryColor,
              accentColor: yellowColor,
              scaffoldBackgroundColor: Colors.white,
                textTheme: GoogleFonts.latoTextTheme(
                  Theme.of(context).textTheme,
                )
            ),
            debugShowCheckedModeBanner: false,
            builder: (context, child) => Navigator(
              key: locator<DialogService>().dialogNavigationKey,
              onGenerateRoute: (settings) => MaterialPageRoute(
                  builder: (context) => DialogManager(child: child)),
            ),
            navigatorKey: locator<NavigatorService>().navigatorKey,
            home: StartUpView(), // first page
            onGenerateRoute: generateRoute,
          ),
        ),
      );
  }
}

class MyHttpOverrides extends HttpOverrides{
  @override
  HttpClient createHttpClient(SecurityContext context){
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
  }
}