import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:traderscircuit/resources/routes/route_name.dart';
import 'package:traderscircuit/resources/routes/routes.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  SharedPreferences prefs = await SharedPreferences.getInstance();
  // GlobalVariables globalVariables = GlobalVariables();
  //token = prefs.getString('token');
//  OnBoard = prefs.getBool("OnBoard");
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
  ]).then((value) => runApp(const MyApp()));
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  var _connectionStatus = ConnectivityResult.values.toString();
  late StreamSubscription<ConnectivityResult> subscription;
  Connectivity connectivity = Connectivity();

  // @override
  // void initState() {
  //   super.initState();
  //   WidgetsBinding.instance.addObserver(this);

  //   connectivity = Connectivity();
  //   checkInternet();
  //   subscription =
  //       connectivity.onConnectivityChanged.listen((ConnectivityResult result) {
  //     _connectionStatus = result.toString();
  //     if (result == ConnectivityResult.wifi ||
  //         result == ConnectivityResult.mobile) {
  //       setState(() {
  //         _connectionStatus = result.toString();

  //         Get.back(result: true);
  //       });
  //     } else {
  //       setState(() {
  //         _connectionStatus = result.toString();
  //         Get.toNamed(RouteName.nointernet);
  //       });
  //     }
  //   });
  //   // print(_connectionStatus);
  // }

  // Future<void> checkInternet() async {
  //   final connectivityResult = await (Connectivity().checkConnectivity());

  //   if (connectivityResult == ConnectivityResult.wifi ||
  //       connectivityResult == ConnectivityResult.mobile) {
  //     setState(() {
  //       _connectionStatus = connectivityResult.toString();
  //     });
  //   } else {
  //     setState(() {
  //       _connectionStatus = connectivityResult.toString();
  //       print(_connectionStatus.toString());
  //       Get.toNamed(RouteName.nointernet);

  //       //  Navigator.pushReplacementNamed(context, "/noInternet");
  //     });
  //   }
  // }

  // @override
  // void dispose() {
  //   super.dispose();
  //   WidgetsBinding.instance.removeObserver(this);

  //   subscription.cancel();
  // }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      builder: (BuildContext context, Widget? child) => GetMaterialApp(
        title: 'Trader\'s Circuit',
        theme: ThemeData(
          primarySwatch: Colors.blue,
          fontFamily: 'manrope',
          //  textTheme: GoogleFonts.manropeTextTheme(),
        ),
        debugShowCheckedModeBanner: false,
        initialRoute: RouteName.mainscreen,

        //initialRoute: RouteName.mainScreen,
        getPages: AppRoutes.appRoutes(),
      ),
      designSize: const Size(390, 844),
    );
  }
}
