import 'dart:developer';

import 'package:test_sa/models/device/supplier.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/new_models/building.dart';
import 'package:test_sa/models/new_models/floor.dart';
import 'package:test_sa/models/new_models/room_model.dart';
import 'package:test_sa/models/new_models/site.dart';

import '../../../models/device/model_definition.dart';
import '../../../models/new_models/department.dart';

class TechnicalInspectionAssetModel {
  num? id;
  num? tableItemId;
  num? lineId;
  num? lineNo;
  num? assetDeliveryInternalDetailAssetId;
  num? assetDeliveryInternalDetailAssetChildId;
  num? costCenterId;
  String? assetNumber;
  String? status;
  Lookup? assetOrigin;
  Lookup? replacedAsset;
  AssetModelDefination? modelDefinition;
  Supplier? supplier;
  Lookup? assetType; // Now nullable
  Site? site; // Now nullable
  Building? building; // Now nullable
  Floor? floor; // Now nullable
  Department? department; // Now nullable
  Rooms? room;
  String? serialNo;
  String? systemId;
  DateTime? installationDate;
  List<TechnicalInspectionAssetModel>? childAssetList;
  bool? flagphysicalInspection;
  bool? flagFunctionPerformance;
  bool? flagGroundingResistance;
  bool? flagChassiss;
  bool? flagLeadsLeakage;
  bool? flagAlert;

  TechnicalInspectionAssetModel({
    this.id,
    this.assetDeliveryInternalDetailAssetId,
    this.assetDeliveryInternalDetailAssetChildId,
    this.tableItemId,
    this.status,
    this.lineId,
    this.lineNo,
    this.costCenterId,
    this.assetNumber,
    this.assetOrigin,
    this.assetType,
    this.replacedAsset,
    this.modelDefinition,
    this.supplier,
    this.site,
    this.building,
    this.floor,
    this.department,
    this.room,
    this.serialNo,
    this.systemId,
    this.installationDate,
    this.childAssetList,
    this.flagphysicalInspection,
    this.flagFunctionPerformance,
    this.flagGroundingResistance,
    this.flagChassiss,
    this.flagLeadsLeakage,
    this.flagAlert,
  });

  TechnicalInspectionAssetModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    lineNo = json['lineNo'];
    assetDeliveryInternalDetailAssetId = json['assetDeliveryInternalDetailAssetId'];
    assetDeliveryInternalDetailAssetChildId = json['assetDeliveryInternalDetailAssetChildId'];
    assetNumber = json['assetNumber'];
    assetOrigin = json['assetOrigin'] != null ? Lookup.fromJson(json['assetOrigin']) : null;
    replacedAsset = json['replacedAsset'] != null ? Lookup.fromJson(json['replacedAsset']) : null;
    modelDefinition = json['modelDefinition'] != null ? AssetModelDefination.fromJson(json['modelDefinition']) : null;
    supplier = json['supplier'] != null ? Supplier.fromJson(json['supplier']) : null;
    assetType = json['assetType'] != null ? Lookup.fromJson(json['assetType']) : null;
    site = json['site'] != null ? Site.fromJson(json['site']) : null;
    building = json['building'] != null ? Building.fromJson(json['building']) : null;
    floor = json['floor'] != null ? Floor.fromJson(json['floor']) : null;
    department = json['department'] != null ? Department.fromJson(json['department']) : null;
    room = json['room'] != null ? Rooms.fromJson(json['room']) : null;
    serialNo = json['serialNo'];
    systemId = json['systemId'];
    installationDate = json['installationDate'] != null ? DateTime.tryParse(json['installationDate']) : null;
    childAssetList =
        json['assetDeliveryInternalDetailAssetChild'] != null ? (json['assetDeliveryInternalDetailAssetChild'] as List).map((e) => TechnicalInspectionAssetModel.fromJson(e)).toList() : [];
    //Need to keep one parm from backend
    // childAssetList = json['assetDeliveryExternalDeliveryLineTechnicalInspectionItemChild'] != null
    //     ? (json['assetDeliveryExternalDeliveryLineTechnicalInspectionItemChild'] as List).map((e) => TechnicalInspectionAssetModel.fromJson(e)).toList()
    //     : [];
    flagphysicalInspection = json['flagphysicalInspection'];
    flagFunctionPerformance = json['flagFunctionPerformance'];
    flagGroundingResistance = json['flagGroundingResistance'];
    flagChassiss = json['flagChassiss'];
    flagLeadsLeakage = json['flagLeadsLeakage'];
    flagAlert = json['flagAlert'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['id'] = id;
    // data['lineNo'] = lineNo;
    data['assetDeliveryInternalDetailAssetId'] = assetDeliveryInternalDetailAssetId;
    data['assetDeliveryExternalDeliveryId'] = tableItemId;
    data['assetDeliveryExternalDetailId'] = lineId;
    data['costCenterId'] = costCenterId;
    data['assetNumber'] = assetNumber;
    data['assetOriginId'] = assetOrigin?.id;
    data['assetTypeId'] = assetType?.id;
    data['replacedAssetId'] = replacedAsset?.id;
    data['modelDefinitionId'] = modelDefinition?.modelDefinitionId;
    data['supplierId'] = supplier?.id;
    data['siteId'] = site?.id;
    data['buildingId'] = building?.id;
    data['floorId'] = floor?.id;
    data['departmentId'] = department?.id;
    data['roomId'] = room?.id;
    data['serialNo'] = serialNo;
    data['systemId'] = systemId;
    data['installationDate'] = installationDate?.toIso8601String();
    data['assetDeliveryInternalDetailAssetChild'] = childAssetList?.map((e) => e.toChildJson()).toList();
    data['flagphysicalInspection'] = flagphysicalInspection;
    data['flagFunctionPerformance'] = flagFunctionPerformance;
    data['flagGroundingResistance'] = flagGroundingResistance;
    data['flagChassiss'] = flagChassiss;
    data['flagLeadsLeakage'] = flagLeadsLeakage;
    data['flagAlert'] = flagAlert;
    return data;
  }

