import 'dart:typed_data';

import 'package:test_sa/models/device/asset.dart';
import 'package:test_sa/models/generic_attachment_model.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/service_request/service_report.dart';
import 'package:test_sa/models/service_request/spare_parts.dart';
import 'package:test_sa/models/service_request/supp_engineer_work_orders.dart';
import 'package:test_sa/models/service_request/supplier_details.dart';
import 'package:test_sa/models/service_request/wo_call_request.dart';
import 'package:test_sa/models/service_request/wo_parent.dart';
import 'package:test_sa/models/timer_model.dart';

import '../fault_description.dart';
import '../new_models/assigned_employee.dart';
import '../new_models/assistant_employee.dart';
import 'contact_person_work_order.dart';

class SearchWorkOrder {
  SearchWorkOrder(
      {this.id,
      this.parentWOId,
      this.workOrderNo,
      this.workOrderYear,
      this.workOrderSequennce,
      this.callRequest,
      this.assetType,
      this.assignedEmployee,
      this.visitDate,
      this.assistantEmployees,
      this.supplier,
      this.vendorTicketNumber,
      this.contactPersonWorkOrders,
      this.calllastSituation,
      this.currentSituation,
      this.repairLocation,
      this.reason,
      // this.startofWorkTime,
      // this.endofWorkTime,
      this.workingHours,
      this.travelingHours,
      this.travelingExpenses,
      this.faultDescription,
    this.sparePartsWorkOrders,
    this.reviewComment,
    this.comment,
    List<GenericAttachmentModel>? attachmentsWorkOrder,
    this.equipmentStatus,
    this.suppEngineerWorkOrders,
      this.engSignature,
      this.nurseSignature,
      this.woParentDto,
      this.timer,
      this.mrNumber,
      this.loanAvailablity,
      this.assetLoan});

  SearchWorkOrder.fromJson(dynamic json) {
    id = json['id'];
    parentWOId = json['parentWOId'];
    workOrderNo = json['workOrderNo'];
    workOrderYear = json['workOrderYear'];
    workOrderSequennce = json['workOrderSequennce'];
    callRequest = json['callRequest'] != null ? CallRequest.fromJson(json['callRequest']) : null;
    assetType = json['assetType'] != null ? Lookup.fromJson(json['assetType']) : null;
    assignedEmployee = json['assignedEmployee'] != null ? AssignedEmployee.fromJson(json['assignedEmployee']) : null;
    visitDate = json['visitDate'];
    mrNumber = json['mrNumber'];
    if (json['assistantEmployees'] != null) {
      assistantEmployees = [];
      json['assistantEmployees'].forEach((v) {
        assistantEmployees!.add(AssistantEmployees.fromJson(v));
      });
    }
    supplier = json['supplier'] != null ? SupplierDetails.fromJson(json['supplier']) : null;
    vendorTicketNumber = json['vendorTicketNumber'];
    if (json['contactPersonWorkOrders'] != null) {
      contactPersonWorkOrders = [];
      json['contactPersonWorkOrders'].forEach((v) {
        contactPersonWorkOrders!.add(ContactPersonWorkOrder.fromJson(v));
      });
    }
    calllastSituation = json['calllastSituation'] != null ? Lookup.fromJson(json['calllastSituation']) : null;
    currentSituation = json['currentSituation'] != null ? Lookup.fromJson(json['currentSituation']) : null;
    repairLocation = json['repairLocation'] != null ? Lookup.fromJson(json['repairLocation']) : null;
    reason = json['reason'] != null ? Lookup.fromJson(json['reason']) : null;
    // startofWorkTime = json['startofWorkTime'];
    // endofWorkTime = json['endofWorkTime'];
    if (json['startofWorkTime'] != null || json['endofWorkTime'] != null) {
      timer = TimerModel();
    }
    if (json['startofWorkTime'] != null) timer!.startAt = DateTime.tryParse(json['startofWorkTime']);
    if (json['endofWorkTime'] != null) timer!.endAt = DateTime.tryParse(json['endofWorkTime']);
    workingHours = json['workingHours'];
    travelingHours = json['travelingHours'];
    travelingExpenses = json['travelingExpenses'];
    faultDescription = json['faultDescription'] != null ? FaultDescription.fromJson(json['faultDescription']) : null;
    if (json['sparePartsWorkOrders'] != null) {
      sparePartsWorkOrders = [];
      json['sparePartsWorkOrders'].forEach((v) {
        sparePartsWorkOrders!.add(SparePartsWorkOrders.fromJson(v));
      });
    }
    reviewComment = json['reviewComment'];
    comment = json['comment'];
    if (json['attachmentsWorkOrder'] != null) {
      attachmentsWorkOrder = [];
      json['attachmentsWorkOrder'].forEach((v) {
        attachmentsWorkOrder!.add(GenericAttachmentModel.fromJson(v));
      });
    }
    equipmentStatus = json['equipmentStatus'] != null ? Lookup.fromJson(json['equipmentStatus']) : null;
    if (json['suppEngineerWorkOrders'] != null) {
      suppEngineerWorkOrders = [];
      json['suppEngineerWorkOrders'].forEach((v) {
        suppEngineerWorkOrders!.add(SuppEngineerWorkOrders.fromJson(v));
      });
    }
    engSignature = json['engSignature'];
    nurseSignature = json['nurseSignature'];

    woParentDto = json['woParentDto'] != null ? WoParent.fromJson(json['woParentDto']) : null;
  }

