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/nurse_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_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_repair_location_provider.dart';
import 'package:test_sa/controllers/providers/api/status_drop_down/report/service_types_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_asset_availability_provider.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_electrical_safety_provider.dart';
import 'package:test_sa/providers/ppm_service_provider.dart';
import 'package:test_sa/providers/ppm_task_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/last_situation_provider.dart';
import 'package:test_sa/providers/service_request_providers/priority_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/activity_status_provider.dart';
import 'package:test_sa/providers/work_order/fault_description_provider.dart';
import 'package:test_sa/providers/work_order/reason_provider.dart';
import 'package:test_sa/providers/work_order/retirement_type_provider.dart';
import 'package:test_sa/providers/work_order/service_type_provider.dart';
import 'package:test_sa/providers/work_order/supplier_engineer_provider.dart';
import 'package:test_sa/providers/work_order/vendor_provider.dart';
import 'package:test_sa/service_request_latest/request_detail_provider.dart';
import 'package:test_sa/service_request_latest/views/nurse/create_new_request_view.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/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/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/requests/create_service_request_page.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 'controllers/providers/api/gas_refill_comments.dart';
import 'controllers/providers/api/user_provider.dart';
import 'controllers/providers/settings/setting_provider.dart';
import 'dashboard_latest/dashboard_provider.dart';
import 'new_views/pages/new_gas_refill_request_page.dart';
import 'providers/service_request_providers/loan_availability_provider.dart';
import 'providers/service_request_providers/reject_reason_provider.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()),
        //new providers according to new Api's..
        ChangeNotifierProvider(create: (_) => DashBoardProvider()),
        ChangeNotifierProvider(create: (_) => RequestDetailProvider()),
        // ChangeNotifierProvider(create: (_) => PreventiveMaintenanceVisitsProvider()),
        ChangeNotifierProvider(create: (_) => PpmProvider()),
        ChangeNotifierProvider(create: (_) => PartsProvider()),

        ///todo deleted
        //ChangeNotifierProvider(create: (_) => ServiceReportReasonsProvider()),
        //ChangeNotifierProvider(create: (_) => ServiceReportStatusProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => ServiceReportEquipmentStatusProvider()),
        //ChangeNotifierProvider(create: (_) => ServiceReportTypesProvider()),
        ChangeNotifierProvider(create: (_) => ServiceStatusProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportLastCallsProvider()),

        ///todo deleted
        //ChangeNotifierProvider(create: (_) => GasCylinderSizesProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => GasCylinderTypesProvider()),
        ChangeNotifierProvider(create: (_) => GasStatusProvider()),
        ChangeNotifierProvider(create: (_) => GasTypesProvider()),
        ChangeNotifierProvider(create: (_) => GasRefillProvider()),
        ChangeNotifierProvider(create: (_) => AssetTransferProvider()),
        ChangeNotifierProvider(create: (_) => AssetTransferStatusProvider()),

        ///todo deleted
        //ChangeNotifierProvider(create: (_) => AssignedToProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => PentryTaskStatusProvider()),
        //ChangeNotifierProvider(create: (_) => PentryVisitStatusProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => PentryStatusProvider()),
        //ChangeNotifierProvider(create: (_) => ServiceRequestPriorityProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => ServiceRequestDefectTypesProvider()),
        //ChangeNotifierProvider(create: (_) => ServiceRequestTypeProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => ServiceRequestedThroughProvider()),
        //ChangeNotifierProvider(create: (_) => ServiceRequestStatusProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => EngineersProvider()),
        ChangeNotifierProvider(create: (_) => SupplierEngineerProvider()),

        /// Loan availability not required
        // ChangeNotifierProvider(create: (_) => ServiceLoanAvailabilityProvider()),
        ///todo deleted
        //ChangeNotifierProvider(create: (_) => ServiceFirstActionProvider()),
        ChangeNotifierProvider(create: (_) => ServiceReportRepairLocationProvider()),
        ChangeNotifierProvider(create: (_) => ServiceRequestFaultDescriptionProvider()),

        ///todo deleted
        //ChangeNotifierProvider(create: (_) => ServiceReportVisitOperatorProvider()),
        ///todo deleted
        //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: (_) => RejectReasonProvider()),
        ChangeNotifierProvider(create: (_) => LastSituationProvider()),
        ChangeNotifierProvider(create: (_) => FaultDescriptionProvider()),
        ChangeNotifierProvider(create: (_) => RetirementTypeProvider()),
        ChangeNotifierProvider(create: (_) => ActivityStatusProvider()),

        ///todo deleted
        //ChangeNotifierProvider(create: (_) => AssetTypesProvider()),
        ChangeNotifierProvider(create: (_) => ServiceTypeProvider()),
        ChangeNotifierProvider(create: (_) => PPMVisitStatusProvider()),

        ///todo deleted
        //ChangeNotifierProvider(create: (_) => PentryTaskStatusProvider()),
        ChangeNotifierProvider(create: (_) => PPMDeviceStatusProvider()),
        ChangeNotifierProvider(create: (_) => PpmAssetAvailabilityProvider()),
        ChangeNotifierProvider(create: (_) => PpmTaskStatusProvider()),
        ChangeNotifierProvider(create: (_) => PpmElectricalSafetyProvider()),
        ChangeNotifierProvider(create: (_) => PpmServiceProvider()),
        ChangeNotifierProvider(create: (_) => CommentsProvider()),
        ChangeNotifierProvider(create: (_) => GasRefillCommentsProvider()),

        ///todo deleted
        //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(),

            ///todo deleted
            //old.LandPage.id: (_) => const old.LandPage(),
            LandPage.routeName: (_) => const LandPage(),
            NewGasRefillRequestPage.routeName: (_) => const NewGasRefillRequestPage(),
            ServiceRequestsPage.id: (_) => const ServiceRequestsPage(),
            //ReportIssuesPage.id: (_) =>  ReportIssuesPage(),
            RequestGasRefill.id: (_) => const RequestGasRefill(),
            CreateServiceRequestPage.id: (_) => const CreateServiceRequestPage(),
            CreateNewRequest.id: (_) => const CreateNewRequest(),
            // SingleHospitalPicker.id: (_) => SingleHospitalPicker(),
            MyAssetsPage.id: (_) => const MyAssetsPage(),
            SingleDepartmentPicker.id: (_) => const SingleDepartmentPicker(),
            NotificationsPage.id: (_) => const NotificationsPage(),
            // FutureRequestServiceDetails.id: (_) => FutureRequestServiceDetails(),
            // PreventiveMaintenanceVisitsPage.id: (_) => PreventiveMaintenanceVisitsPage(),
            PpmPage.id: (_) => const PpmPage(),
            TrackGasRefillPage.id: (_) => const TrackGasRefillPage(),
            RequestDeviceTransfer.id: (_) => const RequestDeviceTransfer(),
            TrackDeviceTransferPage.id: (_) => const TrackDeviceTransferPage(),

            //  todo remove this class after work
            // SearchSubWorkOrderPage.id: (_) => const SearchSubWorkOrderPage(),
            CreateSubWorkOrderPage.id: (_) => const CreateSubWorkOrderPage(),
            WorkOrderListPage.id: (_) => WorkOrderListPage(),
            AssetDetailPage.id: (_) => const 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(),
          },
        ),
      ),
    );
  }
}
