import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.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:sefer_admin1/views/auth/login_page.dart';

import 'controller/firebase/firbase_messge.dart';
import 'controller/functions/encrypt_decrypt.dart';
import 'firebase_options.dart';
import 'models/db_sql.dart';
import 'views/admin/admin_home_page.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 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,
      home: AdminLoginPage(),
    );
  }
}
