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

import '../../constant/api_key.dart';
import '../../constant/links.dart';
import '../functions/crud.dart';
import '../functions/location_controller.dart';

class PointsForRiderController extends GetxController {
  List<String> locations = [];
  String hintTextDestinationPoint = 'Search for your destination'.tr;
  TextEditingController placeStartController = TextEditingController();

  void addLocation(String location) {
    locations.add(location);
    update();
  }

  void getTextFromList(String location) {
    locations.add(location);
    update();
    Get.back();
  }

  void removeLocation(int index) {
    locations.removeAt(index);
    update();
  }

  void onReorder(int oldIndex, int newIndex) {
    if (newIndex > oldIndex) {
      newIndex -= 1;
      update();
    }

    final item = locations.removeAt(oldIndex);
    locations.insert(newIndex, item);
    update();
  }
}

class LocationModel {
  String name;
  double lat, lon;

  LocationModel({required this.name, required this.lat, required this.lon});
}

class WayPointController extends GetxController {
  // A list of text editing controllers for each text field
  // final textFields = [TextEditingController()].obs;
  List<String> wayPoints = [];
  List<List<dynamic>> placeListResponse = [];
  double wayPointHeight = 400;
  String hintTextDestinationPoint = 'Search for your destination'.tr;
  TextEditingController textSearchCotroller = TextEditingController();
  // A list of places corresponding to each text field
  final places = <String>[];

  final hintTextPointList = <String>[];
  late LatLng myLocation;

  void addWayPoints() {
    String wayPoint = 'Add a Stop'.tr;

    if (wayPoints.length < 5) {
      wayPoints.add(wayPoint);
      update();
    } else {
      Get.defaultDialog(
          title: 'This is most WayPoints',
          titleStyle: AppStyle.title,
          middleText: '');
    }
    update();
  }

  void removeTextField(int index) {
    wayPoints.removeAt(index);

    update();
  }

  // A method to reorder the text fields and the places
  void reorderTextFields(int oldIndex, int newIndex) {
    if (newIndex > oldIndex) {
      newIndex -= 1;
    }
    final wayPoint = wayPoints.removeAt(oldIndex);
    wayPoints.insert(newIndex, wayPoint);
    update();
  }

  void updatePlace(int index, String input) async {
    var url =
        '${AppLink.googleMapsLink}place/nearbysearch/json?keyword=$input&location=${myLocation.latitude},${myLocation.longitude}&radius=50000&language=en&key=${AK.mapAPIKEY.toString()}';
    var response = await CRUD().getGoogleApi(link: url, payload: {});
    // final place = input;
    // if (index == 0) {
    List<dynamic> newList = [];
    placeListResponse.add(newList);
    newList = response['results'];
    placeListResponse[index].add(newList);
    update();
    // }

    update();
  }

  @override
  void onInit() {
    Get.put(LocationController());
    addWayPoints();
    myLocation = Get.find<LocationController>().myLocation;
    super.onInit();
  }
}

class PlaceList extends StatelessWidget {
  // Get the controller instance
  final controller = Get.put(WayPointController());

  @override
  Widget build(BuildContext context) {
    // Use the Obx widget to rebuild the widget when the controller changes
    return Obx(() {
      // Use the ListView widget to display the list of places
      return ListView(
        // The children of the list are the places
        children: [
          // Loop through the places in the controller
          for (final place in controller.places)
            // Create a text widget for each place
            Text(
              // Use the place as the text
              place,

              // Add some style and padding
              style: const TextStyle(fontSize: 18.0),
            ),
        ],
      );
    });
  }
}
