import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../constant/box_name.dart';
import '../../constant/colors.dart';
import '../../controller/functions/crud.dart';
import '../../controller/home/map/map_socket_controller.dart';
import '../../controller/home/map/map_engine_controller.dart';
import '../../controller/home/map/location_search_controller.dart';
import '../../controller/home/map/nearby_drivers_controller.dart';
import '../../controller/home/map/ride_lifecycle_controller.dart';
import '../../controller/home/map/ui_interactions_controller.dart';
import '../../controller/home/map/ride_state.dart';
import '../../main.dart';
import '../../views/home/map_widget.dart/ride_begin_passenger.dart';

import '../../controller/home/menu_controller.dart';
import 'map_widget.dart/apply_order_widget.dart';
import 'map_widget.dart/buttom_sheet_map_show.dart';
import 'map_widget.dart/car_details_widget_to_go.dart';
import 'map_widget.dart/cash_confirm_bottom_page.dart';
import 'map_widget.dart/google_map_passenger_widget.dart';
import 'map_widget.dart/left_main_menu_icons.dart';
import 'map_widget.dart/main_bottom_menu_map.dart';
import 'map_widget.dart/map_menu_widget.dart';
import '../../controller/functions/package_info.dart';
import 'map_widget.dart/passengerRideLoctionWidget.dart';
import 'map_widget.dart/payment_method.page.dart';
import 'map_widget.dart/points_page_for_rider.dart';
import 'map_widget.dart/ride_from_start_app.dart';
import 'map_widget.dart/searching_captain_window.dart';
import 'map_widget.dart/vip_begin.dart';

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

  @override
  Widget build(BuildContext context) {
    Get.find<MapSocketController>();
    Get.find<MapEngineController>();
    Get.find<LocationSearchController>();
    Get.find<NearbyDriversController>();
    Get.find<RideLifecycleController>();
    Get.find<UiInteractionsController>();
    Get.find<MyMenuController>();
    Get.find<CRUD>();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      checkForUpdate(context);
    });

    return Scaffold(
      body: SafeArea(
        bottom: true,
        child: Stack(
          children: [
            GoogleMapPassengerWidget(),
            // OsmMapPassengerWidget(),
            leftMainMenuIcons(),
            // PaymobPackage(),
            const PickerIconOnMap(),
            // PickerAnimtionContainerFormPlaces(),
            const MainBottomMenuMap(),
            // NewMainBottomSheet(),

            buttomSheetMapPage(),
            CarDetailsTypeToChoose(),
            // const HeaderDestination(),
            const BurcMoney(),
            // const PromoCode(),
            ApplyOrderWidget(),
            const MapMenuWidget(),
            // hexagonClipper(),
            const CancelRidePageShow(),
            CashConfirmPageShown(),
            const PaymentMethodPage(),
            const SearchingCaptainWindow(),
            AttributionMap(),
            // timerForCancelTripFromPassenger(),
            // const DriverTimeArrivePassengerPage(),
            // const TimerToPassengerFromDriver(),
            const PassengerRideLocationWidget(),
            const RideBeginPassenger(),
            const VipRideBeginPassenger(),
            const RideFromStartApp(),

            // cancelRidePage(),
            // const MenuIconMapPageWidget(),
            PointsPageForRider()
          ],
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Positioned(
      left: 4,
      bottom: 20,
      child: RotatedBox(
        quarterTurns: 0, // يخلي النص عمودي (من تحت لفوق)
        child: Opacity(
          opacity: 0.7,
          child: Text(
            "Siro Maps",
            // "Siro Maps  •  © OpenStreetMap contributors",
            style: TextStyle(
              fontSize: 10,
              color: Colors.grey[700],
              fontWeight: FontWeight.w400,
            ),
          ),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RideLifecycleController>(
      builder: (controller) {
        // نستخدم RideState Enum لأنه أدق، أو نصلح المنطق النصي
        // الشرط:
        // 1. يوجد خط مسار
        // 2. الحالة ليست "بدأت"
        // 3. الحالة ليست "انتهت"
        // 4. الحالة ليست "قيد التنفيذ" (لزيادة التأكيد)

        // bool showCancelButton = controller.polyLines.isNotEmpty &&
        //     controller.statusRide != 'Begin' && // استخدمنا &&
        //     controller.statusRide != 'inProgress' &&
        //     controller.statusRide != 'Finished';

        // يمكنك أيضاً استخدام RideState ليكون أدق:
        bool showCancelButton = controller.polyLines.isNotEmpty &&
            controller.currentRideState.value != RideState.inProgress &&
            controller.currentRideState.value != RideState.finished;

        return showCancelButton
            ? Positioned(
                right: box.read(BoxName.lang) != 'ar' ? 10 : null,
                left: box.read(BoxName.lang) == 'ar' ? 10 : null,
                top: Get.height * .013,
                child: GestureDetector(
                  onTap: () {
                    // استدعاء دالة الإلغاء
                    controller.changeCancelRidePageShow();
                    // ملاحظة: تأكد أن الدالة تظهر ديالوج للتأكيد أولاً ولا تلغي فوراً
                  },
                  child: Container(
                    decoration: BoxDecoration(
                        color: AppColor.redColor,
                        borderRadius: BorderRadius.circular(15)),
                    child: Padding(
                      padding: const EdgeInsets.all(3),
                      child: Icon(
                        Icons.clear,
                        size: 40,
                        color: AppColor.secondaryColor,
                      ),
                    ),
                  ),
                ))
            : const SizedBox();
      },
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return GetBuilder<RideLifecycleController>(
        builder: (controller) => controller.isPickerShown
            ? Positioned(
                bottom: Get.height * .2,
                top: 0,
                left: 0,
                right: 0,
                child: controller.isPickerShown
                    ? const Icon(
                        Icons.add_location,
                        color: Colors.purple,
                      )
                    : const SizedBox(),
              )
            : const SizedBox());
  }
}
