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

class ServiceRequest {
  String id;
  String requestCode;
  String deviceSerialNumber;
  int deviceId;
  String deviceArName;
  String deviceEnName;
  List<String> devicePhotos;
  String callComments;
  String hospitalName;
  int hospitalId;
  String departmentName;
  String engineerName;
  String engineerId;
  String date;
  String audio;
  int statusValue;
  String statusLabel;
  String reviewComment;
  String comments;

  bool viewReport;
  String engineerMobile;
  String deviceModel;
  String faultDescription;
  String workPerformed;
  String visitDate;
  DateTime nextVisitDate;
  String jobSheetNumber;
  int reportID;
  String deviceNumber;
  Lookup priority;
  Lookup defectType;
  Lookup type;
  Lookup requestedThrough;
  Lookup firstAction;
  Lookup loanAvailability;
  Device device;

  ServiceRequest({
    this.id,
    this.date,
    this.requestCode,
    this.hospitalName,
    this.deviceArName,
    this.deviceEnName,
    this.devicePhotos,
    this.hospitalId,
    this.deviceSerialNumber,
    this.callComments,
    this.statusLabel,
    this.statusValue,
    this.departmentName,
    this.deviceId,
    this.audio,
    this.engineerName,
    this.engineerId,
    this.viewReport = false,
    this.deviceModel,
    this.engineerMobile,
    this.faultDescription,
    this.jobSheetNumber,
    this.visitDate,
    this.nextVisitDate,
    this.workPerformed,
    this.reportID,
    this.defectType,
    this.priority,
    this.deviceNumber,
    this.type,
    this.requestedThrough,
    this.device,
    this.reviewComment,
    this.comments,
    this.loanAvailability,
    this.firstAction,
  });

  factory ServiceRequest.fromJson(Map<String, dynamic> parsedJson) {
    List<String> images = [];
    if (parsedJson["attachmentsCallRequest"] is List) {
      List list = parsedJson["attachmentsCallRequest"];
      images = list.map((e) => URLs.getFileUrl(e["attachmentsCallRequest"])).toList();
    }
    return ServiceRequest(
      id: parsedJson["id"].toString(),
      requestCode: parsedJson["callNo"].toString(),
      hospitalName: parsedJson["asset"]["site"]["custName"],
      deviceNumber: parsedJson["asset"]["assetNumber"].toString(),
      deviceId: parsedJson["asset"]["id"],
      audio: URLs.getFileUrl(parsedJson["voiceNote"] ?? ""),
      deviceArName: parsedJson["asset"]["modelDefinition"]["assetName"] ?? "",
      deviceEnName: parsedJson["asset"]["modelDefinition"]["assetName"] ?? "",
      devicePhotos: images,
      deviceSerialNumber: parsedJson["asset"]["assetSerialNo"],
      date: DateTime.tryParse(parsedJson["requestedDate"] ?? "").toString().split(" ").first,
      callComments: parsedJson["callComments"],
      comments: parsedJson["comments"],
      statusLabel: parsedJson["status"] == null ? null : parsedJson["status"]["name"],
      statusValue: parsedJson["status"] == null ? null : parsedJson["status"]["value"],
      departmentName: parsedJson["asset"]["department"] != null ? parsedJson["asset"]["department"]["name"] : "",
      engineerName: parsedJson["assignedEmployee"] == null ? null : parsedJson["assignedEmployee"]["name"],
      engineerId: parsedJson["assignedEmployee"] == null ? null : parsedJson["assignedEmployee"]["id"],
      hospitalId: parsedJson["asset"]["site"]["id"],
      reportID: parsedJson["workOrder"] != null ? parsedJson["workOrder"]["workOrderId"] : null,
      viewReport: parsedJson["workOrder"] != null,
      deviceModel: parsedJson["asset"]["modelDefinition"]["modelName"],
      engineerMobile: parsedJson["assignedEmployee"] == null ? null : parsedJson["assignedEmployee"]["phone"],
      faultDescription: parsedJson["workOrder"] != null ? parsedJson["workOrder"]["faultDescription"] : null,
      jobSheetNumber: parsedJson["workOrder"] != null ? parsedJson["workOrder"]["jobSheetNumber"] : null,
      visitDate: DateTime.tryParse(parsedJson["visitDate"] ?? "").toString().split(" ").first,
      nextVisitDate: DateTime.tryParse(parsedJson["nextVisitDate"] ?? ""),
      workPerformed: parsedJson["workOrder"] != null ? parsedJson["workOrder"]["workPerformed"] : null,
      device: Device.fromJson(parsedJson["asset"]),
      reviewComment: parsedJson["reviewComment"],
      type: Lookup.fromJson(parsedJson['typeofRequest']),
      defectType: Lookup.fromJson(parsedJson['defectType']),
      loanAvailability: Lookup.fromJson(parsedJson['loanAvailability']),
      firstAction: Lookup.fromJson(parsedJson['firstAction']),
      requestedThrough: Lookup.fromJson(parsedJson['requestedThrough']),
      priority: Lookup.fromJson(parsedJson['priority']),
    );
  }
}
