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

class Pickup {
  String? id;
  DateTime? pickupDate;
  String? pickupNumber;
  DateTime? fromTime;
  DateTime? toTime;
  //for customer
  String? customerID;
  String? customerName;
  String? customerRemark;

  String? staffId;
  String? staffName;
  String? staffPhoneNumber;

  String? fcsID;
  String? status;
  String? rescheduleRemark;
  String? zoneID;
  String? zoneName;
  DeliveryAddress? pickupAddress;

  // for complete
  String? completeRemark;
  List<String> photoUrls;

  List<Package> packages;

  Pickup(
      {this.id,
      this.customerID,
      this.customerName,
      this.staffId,
      this.staffName,
      this.staffPhoneNumber,
      this.pickupDate,
      this.fromTime,
      this.toTime,
      this.fcsID,
      this.status,
      this.packages = const [],
      this.photoUrls = const [],
      this.completeRemark,
      this.customerRemark,
      this.pickupNumber,
      this.rescheduleRemark,
      this.zoneID,
      this.zoneName,
      this.pickupAddress});

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

  @override
  int get hashCode => id.hashCode;

  bool isChangedForEdit(Pickup pickup) {
    return pickup.completeRemark != this.completeRemark;
  }

  factory Pickup.fromMap(Map<String, dynamic> map, String id) {
    var _pickupDate = (map['pickup_date'] as Timestamp);
    var _fromTime = map['pickup_time_from'] == null
        ? null
        : (map['pickup_time_from'] as Timestamp);
    var _toTime = map['pickup_time_to'] == null
        ? null
        : (map['pickup_time_to'] as Timestamp);

    var da = map['pickup_address'];
    var _da = da != null ? DeliveryAddress.fromMap(da, da["id"]) : null;

    List<String> _photoUrls =
        map['photo_urls'] == null ? [] : List.from(map['photo_urls']);

    return Pickup(
        id: id,
        pickupDate: _pickupDate.toDate().toLocal(),
        pickupNumber: map['pickup_number'],
        pickupAddress: _da,
        fromTime: _fromTime?.toDate().toLocal() ?? null,
        toTime: _toTime?.toDate().toLocal() ?? null,
        staffId: map['pickup_staff_id'],
        staffName: map['pickup_staff_name'],
        customerID: map['customer_id'],
        customerName: map['customer_name'],
        customerRemark: map['customer_remark'] ?? "",
        completeRemark: map['complete_remark'] ?? "",
        rescheduleRemark: map['reschedule_remark'],
        zoneID: map['zone_id'],
        zoneName: map['zone_name'],
        status: map['status'],
        photoUrls: _photoUrls);
  }

  factory Pickup.fromJson(Map<String, dynamic> json) {
    return Pickup(
      id: json['id'],
      pickupDate: DateTime.parse(json['pickup_date']),
      pickupNumber: json['pickup_number'],
      fromTime: DateTime.parse(json['pickup_date']),
      toTime: DateTime.parse(json['pickup_date']),
      staffId: json['pickup_staff_id'],
      staffName: json['pickup_staff_name'],
      customerID: json['customer_id'],
      customerName: json['customer_name'],
      customerRemark: json['customer_remark'] ?? "",
      completeRemark: json['complete_remark'] ?? "",
      rescheduleRemark: json['reschedule_remark'],
      zoneID: json['zone_id'],
      zoneName: json['zone_name'],
      status: json['status'],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      "id": id,
      "pickup_date": pickupDate?.toUtc().toIso8601String(),
      'from_time': fromTime,
      'to_time': toTime,
      'staff_id': staffId,
      'staff_name': staffName,
      "staff_phone_number": staffPhoneNumber,
      'user_id': customerID,
      'user_name': customerName,
      'status': status,
    };
  }

  Map<String, dynamic> toMapForComplete() {
    return {
      "id": id,
      "complete_remark": completeRemark,
      'photo_urls': photoUrls,
    };
  }

  @override
  String toString() {
    return 'Pickup{id: $id}';
  }
}
