import 'package:test_sa/controllers/api_routes/urls.dart';
import 'package:test_sa/models/department.dart';
import 'package:test_sa/models/device/device.dart';
import 'package:test_sa/models/device/device_transfer_info.dart';
import 'package:test_sa/models/hospital.dart';
import 'package:test_sa/models/lookup.dart';

class DeviceTransfer{
  int 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();
  }

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

  factory DeviceTransfer.fromJson(Map<String,dynamic> parsedJson){
    return DeviceTransfer(
      id: parsedJson["id"],
      title: parsedJson["transferCode"],
      userId: parsedJson["uid"],
      device: Device(
        id: parsedJson["assetId"],
        number: parsedJson["assetNumber"],
        serialNumber: parsedJson["assetSerialNo"],
      ),
      sender: DeviceTransferInfo(
        travelingHours: parsedJson["senderTravelingHours"],
        comment: parsedJson["senderComment"],
        workingHours: parsedJson["senderWorkingHours"],
        userId: parsedJson["senderAssignedEmployeeId"],
        userName: parsedJson["senderAssignedEmployeeName"],
        client: Hospital(
            id: parsedJson["senderSiteId"],
            name: parsedJson["senderSiteName"]
        ),
        department: Department(
          id: parsedJson["senderDepartmentId"],
          name: parsedJson["senderDepartmentName"],
        ),
        // todo add segnature
        signature:  URLs.getFileUrl(parsedJson["senderAttachmentName"]),
        status: Lookup(
          id: parsedJson["senderMachineStatusId"],
          name: parsedJson["senderMachineStatusName"],
        ),
      ),
      receiver: DeviceTransferInfo(
        travelingHours: parsedJson["receiverTravelingHours"],
        comment: parsedJson["receiverComment"],
        workingHours: parsedJson["receiverWorkingHours"],
        userId: parsedJson["receiverAssignedEmployeeId"],
        userName: parsedJson["receiverAssignedEmployeeName"],
        client: Hospital(
            id: parsedJson["destSiteId"],
            name: parsedJson["destSiteName"]
        ),
        department: Department(
          id: parsedJson["destDepartmentId"],
          name: parsedJson["destDepartmentName"],
        ),
        // todo add segnature
        signature: URLs.getFileUrl(parsedJson["receiverAttachmentName"]),
        status: Lookup(
          id: parsedJson["receiverMachineStatusId"],
          name: parsedJson["receiverMachineStatusName"],
        ),
      ),
    );
  }
}

