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';

import '../timer_model.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"],
          destBuildingName: parsedJson["destBuildingName"],
          destDepartmentName: parsedJson["destDepartmentName"],
          destFloor: parsedJson["destFloor"],
          destRoom: parsedJson["destRoom"],
          destSiteName: parsedJson["destSiteName"]),
      sender: DeviceTransferInfo(
        travelingHours: parsedJson["senderTravelingHours"],
        comment: parsedJson["senderComment"],
        workingHours: parsedJson["senderWorkingHours"],
        timer: TimerModel(
          startAt: DateTime.tryParse(parsedJson["senderStartDate"] ?? ""),
          endAt: DateTime.tryParse(parsedJson["senderEndDate"] ?? ""),
          durationInSecond: ((parsedJson["workingHours"] ?? 0) * 60 * 60).toInt(),
          stopped: parsedJson["workingHours"] == null ? null : true,
        ),
        // startDate: parsedJson["senderStartDate"] == null ? null : DateTime.parse(parsedJson["senderStartDate"]),
        // endDate: parsedJson["senderEndDate"] == null ? null : DateTime.parse(parsedJson["senderEndDate"]),
        userId: parsedJson["senderAssignedEmployeeId"],
        userName: parsedJson["senderAssignedEmployeeName"],
        assignedEmployeeName: 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"],
        timer: parsedJson["receiverStartDate"] != null || parsedJson["receiverEndDate"] != null
            ? TimerModel(startAt: DateTime.tryParse(parsedJson["receiverStartDate"] ?? ""), endAt: DateTime.tryParse(parsedJson["receiverEndDate"] ?? ""))
            : null,
        // startDate: parsedJson["receiverStartDate"] == null ? null : DateTime.parse(parsedJson["receiverStartDate"]),
        // endDate: parsedJson["receiverEndDate"] == null ? null : DateTime.parse(parsedJson["receiverEndDate"]),
        userId: parsedJson["receiverAssignedEmployeeId"],
        userName: parsedJson["receiverAssignedEmployeeName"],
        assignedEmployeeName: 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"],
        ),
      ),
    );
  }
}
