class CargoType {
  String? id;
  String? name;
  double rate;
  double weight;
  bool isChecked;
  int qty;
  bool? isCutomDuty;
  double customDutyFee;
  double calRate;
  double calWeight;

  double get calAmount => calRate * calWeight;

  CargoType(
      {this.id,
      this.name,
      this.rate = 0,
      this.weight = 0,
      this.calWeight = 0,
      this.calRate = 0,
      this.isChecked = false,
      this.qty = 0,
      this.isCutomDuty,
      this.customDutyFee = 0});

  factory CargoType.fromMap(Map<String, dynamic> map, String id) {
    return CargoType(
        id: id,
        name: map['name'],
        rate: map['rate']?.toDouble() ?? 0,
        weight: map['weight']?.toDouble() ?? 0,
        calWeight: map['cal_weight']?.toDouble() ?? 0,
        calRate: map['cal_rate']?.toDouble() ?? 0,
        isCutomDuty: map['custom_duty'] ?? false,
        customDutyFee: (map['custom_duty_fee'] ?? 0).toDouble(),
        qty: (map['qty'] ?? 0).toInt());
  }

  Map<String, dynamic> toMap() {
    return {
      "id": id,
      'name': name,
      'rate': rate,
      'weight': weight,
      'cal_weight': calWeight,
      'cal_rate': calRate,
      'custom_duty': isCutomDuty,
      'custom_duty_fee': customDutyFee,
      'qty': qty
    };
  }

  CargoType clone() {
    return CargoType.fromMap(toMap(), this.id!);
  }

  @override
  bool operator ==(Object other) => other is CargoType && other.id == id;

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return name ?? "";
  }

  bool isChangedForEdit(CargoType cargoType) {
    return cargoType.name != this.name || cargoType.rate != this.rate;
  }

  bool isChangedForEditCustomDuty(CargoType cargoType) {
    return cargoType.name != this.name ||
        cargoType.customDutyFee != this.customDutyFee ||
        cargoType.rate != this.rate;
  }
}
