import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'constant/box_name.dart';
import 'controller/firebase/firbase_messge.dart';
import 'controller/functions/encrypt_decrypt.dart';
import 'firebase_options.dart';
import 'models/db_sql.dart';
import 'package:google_fonts/google_fonts.dart';
import 'constant/colors.dart';
import 'routes.dart';
import 'binding/initial_binding.dart';

final box = GetStorage();
const storage = FlutterSecureStorage();
@pragma('vm:entry-point')
Future<void> backgroundMessageHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
  if (message.data.isNotEmpty && message.notification != null) {
    FirebaseMessagesController().fireBaseTitles(message);
  }
} //

DbSql sql = DbSql.instance;

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await GetStorage.init();
  await initializeDateFormatting('ar', null);
  await EncryptionHelper.initialize();
  if (Platform.isAndroid || Platform.isIOS) {
    await Firebase.initializeApp(
      options: DefaultFirebaseOptions.currentPlatform,
    );
    // await FirebaseMessagesController().requestFirebaseMessagingPermission();

    FirebaseMessaging.onBackgroundMessage(backgroundMessageHandler);

    List<Future> initializationTasks = [
      FirebaseMessagesController().getNotificationSettings(),
      FirebaseMessagesController().getToken(),
    ];

    await Future.wait(initializationTasks);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  } // Enable Crashlytics collection
  // FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;

  runApp(const MainApp());
}

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

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Siro Admin',
      locale: const Locale('ar'),
      fallbackLocale: const Locale('en'),
      themeMode: ThemeMode.dark,
      darkTheme: ThemeData(
        brightness: Brightness.dark,
        scaffoldBackgroundColor: AppColor.bg,
        primaryColor: AppColor.accent,
        colorScheme: const ColorScheme.dark(
          primary: AppColor.accent,
          secondary: AppColor.accent,
          surface: AppColor.surface,
          background: AppColor.bg,
          error: AppColor.danger,
        ),
        dividerColor: AppColor.divider,
        textTheme:
            GoogleFonts.cairoTextTheme(ThemeData.dark().textTheme).copyWith(
          bodyLarge: GoogleFonts.inter(color: AppColor.textPrimary),
          bodyMedium: GoogleFonts.inter(color: AppColor.textPrimary),
        ),
        appBarTheme: const AppBarTheme(
          backgroundColor: AppColor.bg,
          elevation: 0,
          centerTitle: true,
          titleTextStyle: TextStyle(
            color: AppColor.textPrimary,
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
      initialBinding: InitialBinding(),
      initialRoute: box.read(BoxName.phoneVerified) == true ? "/" : "/login",
      getPages: routes,
    );
  }
}