  void copyFrom(SearchWorkOrder wo) {
    parentWOId = wo.parentWOId ?? parentWOId;
    workOrderNo = wo.workOrderNo ?? workOrderNo;
    workOrderYear = wo.workOrderYear ?? workOrderYear;
    workOrderSequennce = wo.workOrderSequennce ?? workOrderSequennce;
    callRequest = wo.callRequest ?? callRequest;
    if (wo.assetType != null) assetType = Lookup.fromJson((wo.assetType)?.toJson() ?? {});
    if (wo.assignedEmployee != null) assignedEmployee = AssignedEmployee.fromJson((wo.assignedEmployee)?.toJson() ?? {});
    visitDate = wo.visitDate ?? visitDate;
    assistantEmployees = (wo.assistantEmployees ?? assistantEmployees)
            ?.map((e) => AssistantEmployees.fromJson({
                  "id": e.id,
                  "user": {"name": e.user?.name, "id": (e.user?.id) ?? "0"}
                }))
            .toList() ??
        [];
    if (wo.supplier != null) supplier = SupplierDetails.fromJson((wo.supplier)?.toJson() ?? {});
    vendorTicketNumber = wo.vendorTicketNumber ?? vendorTicketNumber;
    contactPersonWorkOrders = (wo.contactPersonWorkOrders ?? contactPersonWorkOrders)?.map((e) => ContactPersonWorkOrder.fromJson(e.toJson() ?? {})).toList() ?? [];
    calllastSituation = Lookup.fromJson((wo.calllastSituation ?? calllastSituation)?.toJson() ?? {});
    if (wo.currentSituation != null) currentSituation = Lookup.fromJson((wo.currentSituation)?.toJson() ?? {});
    repairLocation = Lookup.fromJson((wo.repairLocation ?? repairLocation)?.toJson() ?? {});
    reason = Lookup.fromJson((wo.reason ?? reason)?.toJson() ?? {});
    // startofWorkTime = wo.startofWorkTime ?? startofWorkTime;
    // endofWorkTime = wo.endofWorkTime ?? endofWorkTime;
    timer = wo.timer ?? timer;
    workingHours = wo.workingHours ?? workingHours;
    travelingHours = wo.travelingHours ?? travelingHours;
    travelingExpenses = wo.travelingExpenses ?? travelingExpenses;
    faultDescription = FaultDescription.fromJson((wo.faultDescription ?? faultDescription)?.toJson() ?? {});
    sparePartsWorkOrders = (wo.sparePartsWorkOrders ?? sparePartsWorkOrders)?.map((e) => SparePartsWorkOrders.fromJson(e.toJson() ?? {})).toList() ?? [];
    reviewComment = wo.reviewComment ?? reviewComment;
    comment = wo.comment ?? comment;
    attachmentsWorkOrder = (wo.attachmentsWorkOrder ?? attachmentsWorkOrder)?.map((e) => GenericAttachmentModel.fromJson(e?.toJson() ?? {})).toList() ?? [];
    equipmentStatus = wo.equipmentStatus ?? equipmentStatus;
    suppEngineerWorkOrders = (wo.suppEngineerWorkOrders ?? suppEngineerWorkOrders)?.map((e) => SuppEngineerWorkOrders.fromJson(e.toJson() ?? {})).toList() ?? [];
    engSignature = wo.engSignature ?? engSignature;
    nurseSignature = wo.nurseSignature ?? nurseSignature;
    woParentDto = wo.woParentDto ?? woParentDto;
    mrNumber = wo.mrNumber ?? mrNumber;
  }

