import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fcs/domain/constants.dart';
import 'package:fcs/domain/entities/carton.dart';
import 'package:fcs/domain/vo/delivery_address.dart';

class Shipment {
  String id;
  String shipmentNumber;
  String shipmentType;
  DeliveryAddress pickupAddress;
  DateTime pickupDate;
  String pickupTimeStart;
  String pickupTimeEnd;

  String userName;
  String userID;
  String phoneNumber;
  int numberOfPackage;
  int weight;
  double handlingFee;
  double paidHandlingFee;
  String address;
  String status;
  bool isCourier;
  int radioIndex;
  List<Carton> boxes;

  String pickupUserID;
  String pickupUserName;
  String pickupUserPhoneNumber;

  String fcsShipmentID;
  String fcsShipmentNumber;
  String shipmentLabelUrl;
  bool isSelected;

  Shipment(
      {this.id,
      this.shipmentNumber,
      this.shipmentType,
      this.userID,
      this.userName,
      this.phoneNumber,
      this.pickupTimeStart,
      this.pickupTimeEnd,
      this.numberOfPackage,
      this.weight,
      this.handlingFee,
      this.paidHandlingFee,
      this.address,
      this.status,
      this.pickupDate,
      this.isCourier = false,
      this.radioIndex = 1,
      this.pickupAddress,
      this.pickupUserID,
      this.pickupUserName,
      this.pickupUserPhoneNumber,
      this.fcsShipmentID,
      this.fcsShipmentNumber,
      this.shipmentLabelUrl,
      this.boxes});

  int get last => DateTime.now().difference(pickupDate).inDays;

  double get totalWeight => boxes?.fold(0, (p, e) => p + e.actualWeight);
  int get totalCount => boxes?.length;

  bool get isPending => status == shipment_pending_status;
  bool get isAssigned => status == shipment_assigned_status;
  bool get isPickuped => status == shipment_pickuped_status;
  bool get isPacked => status == shipment_packed_status;
  bool get isConfirmed => status == shipment_confirmed_status;
  bool get isReceived => status == shipment_received_status;

  factory Shipment.fromMap(Map<String, dynamic> map, String id) {
    var pd = (map['pickup_date'] as Timestamp);
    var pa = map['pickup_address'];
    var _pa = pa != null ? DeliveryAddress.fromMap(pa, pa["id"]) : null;
    return Shipment(
        id: id,
        userName: map['user_name'],
        userID: map['user_id'],
        shipmentNumber: map['shipment_number'],
        phoneNumber: map['phone_number'],
        pickupDate: pd == null ? null : pd.toDate(),
        pickupTimeStart: map['pickup_time_start'],
        pickupTimeEnd: map['pickup_time_end'],
        status: map['status'],
        shipmentType: map['shipment_type'],
        pickupUserID: map['pickup_user_id'],
        pickupUserName: map['pickup_user_name'],
        pickupUserPhoneNumber: map['pickup_user_phone_number'],
        handlingFee: map['handling_fee'],
        paidHandlingFee: map['paid_handling_fee'],
        fcsShipmentID: map['fcs_shipment_id'],
        fcsShipmentNumber: map['fcs_shipment_number'],
        shipmentLabelUrl: map['shipment_label_url'],
        pickupAddress: _pa);
  }

  Map<String, dynamic> toMap() {
    List _boxes = boxes?.map((l) => l.toMap())?.toList() ?? [];

    return {
      "id": id,
      'user_id': userID,
      'cartons': _boxes,
      'shipment_type': shipmentType,
      'pickup_address': pickupAddress.toMap(),
      "pickup_date": pickupDate?.toUtc()?.toIso8601String(),
      'pickup_time_start': pickupTimeStart,
      'pickup_time_end': pickupTimeEnd,
      'pickup_user_id': pickupUserID,
      'pickup_user_name': pickupUserName,
      'pickup_user_phone_number': pickupUserPhoneNumber,
      'handling_fee': handlingFee,
      'fcs_shipment_id': fcsShipmentID,
      'shipment_label_url': shipmentLabelUrl
    };
  }

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

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'PickUp{id:$id, userName:$userName,phoneNumber:$phoneNumber,numberOfPackage:$numberOfPackage,weight:$weight,status:$status}';
  }
}
