import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hmg_qline/view_models/screen_config_view_model.dart';
import 'package:provider/provider.dart';
import 'package:hmg_qline/config/dependency_injection.dart';
import 'package:hmg_qline/config/routes.dart';
import 'package:hmg_qline/constants/app_constants.dart';
import 'package:hmg_qline/view_models/queuing_view_model.dart';
import 'package:hmg_qline/views/view_helpers/size_config.dart';
import 'package:wakelock_plus/wakelock_plus.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await AppDependencies.addDependencies();
  await WakelockPlus.enable();

  // The following foreground task logic is now handled natively in Android. Commented out for clarity.
  /*
  void _initializeForegroundTask() {
    FlutterForegroundTask.init(
      androidNotificationOptions: AndroidNotificationOptions(
        channelId: 'foreground_service',
        channelName: AppStrings.appName,
        channelDescription: '',
        onlyAlertOnce: true,
      ),
      iosNotificationOptions: const IOSNotificationOptions(
        showNotification: false,
        playSound: false,
      ),
      foregroundTaskOptions: ForegroundTaskOptions(
        eventAction: ForegroundTaskEventAction.repeat(5000),
        autoRunOnBoot: true,
        autoRunOnMyPackageReplaced: true,
        allowWakeLock: true,
        allowWifiLock: true,
      ),
    );
  }

  Future<void> _startForegroundService() async {
    await FlutterForegroundTask.startService(
      notificationTitle: AppStrings.appName,
      notificationText: 'App is running in foreground',
      callback: startCallback,
    );
  }

  // Initialize foreground task first
  _initializeForegroundTask();

  // Register lifecycle callback (e.g., reopen app on detach)
  LifecycleHandler(
    onDetached: () {
      getIt<NativeMethodChannelService>().reopenApp();
    },
  ).register();

  // Start foreground service AFTER initialization
  await _startForegroundService();
  */

  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        return OrientationBuilder(builder: (context, orientation) {
          SizeConfig().init(constraints, orientation);
          SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
          return MultiProvider(
            providers: [
              ChangeNotifierProvider<ScreenConfigViewModel>(create: (context) => getIt.get<ScreenConfigViewModel>()),
              ChangeNotifierProvider<QueuingViewModel>(create: (context) => getIt.get<QueuingViewModel>()),
            ],
            child: MaterialApp(
              showSemanticsDebugger: false,
              title: AppStrings.appName,
              theme: ThemeData(
                fontFamily: AppStrings.fontNamePoppins,
                colorScheme: ColorScheme.fromSwatch(primarySwatch: Colors.grey).copyWith(
                  surface: const Color.fromRGBO(255, 255, 255, 1),
                ),
              ),
              initialRoute: AppRoutes.initialRoute,
              routes: AppRoutes.routes,
              debugShowCheckedModeBanner: false,
            ),
          );
        });
      },
    );
  }
}
