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

class GenericAttachmentModel {
  GenericAttachmentModel({
    this.id,
    this.name,
    this.originalName,
    this.createdBy,
    this.documentType,
    this.attachmentDescription,
    this.attachmentURL,
    this.moduleReferenceId,
    this.attachmentTypeId,
    this.createdDate,
    this.modifiedBy,
    this.modifiedDate,
    this.supplierId,
  });

  int? id;
  String? name;
  String? createdBy;
  String? originalName;
  Lookup? documentType;
  String? attachmentDescription;
  String? attachmentURL;
  num? moduleReferenceId;
  num? attachmentTypeId;
  String? createdDate;
  String? modifiedBy;
  String? modifiedDate;
  num? supplierId;

  GenericAttachmentModel.fromJson(Map<String, dynamic> json, {bool convertToLink = false}) {
    id = json['id'];
    name = json['name'] ?? json['attachmentName'] ?? json['imageName'];
    createdBy = json['createdBy'];
    documentType = json['documentType'];
    originalName = json['originalName'] ?? json['imageOriginalName'];

    if (convertToLink) {
      name = URLs.getFileUrl(name);
    }
  }

  GenericAttachmentModel.fromWorkOrderJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    createdBy = json['createdBy'];
  }

  GenericAttachmentModel.fromLoanJson(Map<String, dynamic> json) {
    id = json['id']?.toInt();
    name = json['attachmentName'];
    attachmentDescription = json['attachmentDescription'];
    documentType = json['documentType'];
    moduleReferenceId = json['loanId'];
    attachmentTypeId = json['loanAttachmentTypeId'];
  }

  GenericAttachmentModel.fromLoanDetailJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
    attachmentDescription = json['attachmentDescription'];
    moduleReferenceId = json['loanId'];
    attachmentTypeId = json['loanAttachmentTypeId'];
    createdBy = json['createdBy'];
    createdDate = json['createdDate'];
    modifiedBy = json['modifiedBy'];
    modifiedDate = json['modifiedDate'];
  }

  GenericAttachmentModel.fromPpmJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'] ?? json['name'];
    moduleReferenceId = json['visitId'];
    attachmentURL = json['attachmentURL'];
  }
  GenericAttachmentModel.fromSupplierJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'] ?? json['name'];
    supplierId = json['supplierId'];
    moduleReferenceId = json['visitId'];
    attachmentURL = json['attachmentURL'];
  }

  GenericAttachmentModel.fromGasRefillJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
    moduleReferenceId = json['gasRefillId'];
    documentType = json['documentType'];
  }

  GenericAttachmentModel.fromIncidentJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
    moduleReferenceId = json['incidentId'];
    attachmentDescription = json['attachmentDescription'];
  }

  GenericAttachmentModel.fromAssetTransferJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
  }

  GenericAttachmentModel.fromDemoJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
    originalName = json['originalName'];
    moduleReferenceId = json['demoRequestId'];
    documentType = json['documentType'] != null ? Lookup.fromJson(json['documentType']) : null;
  }

  GenericAttachmentModel.fromInternalAuditJson(Map<String, dynamic> json) {
    id = json['id'];
    name = (json['name'] != null && !json['name'].toString().startsWith('data:image/jpeg')) ? json['name'] : null;
    originalName = json['originalName'];
    createdBy = json['createdBy'];
    documentType = json['documentType'];
  }

  GenericAttachmentModel.fromPreventiveVisitJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
    documentType = json['documentType'];
  }

  GenericAttachmentModel.fromAssetJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
    attachmentURL = json['attachmentURL'];
    originalName = json['originalName'];
  }

  GenericAttachmentModel.fromTrafJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['attachmentName'];
    moduleReferenceId = json['trafId'];
    documentType = json['documentType'];
  }


  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['name'] = name;
    data['createdBy'] = createdBy;
    data['originalName'] = originalName;
    data['documentTypeId'] = documentType?.id;
    return data;
  }

  Map<String, dynamic> toWorkOrderJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['name'] = name;
    data['createdBy'] = createdBy;
    ///TODO need to keep same variable for document type in backend for all modules, currently it is different for different modules
    if (documentType != null) {
      data['attachmentTypeId'] = documentType?.id;
    }
    return data;
  }

  Map<String, dynamic> toLoanJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attachmentName'] = name;
    data['loanId'] = moduleReferenceId;
    data['loanAttachmentTypeId'] = attachmentTypeId;
    data['documentTypeId'] = documentType?.id;
    data['attachmentDescription'] = attachmentDescription;
    return data;
  }

  Map<String, dynamic> toLoanDetailJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attachmentName'] = name;
    data['loanId'] = moduleReferenceId;
    data['loanAttachmentTypeId'] = attachmentTypeId;
    data['attachmentDescription'] = attachmentDescription;
    data['createdBy'] = createdBy;
    data['createdDate'] = createdDate;
    data['documentTypeId'] = documentType?.id;

    data['modifiedBy'] = modifiedBy;
    data['modifiedDate'] = modifiedDate;
    return data;
  }
//TODO need to check where to use this need to verify with backend team.
  Map<String, dynamic> toPpmJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['visitId'] = moduleReferenceId;
    data['attachmentName'] = name;
    data['documentTypeId'] = documentType?.id;
    data['attachmentURL'] = attachmentURL;
    return data;
  }

  Map<String, dynamic> toGasRefillJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['gasRefillId'] = moduleReferenceId;
    data['attachmentName'] = name;
    data['documentTypeId'] = documentType?.id;
    return data;
  }

  Map<String, dynamic> toIncidentJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attachmentName'] = name;
    data['incidentId'] = moduleReferenceId;
    data['documentTypeId'] = documentType?.id;
    data['attachmentDescription'] = attachmentDescription;
    return data;
  }

  Map<String, dynamic> toAssetTransferJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attachmentName'] = name;
    data['documentTypeId'] = documentType?.id;
    return data;
  }

  Map<String, dynamic> toDemoJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attachmentName'] = name;
    data['demoRequestId'] = moduleReferenceId;
    data['originalName'] = originalName;
    if (documentType != null) {
      data['documentTypeId'] = documentType?.id;
    }
    return data;
  }

  Map<String, dynamic> toInternalAuditJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['name'] = name;
    data['originalName'] = originalName;
    data['documentTypeId'] = documentType?.id;
    return data;
  }

  Map<String, dynamic> toPreventiveVisitJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attachmentName'] = name;
    data['documentTypeId'] = documentType?.id;
    return data;
  }

  Map<String, dynamic> toAssetJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['attachmentName'] = name;
    data['attachmentURL'] = attachmentURL;
    data['documentTypeId'] = documentType?.id;
    data['originalName'] = originalName;
    return data;
  }

  Map<String, dynamic> toTrafJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['trafId'] = moduleReferenceId;
    data['attachmentName'] = name;
    data['documentTypeId'] = documentType?.id;
    return data;
  }

  Map<String, dynamic> toSupplierJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['supplierId'] = supplierId;
    data['documentTypeId'] = documentType?.id;
    data['attachmentName'] = name;
    data['attachmentURL'] = attachmentURL;
    return data;
  }
}
