import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/localization/localization.dart';
import 'package:test_sa/controllers/providers/api/departments_provider.dart';
import 'package:test_sa/controllers/providers/api/device_transfer_provider.dart';
import 'package:test_sa/controllers/providers/api/devices_provider.dart';
import 'package:test_sa/controllers/providers/api/gas_refill_provider.dart';
import 'package:test_sa/controllers/providers/api/hospitals_provider.dart';
import 'package:test_sa/controllers/providers/api/notifications_provider.dart';
import 'package:test_sa/controllers/providers/api/regular_visits_provider.dart';
import 'package:test_sa/controllers/providers/api/service_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/employee/assigned_to_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/employee/engineers_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/gas_refill/gas_cylinder_size_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/gas_refill/gas_cylinder_type_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/gas_refill/gas_status_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/gas_refill/gas_types_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/pentry/pentry_task_status_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/pentry/pentry_visit_status_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_defect_types_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_equipment_status_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_maintenance_situation_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_priority_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_users_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_visit_date_operator_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/service_reqest/service_request_first_action_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/service_reqest/service_request_loan_availability_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/service_reqest/service_request_status_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/service_reqest/service_request_through_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/service_reqest/service_request_type_provider.dart';
import 'package:test_sa/views/app_style/colors.dart';
import 'package:test_sa/views/pages/login.dart';
import 'package:test_sa/views/pages/register.dart';
import 'package:test_sa/views/pages/splash_screen.dart';
import 'package:test_sa/views/pages/sub_workorder/create_sub_workorder_page.dart';
import 'package:test_sa/views/pages/sub_workorder/search_sub_workorder_page.dart';
import 'package:test_sa/views/pages/user/gas_refill/request_gas_refill.dart';
import 'package:test_sa/views/pages/user/gas_refill/track_gas_refill.dart';
import 'package:test_sa/views/pages/user/land_page.dart';
import 'package:test_sa/views/pages/user/notifications/notifications_page.dart';
import 'package:test_sa/views/pages/user/profile_page.dart';
import 'package:test_sa/views/pages/user/report_issues_page.dart';
import 'package:test_sa/views/pages/user/requests/create_request.dart';
import 'package:test_sa/views/pages/user/requests/future_request_service_details.dart';
import 'package:test_sa/views/pages/user/requests/requests_page.dart';
import 'package:test_sa/views/pages/user/visits/preventive_maintenance_visits_page.dart';
import 'package:test_sa/views/pages/user/visits/regular_visits_page.dart';
import 'package:test_sa/views/widgets/departments/single_department_picker.dart';
import 'package:test_sa/views/widgets/equipment/single_device_picker.dart';

