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

class  PlanRequestsModel {
  String building_name = '';
  String address = '';
  String type_of_water = '';
  String capacity = '';
  String quantity = '';
  String time = '';
  String averageTime = '';
  String quoted_amount = '';
  String bidding_price = '';
  String displayAddress='';
  double lat=0;
  double lng=0;
  double distanceInMeters=0;
  double distanceInKm=0.0;
  String dbId = '';
  String status='';
  String frequency='';
  String endDate='';
  String startDate='';
  List dates=[];

  PlanRequestsModel();

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

    rtvm.building_name = json['customer_details']['buildingName'] ?? '';
    rtvm.dbId = json['_id']?? '';
    rtvm.address = json['customer_details']['profile']['address1'] ?? '';
    rtvm.type_of_water   = json['type_of_water'] ?? '';
    rtvm.capacity   = json['capacity'] ?? '';
    rtvm.quantity   = json['quantity']?? '';
    rtvm.frequency   = json['frequency']?? '';
    rtvm.dates   = json['dates']?? [];

    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";
    }


    rtvm.averageTime   = json['time'] ?? '';
    rtvm.endDate   = json['end_date'] ?? '';
    rtvm.startDate   = json['start_date'] ?? '';
    rtvm.time   = json['my_supplier_entry']['time'] ?? '';
    rtvm.status   = json['my_supplier_entry']['status'] ?? '';
    rtvm.quoted_amount   = json['my_supplier_entry']['quoted_amount'].toString() ?? '';
    rtvm.bidding_price   = json['bidding_price'].toString() ?? '';
    rtvm.lng=json['customer_details']['longitude'] ?? 0.0;
    rtvm.lat=json['customer_details']['latitude'] ?? 0.0;

    // 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),
    );

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