import 'dart:io';

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

class Package {
  String? id;
  String? trackingID;
  String? userID;
  String? fcsID;
  String? userName;
  String? phoneNumber;
  DateTime? currentStatusDate;
  List<String> photoUrls;
  List<ShipmentStatus> shipmentHistory;
  String? desc;

  String? status;
  String? shipmentNumber;
  String? senderFCSID;
  String? senderName;
  String? senderPhoneNumber;
  String? boxNumber;
  String? cargoDesc;
  String? market;
  bool isChecked;

  int rate;
  int weight;
  String? packageType;
  String? pickUpID;
  // List<String> photos;
  String? remark;
  DateTime? arrivedDate;
  DeliveryAddress? deliveryAddress;

  //for packages in processing
  List<File?> photoFiles;

  int get amount => rate != null && weight != null ? rate * weight : 0;

  double get price => rate.toDouble() * weight;

  Package(
      {this.id,
      this.trackingID,
      this.userID,
      this.userName,
      this.fcsID,
      this.phoneNumber,
      this.shipmentNumber,
      this.senderFCSID,
      this.senderName,
      this.boxNumber,
      this.rate = 0,
      this.weight = 0,
      this.packageType,
      this.pickUpID,
      this.remark,
      this.status,
      this.arrivedDate,
      this.cargoDesc,
      this.market,
      this.shipmentHistory = const [],
      this.currentStatusDate,
      this.photoUrls = const [],
      this.desc,
      this.deliveryAddress,
      this.isChecked = false,
      this.photoFiles = const [],
      this.senderPhoneNumber});

  factory Package.fromMap(Map<String, dynamic> map, String docID) {
    var _currentStatusDate = (map['status_date'] as Timestamp);

    List<ShipmentStatus> _shipmentStatus = List.from(map['all_status'])
        .map((e) => ShipmentStatus.fromMap(Map<String, dynamic>.from(e)))
        .toList();
    List<String> _photoUrls =
        map['photo_urls'] == null ? [] : List.from(map['photo_urls']);
    var da = map['delivery_address'];
    var _da = da != null ? DeliveryAddress.fromMap(da, da["id"]) : null;

    return Package(
        id: docID,
        userID: map['user_id'],
        fcsID: map['fcs_id'],
        trackingID: map['tracking_id'],
        market: map['market'],
        userName: map['user_name'],
        phoneNumber: map['phone_number'],
        remark: map['remark'],
        desc: map['desc'],
        status: map['status'],
        senderFCSID: map['sender_fcs_id'],
        senderName: map['sender_name'],
        senderPhoneNumber: map['sender_phone_number'],
        deliveryAddress: _da,
        currentStatusDate: _currentStatusDate.toDate().toLocal(),
        photoUrls: _photoUrls,
        shipmentHistory: _shipmentStatus);
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'tracking_id': trackingID,
        'market': market,
        'fcs_id': fcsID,
        'sender_fcs_id': senderFCSID,
        "desc": desc,
        "remark": remark,
        "photo_urls": photoUrls
      };

  factory Package.fromJson(Map<String, dynamic> json) {
    return Package(
        id: json['id'],
        trackingID: json['tracking_id'],
        market: json['market'],
        userName: json['user_name'],
        phoneNumber: json['phone_number'],
        senderFCSID: json['sender_fcs_id'],
        senderName: json['sender_name'],
        senderPhoneNumber: json['sender_phone_number'],
        status: json['status'],
        currentStatusDate: DateTime.parse(json['status_date']));
  }

  bool isChangedForEdit(Package package) {
    return package.trackingID != this.trackingID ||
        package.remark != this.remark ||
        package.fcsID != this.fcsID;
  }

  bool isChangedForEditProcessing(Package package) {
    return package.trackingID != this.trackingID ||
        package.fcsID != this.fcsID ||
        package.market != this.market ||
        package.desc != this.desc ||
        package.remark != this.remark;
  }

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

  @override
  int get hashCode => id.hashCode;

  @override
  String toString() {
    return 'Package{id: $id, status: $status, market:$market, trackingID: $trackingID,}';
  }
}
