import 'package:fcs/domain/entities/custom_duty.dart';
import 'package:fcs/domain/entities/discount_by_weight.dart';

import 'cargo_type.dart';

class Rate {
  double deliveryFee;
  double freeDeliveryWeight;
  double volumetricRatio;

  List<CargoType> cargoTypes;
  List<CustomDuty> customDuties;
  List<DiscountByWeight> discountByWeights;

  DiscountByWeight getDiscountByWeight(double weight) {
    discountByWeights.sort((d1, d2) => d2.weight.compareTo(d1.weight));
    return discountByWeights.firstWhere((e) => e.weight < weight,
        orElse: () => null);
  }

  CargoType get defaultCargoType => cargoTypes == null
      ? null
      : cargoTypes.firstWhere((e) => e.name == "General");

  Rate({
    this.deliveryFee,
    this.freeDeliveryWeight,
    this.volumetricRatio,
  });

  factory Rate.fromMap(Map<String, dynamic> map) {
    return Rate(
      deliveryFee: (map['delivery_fee'] ?? 0).toDouble(),
      freeDeliveryWeight: (map['free_delivery_weight'] ?? 0).toDouble(),
      volumetricRatio: (map['volumetric_ratio'] ?? 0).toDouble(),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      "delivery_fee": deliveryFee,
      'free_delivery_weight': freeDeliveryWeight,
      'volumetric_ratio': volumetricRatio,
    };
  }

  @override
  String toString() {
    return 'Rate{deliveryFee:$deliveryFee,freeDeliveryWeight:$freeDeliveryWeight,volumetricRatio:$volumetricRatio}';
  }
}
