import 'device.dart';
import 'device_transfer_info.dart';

class DeviceTransfer {
  String? id;
  String? userId;
  String? title;
  Device? device;
  DeviceTransferInfo? sender;
  DeviceTransferInfo? receiver;

  DeviceTransfer({
    this.id,
    this.device,
    this.title,
    this.userId,
    this.sender,
    this.receiver,
  });

  bool validate() {
    if (device == null) return false;
    return receiver?.validate() ?? false;
  }

  fromDeviceTransfer(DeviceTransfer old) {
    id = old.id;
    title = old.title;
    userId = old.userId;
    device = old.device != null ? Device.fromDevice(old.device!) : null;
    final sender = DeviceTransferInfo();
    if (old.sender != null) {
      sender.fromDetails(old.sender!);
    }
    this.sender = sender;
    final receiver = DeviceTransferInfo();
    if (old.receiver != null) {
      receiver.fromDetails(old.receiver!);
    }
    this.receiver = receiver;
  }

  factory DeviceTransfer.fromJson(Map<String, dynamic> parsedJson) {
    return DeviceTransfer(
      id: parsedJson["id"],
      title: parsedJson["title"],
      userId: parsedJson["uid"],
      device: Device.fromJson(parsedJson["eq_sn"]),
      sender: DeviceTransferInfo.fromJson(parsedJson, "sender_"),
      receiver: DeviceTransferInfo.fromJson(parsedJson, "receiver_"),
    );
  }
}
