import 'dart:io';

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:localization/localization.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/all_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/asset_transfer_provider.dart';
import 'package:test_sa/controllers/providers/api/comments_provider.dart';
import 'package:test_sa/controllers/providers/api/departments_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/parts_provider.dart';
import 'package:test_sa/controllers/providers/api/ppm_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/employee/nurse_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/pentry/pentry_status_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_assistants_employee_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_fault_description_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_last_calls_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_reasons_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_repair_location_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_status_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_report_types_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/report/service_types_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_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/new_views/app_style/app_themes.dart';
import 'package:test_sa/new_views/pages/help_center_page.dart';
import 'package:test_sa/new_views/pages/land_page/land_page.dart';
import 'package:test_sa/new_views/pages/login_page.dart';
import 'package:test_sa/new_views/pages/report_bug_page.dart';
import 'package:test_sa/new_views/pages/settings_page.dart';
import 'package:test_sa/new_views/pages/splash_page.dart';
import 'package:test_sa/providers/asset_transfer/asset_transfer_status_provider.dart';
import 'package:test_sa/providers/department_provider.dart';
import 'package:test_sa/providers/gas_request_providers/cylinder_size_provider.dart';
import 'package:test_sa/providers/gas_request_providers/cylinder_type_provider.dart';
import 'package:test_sa/providers/gas_request_providers/gas_status_provider.dart';
import 'package:test_sa/providers/gas_request_providers/gas_types_provider.dart';
import 'package:test_sa/providers/gas_request_providers/site_provider.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';
import 'package:test_sa/providers/ppm_checklist_status_provider.dart';
import 'package:test_sa/providers/ppm_device_status_provider.dart';
import 'package:test_sa/providers/ppm_visit_status_provider.dart';
import 'package:test_sa/providers/service_request_providers/commissioning_status_provider.dart';
import 'package:test_sa/providers/service_request_providers/equipment_status_provider.dart';
import 'package:test_sa/providers/service_request_providers/first_action_provider.dart';
import 'package:test_sa/providers/service_request_providers/priority_provider.dart';
import 'package:test_sa/providers/service_request_providers/request_status_provider.dart';
import 'package:test_sa/providers/service_request_providers/requested_through_provider.dart';
import 'package:test_sa/providers/service_request_providers/type_of_request_provider.dart';
import 'package:test_sa/providers/work_order/asset_types_provider.dart';
import 'package:test_sa/providers/work_order/reason_provider.dart';
import 'package:test_sa/providers/work_order/service_type_provider.dart';
import 'package:test_sa/providers/work_order/vendor_provider.dart';
import 'package:test_sa/views/pages/device_transfer/asset_filter_screen.dart';
import 'package:test_sa/views/pages/device_transfer/asset_search_screen.dart';
import 'package:test_sa/views/pages/device_transfer/request_device_transfer.dart';
import 'package:test_sa/views/pages/device_transfer/track_device_transfer.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' as old;
import 'package:test_sa/views/pages/user/notifications/notifications_page.dart';
import 'package:test_sa/views/pages/user/ppm/ppm_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_service_request_page.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/requests/update_service_request_page.dart';
import 'package:test_sa/views/pages/user/requests/work_order/work_order_details_page.dart';
import 'package:test_sa/views/pages/user/requests/work_order/work_orders_list_page.dart';
import 'package:test_sa/views/widgets/departments/single_department_picker.dart';
import 'package:test_sa/views/widgets/equipment/asset_detail_page.dart';
import 'package:test_sa/views/widgets/equipment/single_device_picker.dart';
import 'package:test_sa/views/widgets/hospitals/single_hospital_picker.dart';

import 'controllers/providers/api/gas_refill_comments.dart';
import 'controllers/providers/api/user_provider.dart';
import 'controllers/providers/settings/setting_provider.dart';
import 'new_views/pages/new_gas_refill_request_page.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();
  }
  SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(statusBarColor: Colors.transparent));

  /// only portrait mode
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  runApp(ChangeNotifierProvider(create: (_) => SettingProvider(), child: const MyApp()));
}

