import 'dart:io';

import 'package:SEFER/controller/payment/paymob/paymob_response.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:flutter_stripe/flutter_stripe.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:flutter/services.dart';
import 'package:wakelock_plus/wakelock_plus.dart';
import 'constant/api_key.dart';
import 'constant/box_name.dart';
import 'constant/credential.dart';
import 'constant/info.dart';
import 'controller/firebase/firbase_messge.dart';
import 'controller/firebase/local_notification.dart';
import 'controller/functions/device_info.dart';
import 'controller/functions/location_background_controller.dart';
import 'controller/local/local_controller.dart';
import 'controller/local/translations.dart';
import 'controller/payment/paymob/paymob_wallet.dart';
import 'firebase_options.dart';
import 'models/db_sql.dart';
import 'splash_screen_page.dart';

final box = GetStorage();
const storage = FlutterSecureStorage();
// final PaymobPayment paymobPayment = PaymobPayment();
final PaymobPayment paymobPayment = PaymobPayment();
final PaymobPaymentWallet paymobPaymentWallet = PaymobPaymentWallet();

DbSql sql = DbSql.instance;
@pragma('vm:entry-point')
Future<void> backgroundMessageHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  print('===========back===${message.notification?.title}');

  FirebaseMessagesController().fireBaseTitles(message);
}

// @pragma('vm:entry-point')
// Future<void> backgroundLocation() async {
//   await LocationBackgroundController().startBackLocation().then((location) {
//     print('This is current Location ${location.toMap()}');
//   });
// }

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  WakelockPlus.enable();
  // await LocationController().startLocationUpdates();
  if (Platform.isAndroid) {
    await NotificationController().initNotifications();
  }

  await GetStorage.init();
  // Get.put(DriverCallController());
  await AC().gAK();
  print(
      'local is    ${WidgetsBinding.instance.platformDispatcher.locale.countryCode}');

  print(Get.deviceLocale!.countryCode);
  Stripe.publishableKey = AK.publishableKey;
  if (box.read(BoxName.driverID) != null) {
    Get.put(LocationBackgroundController());
  }
  if (Platform.isAndroid || Platform.isIOS) {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
    await FirebaseMessagesController().requestFirebaseMessagingPermission();

    FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);

    List<Future> initializationTasks = [
      FirebaseMessagesController().getNotificationSettings(),
      FirebaseMessagesController().getToken(),
      // Get.put(LocationController()).startLocationUpdates(),
      // Get.put(LocationBackgroundController()).startBackLocation(),
    ];
    // cameras = await availableCameras();
    await Future.wait(initializationTasks);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }
  // PaymobPayment.instance.initialize(
  //   apiKey: AK
  //       .payMobApikey, // from dashboard Select Settings -> Account Info -> API Key
  //   integrationID: int.parse(AK.integrationIdPayMob),
  //   userTokenExpiration: 200,
  //   iFrameID: 837992,
  // );
  PaymobPayment.instance.initialize(
    apiKey: AK
        .payMobApikey, // from dashboard Select Settings -> Account Info -> API Key
    integrationID: int.parse(AK.integrationIdPayMob),
    userTokenExpiration: 200,
    iFrameID: 837992,
  );
  PaymobPaymentWallet.instance.initialize(
    apiKey: AK
        .payMobApikey, // from dashboard Select Settings -> Account Info -> API Key
    integrationID: int.parse(AK.integrationIdPayMobWallet),
    userTokenExpiration: 200,
    iFrameID: 837992,
  );
  // Get device information
  List<Map<String, dynamic>> deviceDataList =
      await DeviceInfoPlus.getDeviceInfo();

  // Print all device data
  DeviceInfoPlus.printDeviceInfo();

  runApp(const MyApp());
}

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

  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    LocaleController localController = Get.put(LocaleController());

    return GetMaterialApp(
        title: AppInformation.appName,
        translations: MyTranslation(),
        debugShowCheckedModeBanner: false,
        locale: localController.language,
        theme: localController.appTheme,
        key: UniqueKey(),
        // routes: {'/':const HomePage()},
        // home: LoginCaptin());
        home: SplashScreen());
  }
}