import 'controllers/providers/api/parts_provider.dart';
import 'controllers/providers/api/preventive_maintenance_visits_provider.dart';
import 'controllers/providers/api/status_drop_down/pentry/pentry_status_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_report_assistants_employee_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_report_fault_description_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_report_last_calls_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_report_reasons_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_report_repair_location_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_report_status_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_report_types_provider.dart';
import 'controllers/providers/api/status_drop_down/report/service_types_provider.dart';
import 'controllers/providers/api/user_provider.dart';
import 'controllers/providers/settings/setting_provider.dart';
import 'views/pages/device_transfer/request_device_transfer.dart';
import 'views/pages/device_transfer/track_device_transfer.dart';
import 'views/widgets/hospitals/single_hospital_picker.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (Platform.isIOS) {
    await Firebase.initializeApp(
      options:
          const FirebaseOptions(apiKey: "AIzaSyACQkSleNwU1jzEKR5ho1uSfZERokwwAbc", appId: "1:973582662416:ios:bc4a8061444c6a08fbc395", messagingSenderId: "973582662416", projectId: "atoms-fb912"),
    );
  } else {
    await Firebase.initializeApp();
  }
  runApp(ChangeNotifierProvider(
    create: (_) => SettingProvider(),
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    final _settingProvider = Provider.of<SettingProvider>(context);
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => UserProvider()),
        ChangeNotifierProvider(create: (_) => HospitalsProvider()),
        ChangeNotifierProvider(create: (_) => DevicesProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestsProvider()),
        ChangeNotifierProvider(create: (_) => DepartmentsProvider()),
        ChangeNotifierProvider(create: (_) => NotificationsProvider()),
        ChangeNotifierProvider(create: (_) => PreventiveMaintenanceVisitsProvider()),
        ChangeNotifierProvider(create: (_) => RegularVisitsProvider()),
        ChangeNotifierProvider(create: (_) => PartsProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportReasonsProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportStatusProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportEquipmentStatusProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportTypesProvider()),
        ChangeNotifierProvider(create: (_) => ServiceStatusProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportLastCallsProvider()),
        ChangeNotifierProvider(create: (_) => GasCylinderSizesProvider()),
        ChangeNotifierProvider(create: (_) => GasCylinderTypesProvider()),
        ChangeNotifierProvider(create: (_) => GasStatusProvider()),
        ChangeNotifierProvider(create: (_) => GasTypesProvider()),
        ChangeNotifierProvider(create: (_) => GasRefillProvider()),
        ChangeNotifierProvider(create: (_) => DeviceTransferProvider()),
        ChangeNotifierProvider(create: (_) => AssignedToProvider()),
        ChangeNotifierProvider(create: (_) => PentryTaskStatusProvider()),
        ChangeNotifierProvider(create: (_) => PentryVisitStatusProvider()),
        ChangeNotifierProvider(create: (_) => PentryStatusProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestPriorityProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestDefectTypesProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestTypeProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestedThroughProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestStatusProvider()),
        ChangeNotifierProvider(create: (_) => EngineersProvider()),
        ChangeNotifierProvider(create: (_) => ServiceLoanAvailabilityProvider()),
        ChangeNotifierProvider(create: (_) => ServiceFirstActionProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportRepairLocationProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestFaultDescriptionProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportVisitOperatorProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportMaintenanceSituationProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportUsersProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportAssistantsEmployeeProvider()),
      ],
      child: GestureDetector(
        onTap: () {
          FocusScopeNode currentFocus = FocusScope.of(context);
          if (!currentFocus.hasPrimaryFocus) {
            FocusManager.instance.primaryFocus.unfocus();
          }
        },
        child: MaterialApp(
          title: 'ATOMS',
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
              fontFamily: "Poppins",
              //canvasColor: AColors.primaryColor,
              scaffoldBackgroundColor: AColors.scaffoldBackgroundColor,
              primaryColor: AColors.primaryColor,
              indicatorColor: AColors.primaryColor,
              colorScheme: const ColorScheme.light(primary: AColors.primaryColor, onPrimary: Colors.white, secondary: AColors.secondaryColor, onSecondary: Colors.white)),
          localizationsDelegates: const [
            // ... app-specific localization delegate[s] here
            AppLocalization.delegate,
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [
            Locale('en'),
            Locale('ar'),
          ],
          locale: Locale(_settingProvider.language ?? 'en'),
          initialRoute: SplashScreen.id,
          routes: {
            SplashScreen.id: (_) => const SplashScreen(),
            LandPage.id: (_) => const LandPage(),
            Login.id: (_) => Login(),
            Register.id: (_) => Register(),
            ProfilePage.id: (_) => ProfilePage(),
            ReportIssuesPage.id: (_) => const ReportIssuesPage(),
            RequestGasRefill.id: (_) => const RequestGasRefill(),
            CreateRequestPage.id: (_) => const CreateRequestPage(),
            SingleHospitalPicker.id: (_) => SingleHospitalPicker(),
            SingleDevicePicker.id: (_) => SingleDevicePicker(),
            SingleDepartmentPicker.id: (_) => SingleDepartmentPicker(),
            ServiceRequestsPage.id: (_) => ServiceRequestsPage(),
            NotificationsPage.id: (_) => NotificationsPage(),
            FutureRequestServiceDetails.id: (_) => FutureRequestServiceDetails(),
            PreventiveMaintenanceVisitsPage.id: (_) => PreventiveMaintenanceVisitsPage(),
            RegularVisitsPage.id: (_) => RegularVisitsPage(),
            TrackGasRefillPage.id: (_) => const TrackGasRefillPage(),
            RequestDeviceTransfer.id: (_) => const RequestDeviceTransfer(),
            TrackDeviceTransferPage.id: (_) => const TrackDeviceTransferPage(),
            SearchSubWorkOrderPage.id: (_) => const SearchSubWorkOrderPage(),
            CreateSubWorkOrderPage.id: (_) => const CreateSubWorkOrderPage(),
          },
        ),
      ),
    );
  }
}