  num? id;
  num? parentWOId;
  String? workOrderNo;
  num? workOrderYear;
  num? workOrderSequennce;
  CallRequest? callRequest;
  Lookup? assetType;
  AssignedEmployee? assignedEmployee;
  String? visitDate;
  List<AssistantEmployees>? assistantEmployees;
  SupplierDetails? supplier;
  String? vendorTicketNumber;
  List<ContactPersonWorkOrder>? contactPersonWorkOrders;
  Lookup? calllastSituation;
  Lookup? currentSituation;
  Lookup? repairLocation;
  Lookup? reason;

  // String startofWorkTime;
  // String endofWorkTime;
  num? workingHours;
  num? travelingHours;
  num? travelingExpenses;
  FaultDescription? faultDescription;
  List<SparePartsWorkOrders>? sparePartsWorkOrders;
  String? reviewComment;
  String? comment;
  List<GenericAttachmentModel>? attachmentsWorkOrder;
  Lookup? equipmentStatus;
  List<SuppEngineerWorkOrders>? suppEngineerWorkOrders;
  String? engSignature;
  Uint8List? localEngSignature;
  String? nurseSignature;
  Uint8List? localNurseSignature;
  WoParent? woParentDto;
  TimerModel? timer;
  String? mrNumber;

  Lookup? loanAvailablity;

  AssetInfo? assetLoan;

  SearchWorkOrder copyWith({
    num? id,
    num? parentWOId,
    String? workOrderNo,
    num? workOrderYear,
    num? workOrderSequennce,
    CallRequest? callRequest,
    Lookup? assetType,
    AssignedEmployee? assignedEmployee,
    String? visitDate,
    List<AssistantEmployees>? assistantEmployees,
    SupplierDetails? supplier,
    String? vendorTicketNumber,
    List<ContactPersonWorkOrder>? contactPersonWorkOrders,
    Lookup? calllastSituation,
    Lookup? currentSituation,
    Lookup? repairLocation,
    Lookup? reason,
    // String startofWorkTime,
    // String endofWorkTime,
    num? workingHours,
    num? travelingHours,
    num? travelingExpenses,
    FaultDescription? faultDescription,
    List<SparePartsWorkOrders>? sparePartsWorkOrders,
    String? reviewComment,
    String? comment,
    List<GenericAttachmentModel>? attachmentsWorkOrder,
    Lookup? equipmentStatus,
    List<SuppEngineerWorkOrders>? suppEngineerWorkOrders,
    String? engSignature,
    String? nurseSignature,
    WoParent? woParentDto,
    TimerModel? timer,
    String? mrNumber,
    Lookup? loanAvailablity,
    AssetInfo? assetLoan,
  }) =>
      SearchWorkOrder(
        id: id ?? this.id,
        parentWOId: parentWOId ?? this.parentWOId,
        workOrderNo: workOrderNo ?? this.workOrderNo,
        workOrderYear: workOrderYear ?? this.workOrderYear,
        workOrderSequennce: workOrderSequennce ?? this.workOrderSequennce,
        callRequest: callRequest ?? this.callRequest,
        assetType: assetType ?? this.assetType,
        assignedEmployee: assignedEmployee ?? this.assignedEmployee,
        visitDate: visitDate ?? this.visitDate,
        assistantEmployees: assistantEmployees ?? this.assistantEmployees,
        supplier: supplier ?? this.supplier,
        vendorTicketNumber: vendorTicketNumber ?? this.vendorTicketNumber,
        contactPersonWorkOrders: contactPersonWorkOrders ?? this.contactPersonWorkOrders,
        calllastSituation: calllastSituation ?? this.calllastSituation,
        currentSituation: currentSituation ?? this.currentSituation,
        repairLocation: repairLocation ?? this.repairLocation,
        reason: reason ?? this.reason,
        // startofWorkTime: startofWorkTime ?? this.startofWorkTime,
        // endofWorkTime: endofWorkTime ?? this.endofWorkTime,
        workingHours: workingHours ?? this.workingHours,
        travelingHours: travelingHours ?? this.travelingHours,
        travelingExpenses: travelingExpenses ?? this.travelingExpenses,
        faultDescription: faultDescription ?? this.faultDescription,
        sparePartsWorkOrders: sparePartsWorkOrders ?? this.sparePartsWorkOrders,
        reviewComment: reviewComment ?? this.reviewComment,
        comment: comment ?? this.comment,
        attachmentsWorkOrder: attachmentsWorkOrder ?? this.attachmentsWorkOrder,
        equipmentStatus: equipmentStatus ?? this.equipmentStatus,
        suppEngineerWorkOrders: suppEngineerWorkOrders ?? this.suppEngineerWorkOrders,
        engSignature: engSignature ?? this.engSignature,
        nurseSignature: nurseSignature ?? this.nurseSignature,
        woParentDto: woParentDto ?? this.woParentDto,
        timer: timer ?? this.timer,
        mrNumber: mrNumber ?? this.mrNumber,
        loanAvailablity: loanAvailablity ?? this.loanAvailablity,
        assetLoan: assetLoan ?? this.assetLoan,
      );

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['id'] = id ?? 0;
    map['parentWOId'] = parentWOId;
    map['workOrderNo'] = workOrderNo;
    map['workOrderYear'] = workOrderYear;
    map['workOrderSequennce'] = workOrderSequennce;
    if (callRequest != null) {
      map['callRequest'] = callRequest!.toJson();
    }
    if (assetType != null) {
      map['assetType'] = assetType!.toJson();
    }
    if (assignedEmployee != null) {
      map['assignedEmployee'] = assignedEmployee!.toJson();
    }
    map['visitDate'] = visitDate;
    if (assistantEmployees != null) {
      map['assistantEmployees'] = assistantEmployees!.map((v) => v.toJson()).toList();
    }
    if (supplier != null) {
      map['supplier'] = supplier!.toJson();
    }
    map['vendorTicketNumber'] = vendorTicketNumber;
    if (contactPersonWorkOrders != null) {
      map['contactPersonWorkOrders'] = contactPersonWorkOrders!.map((v) => v.toJson()).toList();
    }
    if (calllastSituation != null) {
      map['calllastSituation'] = calllastSituation!.toJson();
    }
    map['currentSituation'] = currentSituation?.toJson();
    if (repairLocation?.id != null) {
      map['repairLocation'] = repairLocation!.toJson();
    }
    if (reason?.id != null) {
      map['reason'] = reason!.toJson();
    }
    // map['startofWorkTime'] = startofWorkTime;
    // map['endofWorkTime'] = endofWorkTime;
    map['startofWorkTime'] = timer?.startAt?.toIso8601String();
    map['endofWorkTime'] = timer?.endAt?.toIso8601String();
    map['workingHours'] = workingHours;
    map['travelingHours'] = travelingHours;
    map['travelingExpenses'] = travelingExpenses;