final navigatorKey = GlobalKey<NavigatorState>();

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    LocalJsonLocalization.delegate.directories = ['assets/translations'];
    final settingProvider = Provider.of<SettingProvider>(context);
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => UserProvider()),
        ChangeNotifierProvider(create: (_) => GasTypesProvider()),
        ChangeNotifierProvider(create: (_) => NullableLoadingProvider()),
        ChangeNotifierProvider(create: (_) => CylinderTypesProvider()),
        ChangeNotifierProvider(create: (_) => CylinderSizeProvider()),
        ChangeNotifierProvider(create: (_) => DepartmentProvider()),
        ChangeNotifierProvider(create: (_) => SiteProvider()),
        ChangeNotifierProvider(create: (_) => NurseProvider()),
        ChangeNotifierProvider(create: (_) => HospitalsProvider()),
        ChangeNotifierProvider(create: (_) => AssetProvider()),
        ChangeNotifierProvider(create: (_) => AllRequestsProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestsProvider()),
        ChangeNotifierProvider(create: (_) => DepartmentsProvider()),
        ChangeNotifierProvider(create: (_) => NotificationsProvider()),
        // ChangeNotifierProvider(create: (_) => PreventiveMaintenanceVisitsProvider()),
        ChangeNotifierProvider(create: (_) => PpmProvider()),
        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: (_) => AssetTransferProvider()),
        ChangeNotifierProvider(create: (_) => AssetTransferStatusProvider()),
        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()),

        /// Loan availability not required
        // ChangeNotifierProvider(create: (_) => ServiceLoanAvailabilityProvider()),
        ChangeNotifierProvider(create: (_) => ServiceFirstActionProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportRepairLocationProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestFaultDescriptionProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportVisitOperatorProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportMaintenanceSituationProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportUsersProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportAssistantsEmployeeProvider()),
        ChangeNotifierProvider(create: (_) => PriorityProvider()),
        ChangeNotifierProvider(create: (_) => EquipmentStatusProvider()),
        ChangeNotifierProvider(create: (_) => CommissioningStatusProvider()),
        ChangeNotifierProvider(create: (_) => RequestedThroughProvider()),
        ChangeNotifierProvider(create: (_) => TypeOfRequestProvider()),
        ChangeNotifierProvider(create: (_) => FirstActionStatusProvider()),

        /// Loan availability not required
        // ChangeNotifierProvider(create: (_) => LoanAvailabilityProvider()),
        ChangeNotifierProvider(create: (_) => ReasonProvider()),
        ChangeNotifierProvider(create: (_) => AssetTypesProvider()),
        ChangeNotifierProvider(create: (_) => ServiceTypeProvider()),
        ChangeNotifierProvider(create: (_) => PPMVisitStatusProvider()),
        ChangeNotifierProvider(create: (_) => PentryTaskStatusProvider()),
        ChangeNotifierProvider(create: (_) => PPMDeviceStatusProvider()),
        ChangeNotifierProvider(create: (_) => CommentsProvider()),
        ChangeNotifierProvider(create: (_) => GasRefillCommentsProvider()),
        ChangeNotifierProvider(create: (_) => RequestStatusProvider()),
        ChangeNotifierProvider(create: (_) => VendorProvider()),
        ChangeNotifierProvider(create: (_) => PpmChecklistStatusProvider()),
      ],
      child: GestureDetector(
        onTap: () {
          FocusScopeNode currentFocus = FocusScope.of(context);
          if (!currentFocus.hasPrimaryFocus) {
            FocusManager.instance.primaryFocus.unfocus();
          }
        },
        child: MaterialApp(
          title: 'ATOMS',
          debugShowCheckedModeBanner: false,
          theme: settingProvider.theme == "dark" ? AppThemes.darkTheme : AppThemes.lightTheme,
          localizationsDelegates: AppLocalizations.localizationsDelegates,
          supportedLocales: AppLocalizations.supportedLocales,
          locale: Locale(settingProvider.language ?? 'en'),
          initialRoute: SplashPage.routeName,
          navigatorKey: navigatorKey,
          routes: {
            SplashPage.routeName: (_) => const SplashPage(),
            LoginPage.routeName: (_) => const LoginPage(),
            old.LandPage.id: (_) => const old.LandPage(),
            LandPage.routeName: (_) => const LandPage(),
            NewGasRefillRequestPage.routeName: (_) => const NewGasRefillRequestPage(),
            ServiceRequestsPage.id: (_) => ServiceRequestsPage(),
            ReportIssuesPage.id: (_) => const ReportIssuesPage(),
            RequestGasRefill.id: (_) => const RequestGasRefill(),
            CreateServiceRequestPage.id: (_) => const CreateServiceRequestPage(),
            SingleHospitalPicker.id: (_) => SingleHospitalPicker(),
            MyAssetsPage.id: (_) => const MyAssetsPage(),
            SingleDepartmentPicker.id: (_) => SingleDepartmentPicker(),
            NotificationsPage.id: (_) => NotificationsPage(),
            FutureRequestServiceDetails.id: (_) => FutureRequestServiceDetails(),
            // PreventiveMaintenanceVisitsPage.id: (_) => PreventiveMaintenanceVisitsPage(),
            PpmPage.id: (_) => const PpmPage(),
            TrackGasRefillPage.id: (_) => const TrackGasRefillPage(),
            RequestDeviceTransfer.id: (_) => const RequestDeviceTransfer(),
            TrackDeviceTransferPage.id: (_) => const TrackDeviceTransferPage(),
            SearchSubWorkOrderPage.id: (_) => const SearchSubWorkOrderPage(),
            CreateSubWorkOrderPage.id: (_) => const CreateSubWorkOrderPage(),
            WorkOrderListPage.id: (_) => WorkOrderListPage(),
            AssetDetailPage.id: (_) => AssetDetailPage(),
            AssetSearchScreen.id: (_) => const AssetSearchScreen(),
            AssetFilterScreen.id: (_) => const AssetFilterScreen(),
            WorkOrderDetailsPage.id: (_) => const WorkOrderDetailsPage(),
            UpdateServiceRequestPage.id: (_) => const UpdateServiceRequestPage(),
            SettingsPage.id: (_) => const SettingsPage(),
            ProfilePage.id: (_) => const ProfilePage(),
            ReportBugPage.id: (_) => const ReportBugPage(),
            HelpCenterPage.id: (_) => const HelpCenterPage(),
          },
        ),
      ),
    );
  }
}
