import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intaleq_maps/intaleq_maps.dart';

import '../../../../constant/api_key.dart';
import '../../../../controller/functions/location_controller.dart';
import '../../../../controller/home/captin/home_captain_controller.dart';
import '../../../../controller/functions/country_logic.dart';

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

  @override
  Widget build(BuildContext context) {
    final LocationController locationController =
        Get.find<LocationController>();
    final HomeCaptainController homeCaptainController =
        Get.find<HomeCaptainController>();

    return Obx(() {
      final LatLng currentLocation = LatLng(
          locationController.myLocation.latitude,
          locationController.myLocation.longitude);
      final double currentHeading = locationController.heading;

      final cityPolygon = CountryLogic.getCurrentCityPolygon();
      final bool inServiceArea = CountryLogic.isPointInPolygon(currentLocation, cityPolygon);

      return Stack(
        children: [
          IntaleqMap(
            apiKey: AK.mapSaasKey,
            initialCameraPosition: CameraPosition(
              target: currentLocation,
              zoom: 15,
              bearing: currentHeading,
            ),
            markers: {
              Marker(
                markerId: const MarkerId('myLocation'),
                position: currentLocation,
                rotation: currentHeading,
                icon: homeCaptainController.carIcon,
                anchor: const Offset(0.5, 0.5),
                flat: true,
                zIndex: 2,
              )
            },
            polygons: {
              Polygon(
                polygonId: const PolygonId('city_polygon'),
                points: cityPolygon,
                fillColor: Colors.green.withOpacity(0.15),
                strokeColor: Colors.green[800]!,
                strokeWidth: 2,
              )
            },
            onMapCreated: (IntaleqMapController controller) {
              // You can assign this controller if needed
            },
          ),
          if (!inServiceArea)
            Positioned(
              bottom: 20,
              left: 20,
              right: 20,
              child: Container(
                padding: const EdgeInsets.symmetric(vertical: 12, horizontal: 16),
                decoration: BoxDecoration(
                  color: Colors.redAccent.withOpacity(0.9),
                  borderRadius: BorderRadius.circular(12),
                  boxShadow: const [
                    BoxShadow(color: Colors.black26, blurRadius: 4, offset: Offset(0, 2))
                  ],
                ),
                child: const Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Icon(Icons.warning_amber_rounded, color: Colors.white),
                    SizedBox(width: 8),
                    Expanded(
                      child: Text(
                        'الخدمة غير متوفرة في منطقتك حالياً',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                          color: Colors.white,
                          fontWeight: FontWeight.bold,
                          fontSize: 14,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
        ],
      );
    });
  }
}