  Map<String, dynamic> toChildJson() {
    final Map<String, dynamic> data = {};
    data['id'] = id ?? 0;
    data['assetDeliveryInternalDetailAssetChildId'] = assetDeliveryInternalDetailAssetChildId;
    data['assetNumber'] = assetNumber;
    data['assetOriginId'] = assetOrigin?.id;
    data['assetTypeId'] = assetType?.id;
    data['replacedAssetId'] = replacedAsset?.id;
    data['modelDefinitionId'] = modelDefinition?.modelDefinitionId;
    data['supplierId'] = supplier?.id;
    data['siteId'] = site?.id;
    data['buildingId'] = building?.id;
    data['floorId'] = floor?.id;
    data['departmentId'] = department?.id;
    data['roomId'] = room?.id;
    data['serialNo'] = serialNo;
    data['systemId'] = systemId;
    data['installationDate'] = installationDate?.toIso8601String();
    data['flagphysicalInspection'] = flagphysicalInspection;
    data['flagFunctionPerformance'] = flagFunctionPerformance;
    data['flagGroundingResistance'] = flagGroundingResistance;
    data['flagChassiss'] = flagChassiss;
    data['flagLeadsLeakage'] = flagLeadsLeakage;
    data['flagAlert'] = flagAlert;
    return data;
  }
}

class InspectionChecklistItem {
  String? title;
  String? value;
  String? status; // "PASS" or "FAIL"

  InspectionChecklistItem({this.title, this.status, this.value});

  InspectionChecklistItem.fromJson(Map<String, dynamic> json) {
    title = json['title'];
    value = json['value'];
    status = json['status'];
  }

  Map<String, dynamic> toJson() {
    return {
      'title': title,
      'value': value,
      'status': status,
    };
  }
}

class AssetModelDefination {
  int? modelDefinitionId;
  String? assetName;
  String? model;
  String? manufacturer;

  AssetModelDefination({
    this.modelDefinitionId,
    this.assetName,
    this.model,
    this.manufacturer,
  });

  AssetModelDefination.fromJson(Map<String, dynamic> json) {
    modelDefinitionId = json['modelDefinitionId'];
    assetName = json['assetName'];
    model = json['model'];
    manufacturer = json['manufacturer'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['modelDefinitionId'] = modelDefinitionId;
    data['assetName'] = assetName;
    data['model'] = model;
    data['manufacturer'] = manufacturer;
    return data;
  }
}
