

import 'package:supplier_new/common/settings.dart';
import 'package:geolocator/geolocator.dart';

class  PlansModelNew {
  String building_name = '';
  String address = '';
  String type_of_water = '';
  String capacity = '';
  String frequency = '';
  String quantity = '';
  String time = '';
  String averageTime = '';
  String quoted_amount = '';
  String displayAddress='';
  double lat=0;
  double lng=0;
  double distanceInMeters=0;
  double distanceInKm=0.0;
  String dbId = '';
  String status='';
  String date='';
  String imageAsset='images/building.png';
  String delivery_agent_name = '';
  String tanker_name = '';
  String water_source_location='';

  PlansModelNew();

  factory PlansModelNew.fromJson(Map<String, dynamic> json){
    PlansModelNew rtvm = new PlansModelNew();

    rtvm.building_name = json['buildingName'] ?? '';
    rtvm.dbId = json['_id']?? '';
    rtvm.address   = json['address'] ?? '';
    rtvm.type_of_water   = json['typeofwater'] ?? '';
    rtvm.capacity   = json['capacity'] ?? '';
    rtvm.quantity   = json['quantity']?? '';
    rtvm.time   = json['time'] ?? '';
    rtvm.date   = json['dateOfOrder'] ?? '';
    rtvm.status   = json['orderStatus'] ?? '';
    rtvm.quoted_amount   = json['price'].toString() ?? '';
    rtvm.lng=json['longitude'] ?? 0.0;
    rtvm.lat=json['latitude'] ?? 0.0;
    rtvm.delivery_agent_name = json['delivery_agent'] ?? '';
    rtvm.tanker_name = json['tankerName'] ?? '';
    rtvm.water_source_location = json['water_source_location'] ?? '';

    // Split and trim
    List<String> parts = rtvm.address.split(',').map((e) => e.trim()).toList();

// Usually, the locality is the part before the main city (Hyderabad)displayAddress = "";
    if (parts.length >= 2) {
      rtvm.displayAddress = parts[parts.length -4]; // "Banjara Hills"
    }
    // Distance in meters
    rtvm.distanceInMeters = double.parse(
      Geolocator.distanceBetween(
        rtvm.lat,
        rtvm.lng,
        AppSettings.supplierLatitude,
        AppSettings.supplierLongitude,
      ).toStringAsFixed(2),
    );

// Distance in km
    rtvm.distanceInKm = double.parse(
      (rtvm.distanceInMeters / 1000).toStringAsFixed(2),
    );

    rtvm.frequency   = json['frequency']?? '';

    if(rtvm.frequency.toString().toLowerCase()=='weekly_twice'){
      rtvm.quantity="2/Week";
    }
    if(rtvm.frequency.toString().toLowerCase()=='weekly_thrice'){
      rtvm.quantity="2/Week";
    }
    else if(rtvm.frequency.toString().toLowerCase()=='weekly_thrice'){
      rtvm.quantity="3/Week";
    }
    else if(rtvm.frequency.toString().toLowerCase()=='weekly_once'){
      rtvm.quantity="1/Week";
    }
    else if(rtvm.frequency.toString().toLowerCase()=='daily'){
      rtvm.quantity="7/Week";
    }


    return rtvm;
  }
  Map<String, dynamic> toJson() => {
    "boreName":this.building_name,
  };
}