import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:hmg_patient_app_new/core/app_state.dart';
import 'package:hmg_patient_app_new/core/dependencies.dart';
import 'package:hmg_patient_app_new/core/location_util.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/route_extensions.dart';
import 'package:hmg_patient_app_new/features/book_appointments/book_appointments_view_model.dart';
import 'package:hmg_patient_app_new/features/my_appointments/my_appointments_view_model.dart';
import 'package:hmg_patient_app_new/generated/locale_keys.g.dart';
import 'package:hmg_patient_app_new/presentation/hmg_services/services_page.dart';
import 'package:hmg_patient_app_new/presentation/home/landing_page.dart';
import 'package:hmg_patient_app_new/presentation/medical_file/medical_file_page.dart';
import 'package:hmg_patient_app_new/presentation/weather/weather_details_page.dart';
import 'package:hmg_patient_app_new/routes/app_routes.dart';
import 'package:hmg_patient_app_new/services/navigation_service.dart';
import 'package:hmg_patient_app_new/widgets/bottom_navigation/bottom_navigation.dart';
import 'package:hmg_patient_app_new/widgets/common_bottom_sheet.dart';
import 'package:permission_handler/permission_handler.dart' show openAppSettings;


class LandingNavigation extends StatefulWidget {
  const LandingNavigation({super.key});

  @override
  State<LandingNavigation> createState() => _LandingNavigationState();
}

class _LandingNavigationState extends State<LandingNavigation> {
  int _currentIndex = 0;
  final PageController _pageController = PageController();

  @override
  Widget build(BuildContext context) {
    AppState appState = getIt.get<AppState>();
    return Scaffold(
      body: PageView(
        controller: _pageController,
        physics: const NeverScrollableScrollPhysics(),
        children: [
          const LandingPage(),
          appState.isAuthenticated ? MedicalFilePage(showBackIcon: false) : const WeatherDetailsPage(showBackIcon: false,),
          SizedBox(),
          // const ToDoPage(),
          // appState.isAuthenticated ? UserInfoSelectionScreen() : /* need add news page */ SizedBox(),
          SizedBox(),
          ServicesPage(showBackIcon: false),
        ],
      ),
      bottomNavigationBar: BottomNavigation(
        currentIndex: _currentIndex,
        onTap: (index) async {
          setState(() => _currentIndex = index);

          if (_currentIndex == 1 && !appState.isAuthenticated) {
            final locationUtils = getIt.get<LocationUtils>();
            final isLocationEnabled = await locationUtils.isLocationPermissionEnabled();
            if (isLocationEnabled) {
              _pageController.animateToPage(index, duration: const Duration(milliseconds: 300), curve: Curves.easeInOut);
            } else {
              locationUtils.getLocation(
                isShowConfirmDialog: true,
                onSuccess: (position) {
                  _pageController.animateToPage(index, duration: const Duration(milliseconds: 300), curve: Curves.easeInOut);
                },
                onFailure: () {
                     showCommonBottomSheetWithoutHeight(
                    title: LocaleKeys.notice.tr(),
                    context,
                    child: Utils.getWarningWidget(
                      loadingText: LocaleKeys.grantLocationPermission.tr(),
                      isShowActionButtons: true,
                      onCancelTap: () {
                        getIt.get<NavigationService>().pop();
                        setState(() => _currentIndex = 0);
                      },
                      onConfirmTap: () async {
                        getIt.get<NavigationService>().pop();
                        openAppSettings();
                        setState(() => _currentIndex = 0);
                      },
                    ),
                    callBackFunc: () {},
                    isFullScreen: false,
                    isCloseButtonVisible: true,
                  );
                },
              );
            }
            return;
          }

          if (_currentIndex == 2) {
            getIt.get<BookAppointmentsViewModel>().onTabChanged(0);
            getIt.get<MyAppointmentsViewModel>().getPatientFavouriteDoctors();
            context.navigateWithName(AppRoutes.bookAppointmentPage);
            return;
          }
          if (_currentIndex == 3) {
            if (appState.isAuthenticated) {
              context.navigateWithName(AppRoutes.userInfoSelection);
            } else {
              Utils.openWebView(
                url: 'https://x.com/HMG',
              );
            }
            return;
          }
          _pageController.animateToPage(index, duration: const Duration(milliseconds: 300), curve: Curves.easeInOut);
        },
      ),
    );
  }
}
