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

import 'ex.dart';

class DistanceController extends GetxController {
  final DistanceService _distanceService = DistanceService();
  RxDouble distance = 0.0.obs;

  void calculateDistance(double pickupLat, double pickupLong, double dropLat, double dropLong) async {
    try {
      final distanceValue = await _distanceService.getDistance(pickupLat, pickupLong, dropLat, dropLong);
      distance(distanceValue);
    } catch (e) {
      print(e);
    }
  }
}


class MyDistanceCalculate extends StatelessWidget {
  final DistanceController distanceController = Get.put(DistanceController());

  final lat1 = 11.0043693;
  final lon1 = 77.00916169999999;

  final lat2 = 11.0195965;
  final lon2 = 76.9727256;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Distance Calculator'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
              onPressed: () {
                final pickupLat = double.tryParse(lat1.toString());
                final pickupLong = double.tryParse(lon1.toString());
                final dropLat = double.tryParse(lat2.toString());
                final dropLong = double.tryParse(lon2.toString());
                if (pickupLat != null && pickupLong != null && dropLat != null && dropLong != null) {
                  distanceController.calculateDistance(pickupLat, pickupLong, dropLat, dropLong);
                } else {
                  // Handle invalid input
                  print('Invalid input');
                }
              },
              child: Text('Calculate Distance'),
            ),
            Obx(() => Text('Distance: ${distanceController.distance.toStringAsFixed(2)} kms')),
          ],
        ),
      ),




    );
  }
}