import "package:client/data.dart";
import "package:client/services.dart";
import "package:flutter/widgets.dart" hide Path;

import "view_model.dart";

/// The view model for the home page.
class HomeModel extends ViewModel {
  final startLatitudeController = TextEditingController();
  final startLongitudeController = TextEditingController();
  final endLatitudeController = TextEditingController();
  final endLongitudeController = TextEditingController();

  Path? path;

  double? get startLatitude => double.tryParse(startLatitudeController.text);
  double? get startLongitude => double.tryParse(startLongitudeController.text);
  double? get endLatitude => double.tryParse(endLatitudeController.text);
  double? get endLongitude => double.tryParse(endLongitudeController.text);

  bool isSearching = false;
  bool isGoogleReady = false;

  @override
  Future<void> init() async {
    await Future<void>.delayed(const Duration(seconds: 2));
    isGoogleReady = true;
    notifyListeners();
  }

  Future<void> search() async {
    final start = (lat: startLatitude, long: startLongitude);
    final end = (lat: endLatitude, long: endLongitude);
    if (start.lat == null || start.long == null) return;
    if (end.lat == null || end.long == null) return;
    isSearching = true;
    isLoading = true;
    path = await services.api.getPath(start: start as Coordinates, end: end as Coordinates);
    if (path != null) isSearching = false;
    isLoading = false;
  }
}
