import 'package:flutter/material.dart';
import 'package:generp/Notifiers/NearByGeneratorsProvider.dart';
import 'package:generp/Utils/app_colors.dart';
import 'package:generp/Utils/commonWidgets.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:provider/provider.dart';

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

  @override
  State<Nearbygenerators> createState() => _NearbygeneratorsState();
}

class _NearbygeneratorsState extends State<Nearbygenerators> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      var provider = Provider.of<Nearbygeneratorsprovider>(context,listen: false);
      provider.getLocationPermission(context);
    },);
  }

  Future infoDialogue(BuildContext context) async {

    return await showDialog(
      context: context,
      builder:
          (context) => Consumer<Nearbygeneratorsprovider>(
        builder: (context,provider,child) {
          return StatefulBuilder(
            builder:
                (context, setState) =>
                AlertDialog(
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10.0),
                  ),
                  title: Column(
                    children: [
                      Row(
                        children: [
                          Expanded(
                            child: Align(
                              alignment: Alignment.topLeft,
                              child: Text(
                                'Filter',
                                style: TextStyle(
                                  color: Colors.black,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                          ),
                          InkWell(
                            child: Icon(Icons.close),
                            onTap: () {
                              setState(() {
                                provider.currentValue = 0.0;
                                provider.selectedItem = "Active";
                              });
                              Navigator.pop(context);
                            },
                          ),
                        ],
                      ),
                      Divider(
                        color: Colors.grey,
                        thickness: 1.0,
                        height: 0.0,
                      ),
                    ],
                  ),

                  content: Container(
                    height: 230,
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Text(
                              "Radius",
                              style: TextStyle(
                                fontSize: 18.0,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                            Spacer(),
                            Text(
                              '${provider.currentValue.toStringAsFixed(2)} KM',
                              style: TextStyle(
                                fontSize: 18.0,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ],
                        ),
                        Slider(
                          value: provider.currentValue,
                          max: 100,
                          divisions: 100,
                          label: provider.currentValue.toStringAsFixed(2),
                          inactiveColor: Color(0xFFD7D7D7),
                          activeColor: AppColors.cyan_blue,
                          onChanged: (value) {
                            provider.currentValue = value;
                            provider.debounce(() {
                              provider.LoadNearbyGeneratorsAPI(context);
                            }, Duration(milliseconds: 500));
                          },
                        ),
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Text(
                            'Status',

                            style: TextStyle(
                              fontSize: 18.0,
                              fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                        Container(
                          child: Row(
                            children: [
                              Expanded(
                                child: DropdownButton<String>(
                                  value: provider.selectedItem,
                                  items:
                                  <String>[
                                    'Active',
                                    'Inactive',
                                    'Suspense',
                                  ].map<DropdownMenuItem<String>>((String value,) {
                                    return DropdownMenuItem<String>(
                                      value: value,
                                      child: Text(value),
                                    );
                                  }).toList(),
                                  onChanged: (String? newValue) {
                                    setState(() {
                                      provider.selectedItem = newValue!;
                                    });
                                  },
                                  icon: Icon(
                                    Icons.keyboard_arrow_down,
                                  ),
                                  iconSize: 12,
                                  iconEnabledColor:
                                  Colors
                                      .black, // Remove the default dropdown icon
                                ),
                              ),
                            ],
                          ),
                        ),
                        SizedBox(height: 30.0),
                        Container(
                          child: InkWell(
                            onTap: () {
                              provider.debounce(() {
                                provider.LoadNearbyGeneratorsAPI(context);
                                Navigator.pop(context);
                              }, Duration(milliseconds: 500));
                            },
                            child: Container(
                              alignment: Alignment.center,
                              height: 45,
                              margin: EdgeInsets.only(
                                left: 15.0,
                                right: 15.0,
                              ),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(10.0),
                              ),
                              child: Text(
                                "Search",
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                  fontFamily: 'Nexa',
                                  fontWeight: FontWeight.w700,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
          );
        }),
      barrierDismissible: true,
    ) ??
        false;
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("Nearbygenerators widget rebuilt");
    return Consumer<Nearbygeneratorsprovider>(builder: (context, provider, child) {
      var sendWidget = GestureDetector(
          onTap: () {
            infoDialogue(context);
          },
          child: InkWell(
            child: Icon(Icons.filter_alt_outlined),
          ));

      return WillPopScope(
        onWillPop: () => onBackPressed(context),
        child: Scaffold(
          resizeToAvoidBottomInset: true,
          appBar: appbar2(context, "Nearby Generators",sendWidget),
          backgroundColor: AppColors.scaffold_bg_color,
          body: Container(
            child: SingleChildScrollView(
              child:Column(
                children: [
                  ClipRRect(
                    // Apply border radius using ClipRRect
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(30.0),
                      topRight: Radius.circular(30.0),
                    ),
                    // padding: EdgeInsets.fromLTRB(10, 20, 10, 20),
                    child: Container(
                      height: MediaQuery.of(context).size.height,
                      child: Stack(children: [
                        GoogleMap(
                          myLocationEnabled: true,
                          zoomGesturesEnabled: true,

                          initialCameraPosition: CameraPosition(
                            target: provider.startLocation,
                            zoom: 14.0,
                          ),
                          markers:provider.markers.toSet(),
                          mapType: MapType.normal,
                          onMapCreated: (controller) {
                            setState(() {
                              provider.mapController = controller;
                            });
                          },
                          onCameraMove: (position) {
                            provider.onCameraMove(context,position);
                          },
                        ),
                      ]),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      );
    },);
  }
}