    if (faultDescription?.id != null) {
      map['faultDescription'] = faultDescription!.toJson();
    }
    if (sparePartsWorkOrders != null) {
      map['sparePartsWorkOrders'] = sparePartsWorkOrders!.map((v) => v.toJson()).toList();
    }
    map['reviewComment'] = reviewComment;
    map['comment'] = comment;
    if (attachmentsWorkOrder != null) {
      map['attachmentsWorkOrder'] = attachmentsWorkOrder!.map((v) => v.toJson()).toList();
    }
    if ( equipmentStatus?.id != null) {
      map['equipmentStatus'] = equipmentStatus!.toJson();
    }
    if (suppEngineerWorkOrders != null) {
      map['suppEngineerWorkOrders'] = suppEngineerWorkOrders!.map((v) => v.toJson()).toList();
    }
    map['engSignature'] = engSignature;
    map['nurseSignature'] = nurseSignature;
    if (woParentDto != null) {
      map['woParentDto'] = woParentDto!.toJson();
    }
    if (mrNumber != null) {
      map['mrNumber'] = mrNumber;
    }
    if (loanAvailablity != null) {
      map['loanAvailablity'] = loanAvailablity!.toJson();
    }
    if (assetLoan != null) {
      map['assetLoan'] = assetLoan!.toJson();
    }
    return map;
  }

  void copyDetails(ServiceReport subWoDetails) {
    equipmentStatus = subWoDetails.equipmentStatus;
    reason = subWoDetails.reason;
    faultDescription = subWoDetails.faultDescription;
    sparePartsWorkOrders = subWoDetails.sparePartsWorkOrders;
    suppEngineerWorkOrders = subWoDetails.suppEngineerWorkOrders;
    supplier = subWoDetails.supplier;
  }
}
